UIPinchGestureRecognizer捏合手势
- alloc
- initWithTarget
- imageView.transform = CGAffineTransformScale(iView.transform, pinch.scale, pinch.scale);
// 对图像视图对象进行矩阵变换计算并赋值
// CGAffineTransformScale通过缩放的方式产生一个新的矩阵
//P1:原来的矩阵
//P2:x方向上的缩放比例
//P3:y方向上的缩放比例 - <UIGestureRecognizerDelegate>协议方法
- shouldRecognizeSimultaneouslyWithGestureRecognizer是否可以同时响应两种手势
UIRotationGestureRecognizer旋转手势
- alloc
- initWithTarget
- imageView.transform = CGAffineTransformRotate(iView.transform, rot.rotation);
具体使用:让一个图像视图支持缩放和旋转手势
主要代码如下
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImage* image = [UIImage imageNamed:@"icon1"];
UIImageView* iView = [[UIImageView alloc]initWithImage:image];
iView.frame = CGRectMake(100, 200, 150, 150);
//打开图像视图支持交互的开关
iView.userInteractionEnabled = YES;
[self.view addSubview:iView];
//创建捏合手势对象
_pinchGes = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAct:)];
[iView addGestureRecognizer:_pinchGes];
//创建旋转手势
_rotGes = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotAct:)];
[iView addGestureRecognizer:_rotGes];
_rotGes.delegate = self;
_pinchGes.delegate = self;
}
//协议方法:是否可以同时响应两个手势
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
-(void)rotAct:(UIRotationGestureRecognizer *)rot{
UIImageView* iView = (UIImageView *)rot.view;
//计算旋转的变换矩阵并且赋值
iView.transform = CGAffineTransformRotate(iView.transform, rot.rotation);
//旋转角度清零
rot.rotation = 0;
}
-(void)pinchAct:(UIPinchGestureRecognizer *)pinch{
//获取监控视图图像
UIImageView* iView = (UIImageView *)pinch.view;
//对图像视图对象进行矩阵变换计算并赋值
//CGAffineTransformScale通过缩放的方式产生一个新的矩阵
//P1:原来的矩阵
//P2:x方向上的缩放比例
//P3:y方向上的缩放比例
iView.transform = CGAffineTransformScale(iView.transform, pinch.scale, pinch.scale);
//将缩放值归位为单位值,由于响应函数是在每个瞬间都调用的(不管手指是否有滑动)
//如果不归零,缩放后保持手指触屏位置不变时,图片也会一直按照上一次缩放的比例继续缩放。
//scale=1原来的大小
pinch.scale = 1;
}
网友评论