UIPinchGestureRecognizer捏合手勢
- alloc
- initWithTarget
- imageView.transform = CGAffineTransformScale(iView.transform, pinch.scale, pinch.scale);
// 對(duì)圖像視圖對(duì)象進(jìn)行矩陣變換計(jì)算并賦值
// CGAffineTransformScale通過縮放的方式產(chǎn)生一個(gè)新的矩陣
//P1:原來的矩陣
//P2:x方向上的縮放比例
//P3:y方向上的縮放比例 - <UIGestureRecognizerDelegate>協(xié)議方法
- shouldRecognizeSimultaneouslyWithGestureRecognizer是否可以同時(shí)響應(yīng)兩種手勢
UIRotationGestureRecognizer旋轉(zhuǎn)手勢
- alloc
- initWithTarget
- imageView.transform = CGAffineTransformRotate(iView.transform, rot.rotation);
具體使用:讓一個(gè)圖像視圖支持縮放和旋轉(zhuǎn)手勢
主要代碼如下
- (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);
//打開圖像視圖支持交互的開關(guān)
iView.userInteractionEnabled = YES;
[self.view addSubview:iView];
//創(chuàng)建捏合手勢對(duì)象
_pinchGes = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAct:)];
[iView addGestureRecognizer:_pinchGes];
//創(chuàng)建旋轉(zhuǎn)手勢
_rotGes = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotAct:)];
[iView addGestureRecognizer:_rotGes];
_rotGes.delegate = self;
_pinchGes.delegate = self;
}
//協(xié)議方法:是否可以同時(shí)響應(yīng)兩個(gè)手勢
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
-(void)rotAct:(UIRotationGestureRecognizer *)rot{
UIImageView* iView = (UIImageView *)rot.view;
//計(jì)算旋轉(zhuǎn)的變換矩陣并且賦值
iView.transform = CGAffineTransformRotate(iView.transform, rot.rotation);
//旋轉(zhuǎn)角度清零
rot.rotation = 0;
}
-(void)pinchAct:(UIPinchGestureRecognizer *)pinch{
//獲取監(jiān)控視圖圖像
UIImageView* iView = (UIImageView *)pinch.view;
//對(duì)圖像視圖對(duì)象進(jìn)行矩陣變換計(jì)算并賦值
//CGAffineTransformScale通過縮放的方式產(chǎn)生一個(gè)新的矩陣
//P1:原來的矩陣
//P2:x方向上的縮放比例
//P3:y方向上的縮放比例
iView.transform = CGAffineTransformScale(iView.transform, pinch.scale, pinch.scale);
//將縮放值歸位為單位值尘吗,由于響應(yīng)函數(shù)是在每個(gè)瞬間都調(diào)用的(不管手指是否有滑動(dòng))
//如果不歸零鹉梨,縮放后保持手指觸屏位置不變時(shí)婴程,圖片也會(huì)一直按照上一次縮放的比例繼續(xù)縮放。
//scale=1原來的大小
pinch.scale = 1;
}