拖动实现图片移动效果

2015-11-21 00:58:25 · 作者: · 浏览: 6

拖动实现图片移动效果

?

先写一个手势,注意图片的 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];

?

}


?