拖動實現圖片移動效果
先寫一個手勢,注意圖片的 userInteractionEnabled設置為yes
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
UIImage *image = [UIImage imageNamed:@r.jpg];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 40, 80, 80)];
imageView.image = image;
imageView.userInteractionEnabled = YES;
[self addSubview:imageView];
[imageView addGestureRecognizer:pan];
}
拖動的方法,最後一句是關鍵代碼
- (void)pan:(UIPanGestureRecognizer *)gesture
{
CGPoint point = [gesture translationInView:self];
gesture.view.center = CGPointMake(gesture.view.center.x + point.x, gesture.view.center.y + point.y);
[gesture setTranslation:CGPointMake(0, 0) inView:self];
}