使用手势对UIImageView进行缩放、旋转和移动
【摘要】
// 添加所有的手势 - (void) addGestureRecognizerToView:(UIView *)view { // 旋转手势 UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognize...
-
// 添加所有的手势
-
- (void) addGestureRecognizerToView:(UIView *)view
-
{
-
// 旋转手势
-
UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateView:)];
-
[view addGestureRecognizer:rotationGestureRecognizer];
-
-
// 缩放手势
-
UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];
-
[view addGestureRecognizer:pinchGestureRecognizer];
-
-
// 移动手势
-
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];
-
[view addGestureRecognizer:panGestureRecognizer];
-
}
-
-
// 处理旋转手势
-
- (void) rotateView:(UIRotationGestureRecognizer *)rotationGestureRecognizer
-
{
-
UIView *view = rotationGestureRecognizer.view;
-
if (rotationGestureRecognizer.state == UIGestureRecognizerStateBegan || rotationGestureRecognizer.state == UIGestureRecognizerStateChanged) {
-
view.transform = CGAffineTransformRotate(view.transform, rotationGestureRecognizer.rotation);
-
[rotationGestureRecognizer setRotation:0];
-
}
-
}
-
-
// 处理缩放手势
-
- (void) pinchView:(UIPinchGestureRecognizer *)pinchGestureRecognizer
-
{
-
UIView *view = pinchGestureRecognizer.view;
-
if (pinchGestureRecognizer.state == UIGestureRecognizerStateBegan || pinchGestureRecognizer.state == UIGestureRecognizerStateChanged) {
-
view.transform = CGAffineTransformScale(view.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);
-
pinchGestureRecognizer.scale = 1;
-
}
-
}
-
-
// 处理拖拉手势
-
- (void) panView:(UIPanGestureRecognizer *)panGestureRecognizer
-
{
-
UIView *view = panGestureRecognizer.view;
-
if (panGestureRecognizer.state == UIGestureRecognizerStateBegan || panGestureRecognizer.state == UIGestureRecognizerStateChanged) {
-
CGPoint translation = [panGestureRecognizer translationInView:view.superview];
-
[view setCenter:(CGPoint){view.center.x + translation.x, view.center.y + translation.y}];
-
[panGestureRecognizer setTranslation:CGPointZero inView:view.superview];
-
}
-
}
-
[self addGestureRecognizerToView:view];
-
-
//如果处理的是图片,别忘了
-
[imageView setUserInteractionEnabled:YES];
-
[imageView setMultipleTouchEnabled:YES];
文章来源: zzzili.blog.csdn.net,作者:清雨小竹,版权归原作者所有,如需转载,请联系作者。
原文链接:zzzili.blog.csdn.net/article/details/51145044
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)