09-手勢識別(拖動,旋轉(zhuǎn),捏合)
1.平移
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self
action:@selector(pan:)];
添加手勢
[self.imageV addGestureRecognizer:pan];
實現(xiàn)手勢方法
手指在屏幕上移動進調(diào)用
- (void)pan:(UIPanGestureRecognizer *)pan{
獲取當(dāng)前手指移動的偏移量
CGPoint transP = [pan translationInView:self.imageV];
NSLog(@"%@",NSStringFromCGPoint(transP));
Make它會清空上一次的形變.
self.imageV.transform = CGAffineTransformMakeTranslation(transP.x, transP.y);
self.imageV.transform = CGAffineTransformTranslate(self.imageV.transform,
transP.x, transP.y);
復(fù)位,相對于上一次.
[pan setTranslation:CGPointZero inView:self.imageV];
}
2.旋轉(zhuǎn)
添加旋轉(zhuǎn)手勢
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]
initWithTarget:self action:@selector(rotation:)];
設(shè)置代理,設(shè)置代理的目的就讓它能夠同時支持旋轉(zhuǎn)跟縮放
rotation.delegate = self;
添加手勢
[self.imageV addGestureRecognizer:rotation];
當(dāng)旋轉(zhuǎn)時調(diào)用
- (void)rotation:(UIRotationGestureRecognizer *)rotation{
旋轉(zhuǎn)也是相對于上一次
self.imageV.transform = CGAffineTransformRotate(self.imageV.transform,
rotation.rotation);
設(shè)置代理,設(shè)置代理的目的就讓它能夠同時支持旋轉(zhuǎn)跟縮放
rotation.delegate = self;
也要做復(fù)位操作
rotation.rotation = 0;
}
3.添加縮放手勢
添加縮放手勢
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
[self.imageV addGestureRecognizer:pinch];
縮放手勢時調(diào)用
-(void)pinch:(UIPinchGestureRecognizer *)pinch{
平移也是相對于上一次
self.imageV.transform = CGAffineTransformScale(self.imageV.transform, pinch.scale,
pinch.scale);
復(fù)位
pinch.scale = 1;
}