在touchMoved方法中
UITouch *touch = [touch anyObject];
CGPoint curP = [touch locationInView:self];
CGPoint preP = [touch previousLocationInView:self];
CGFloat offsetX = curP.x - preP.x;
//平移
self.transform = CGAffineTransformTranslate(self.transform, offsetX, 0);
如果采用pan手势平移, 则在UIPangestureRecognizer响应事件中
CGPoint transP = [pan translationInView:self.imageV];
self.imageV.transform = CGAffineTransformTranslate(self.imageV, transP.x, transP.y);
//让它相对于上一次复位
[pan setTranslation:CGPointZero inView:self.imageV];
如果采用pinch想要实现捏合缩放, 则在UIPinchRecognizer响应事件中
self.imageV.transform = CGAffineTransformScale(self.imageV.transform, pinch.scale, pinch.scale);
//复位
[pinch setScale:1];
想要旋转也是同理, 在UIRotationGestureRecognizer响应事件中,
self.imageV.transform = CGAffineTransformRotate(self.imageV.transform, rota.rotation);
//复位
[rota setRotation:0];
如果既想捏合又想旋转, 如果只是简单地添加两种手势, 会发现, 先做缩放或先做旋转, 那另一种就不能同时做了, 此时要分别对两种手势添加代理, UIGestureRecognizerDelegate, 并实现一个协议方法:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
让系统知道我们需要同时支持多种手势
当我们要实现view的某些地方可以响应手势, 某些地方不响应
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
CGPoint curP = [touch locationInView:self.imageV];
if(curP.x > self.imageV.frame.width * .5){
return YES;
}
else{
return NO;
}
}
网友评论