返回顶部

收藏

使用手势UIGestureRecognizer对图像进行缩放、移动、旋转操作

更多

iOS提供了拍击、旋转、滑动、挤捏等丰富的手势,因此对图像的操作就显得非常友好、简单。下面代码展示了使用UIGestureRecognizer对图像进行缩放 、移动、旋转操作使用方法。在使用代码之前,首先建立一个UIView,并在该视图中增加一个UIImageView视图,用于展示图像。那么该UIView就类似图 像画板一样,对图像的操作都基于此视图中进行。http://www.ctolive.com/space-1023-do-blog-id-2109.html

在视图中创建手势识别器UIGestureRecognizers

- (void)viewDidLoad {
    [super viewDidLoad];

    UIPinchGestureRecognizer *pinchRecognizer = [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)] autorelease];
    [pinchRecognizer setDelegate:self];
    [self.view addGestureRecognizer:pinchRecognizer];

    UIRotationGestureRecognizer *rotationRecognizer = [[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)] autorelease];
    [rotationRecognizer setDelegate:self];
    [self.view addGestureRecognizer:rotationRecognizer];

    UIPanGestureRecognizer *panRecognizer = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)] autorelease];
    [panRecognizer setMinimumNumberOfTouches:1];
    [panRecognizer setMaximumNumberOfTouches:1];
    [panRecognizer setDelegate:self];
    [canvas addGestureRecognizer:panRecognizer];

    UITapGestureRecognizer *tapProfileImageRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)] autorelease];
    [tapProfileImageRecognizer setNumberOfTapsRequired:1];
    [tapProfileImageRecognizer setDelegate:self];
    [canvas addGestureRecognizer:tapProfileImageRecognizer];
}

下面是响应各手势消息的方法:

// 缩放
-(void)scale:(id)sender {

    if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
      _lastScale = 1.0;
    }

    CGFloat scale = 1.0 - (_lastScale - [(UIPinchGestureRecognizer*)sender scale]);

    CGAffineTransform currentTransform = photoImage.transform;
    CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);

    [photoImage setTransform:newTransform];

    _lastScale = [(UIPinchGestureRecognizer*)sender scale];
    [self showOverlayWithFrame:photoImage.frame];
}

// 旋转
-(void)rotate:(id)sender {

    if([(UIRotationGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {

      _lastRotation = 0.0;
      return;
    }

    CGFloat rotation = 0.0 - (_lastRotation - [(UIRotationGestureRecognizer*)sender rotation]);

    CGAffineTransform currentTransform = photoImage.transform;
    CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);

    [photoImage setTransform:newTransform];

    _lastRotation = [(UIRotationGestureRecognizer*)sender rotation];
    [self showOverlayWithFrame:photoImage.frame];
}

// 移动
-(void)move:(id)sender {

  CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:canvas];

  if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
    _firstX = [photoImage center].x;
    _firstY = [photoImage center].y;
  }

  translatedPoint = CGPointMake(_firstX+translatedPoint.x, _firstY+translatedPoint.y);

  [photoImage setCenter:translatedPoint];
  [self showOverlayWithFrame:photoImage.frame];
}

标签:Objective-C

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. 博主 发表 2018-10-08 04:18:16 Tagged Pointer 字符串
  2. 博主 发表 2018-07-31 16:50:57 MessageThrottle Safety
  3. 小芒 发表 2018-07-09 03:26:18 Objective-C代码开发规范篇
  4. 博主 发表 2018-06-24 09:52:26 追踪 Objective-C Block 代码定义的位置
  5. 博主 发表 2018-05-30 18:45:02 MessageThrottle Performance Benchmark and Optimiza
  6. Solrex 发表 2018-05-05 13:49:55 JS Bridge API - 安卓和iOS统一设计探讨
  7. 博主 发表 2018-03-31 15:49:10 追踪 Objective-C 方法中的 Block 参数对象
  8. 博主 发表 2018-02-28 11:52:34 Hook Objective-C Block with Libffi
  9. 博主 发表 2014-12-03 00:00:00 COSLayout: An iOS Layout Library
  10. 博主 发表 2017-12-14 16:19:38 Associated Object 与 Dealloc
  11. 博主 发表 2017-11-04 17:46:53 Objective-C Message Throttle and Debounce
  12. 博主 发表 2017-07-16 09:08:56 What's New in LLVM

发表评论