使用手势对UIImageView进行缩放、旋转和移动

简介: 使用手势对UIImageView进行缩放、旋转和移动
// 添加所有的手势  
- (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];  
相关文章
|
1月前
|
JavaScript 开发者
HarmonyNext动画大全03-帧动画
HarmonyNext动画大全03-帧动画
19 2
UIScrollView滑动选页
UIScrollView滑动选页
78 0
UIView随手指的移动
UIView随手指的移动
56 0
|
定位技术 图形学
交互式QGraphicsView(平移/缩放/旋转)
简述 Graphics View提供了一个平台,用于大量自定义 2D 图元的管理与交互,框架包括一个事件传播架构,支持场景 Scene 中的图元 Item 进行精确的双精度交互功能。Item 可以处理键盘事件、鼠标按下、移动、释放和双击事件,同时也能跟踪鼠标移动。 和 Google 地图一样,在管理大量 Item 的时候,通常需要 View 具有交互(平移/缩放/旋转)
10430 1
|
C#
WPF动画旋转(3轴同时旋转问题)
原文:WPF动画旋转(3轴同时旋转问题) WPF的资料比较少,做起来不是很方便,之前一直有个XYZ3个轴同时旋转的问题,开始的时候以为通过  this.
1220 0
|
图形学
Unity中的平移 缩放 旋转
Unity中的平移 缩放 旋转 1.最近想实现Unity中的平移 缩放 旋转功能, 类似于Unity编辑器状态下的效果.尝试了好几个方式,效果都有瑕疵,网友们谁有好的实现方式,请教我. 下面是我的一种实现方式: using System.
2255 0