#import <UIKit/UIKit.h>
@interface MyImageView : UIImageView{
CGPoint startPoint;
}
@end
#import "MyImageView.h"
@implementation MyImageView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(id)initWithCoder:(NSCoder* )aDecoder{
self = [super initWithCoder:aDecoder];
if (self) {
//啟動觸控
self.userInteractionEnabled= YES;
//設定圖檔
self.image = [UIImage imageNamed:@"image4.jpg"];
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//取得觸控點
CGPoint clickPoint = [[touches anyObject]locationInView:self];
NSLog(@"觸控點在(%.1f,%.1f)",clickPoint.x,clickPoint.y);
startPoint = clickPoint;
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
CGPoint clickPoint = [[touches anyObject]locationInView:self];
//計算偏移值
float offsetX = clickPoint.x - startPoint.x;
float offsetY = clickPoint.y - startPoint.y;
//設定新值並更新
CGPoint newPoint = CGPointMake(self.center.x+offsetX, self.center.y+offsetY);
self.center = newPoint;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end
沒有留言:
張貼留言