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

iOS提供了拍击、旋转、滑动、挤捏等丰富的手势,因此对图像的操作就显得非常友好、简单。

下面代码展示了使用UIGestureRecognizer对图像进行缩放、移动、旋转操作使用方法。

在使用代码之前,首先建立一个UIView,并在该视图中增加一个UIImageView视图,用于展示图像。那么该UIView就类似图像画板一样,对图像的操作都基于此视图中进行。

在视图中创建手势识别器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];
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值