- 輕擊手勢(shì)(TapGestureRecognizer)
- 輕掃手勢(shì) (SwipeGestureRecognizer)
- 長(zhǎng)按手勢(shì)(LongPressGestureRecognizer)
- 拖動(dòng)手勢(shì)(PanGestureRecognizer)
- 捏合手勢(shì)(PinchGestureRecognizer)
- 旋轉(zhuǎn)手勢(shì)(RotationGestureRecognizer)
@interface ViewController () <UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 1.添加點(diǎn)擊手勢(shì)
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
// tap.numberOfTapsRequired = 2; // 要求雙擊才觸發(fā)事件
// tap.numberOfTouchesRequired = 2; // 要求兩個(gè)手指同時(shí)觸摸才觸發(fā)事件
tap.delegate = self;
// 將點(diǎn)擊手勢(shì)添加到imageView上
[_imageView addGestureRecognizer:tap];
// 2.添加移動(dòng)手勢(shì)
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[_imageView addGestureRecognizer:pan];
pan.delegate = self;
// 3.添加縮放手勢(shì)
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
[_imageView addGestureRecognizer:pinch];
pinch.delegate = self;
// 4.旋轉(zhuǎn)手勢(shì)
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
[_imageView addGestureRecognizer:rotation];
rotation.delegate = self;
// 5.輕掃手勢(shì)
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
// 設(shè)置輕掃的方向
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
[_imageView addGestureRecognizer:swipe];
swipe.delegate = self;
// 添加長(zhǎng)按手勢(shì)
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
//設(shè)置長(zhǎng)按時(shí)間
longPress.minimumPressDuration = 0.5;
[_imageView addGestureRecognizer:longPress];
longPress.delegate = self;
}
- (void)tap:(UITapGestureRecognizer *)tap {
NSLog(@"tap---");
}
- (void)pan:(UIPanGestureRecognizer *)pan {
// 獲得移動(dòng)手勢(shì)在self.view上面的偏移量(x, y)
CGPoint point = [pan translationInView:self.view];
// 讓圖片視圖隨著手指移動(dòng)
pan.view.center = CGPointMake(pan.view.center.x + point.x, pan.view.center.y + point.y);
// 重置手勢(shì)獲取的偏移量
[pan setTranslation:CGPointZero inView:self.view];
}
- (void)pinch:(UIPinchGestureRecognizer *)pinch {
// 參數(shù)一:原來(lái)的transform
// 參數(shù)二:水平方向縮放的倍數(shù)
// 參數(shù)三:垂直方向縮放的倍數(shù)
pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
// 重置縮放倍數(shù)
pinch.scale = 1.0;
}
- (void)rotation:(UIRotationGestureRecognizer *)rotation {
rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
// 重置角度
rotation.rotation = 0;
}
- (void)swipe:(UISwipeGestureRecognizer *)swipe {
NSLog(@"swipe");
}
- (void)longPress:(UILongPressGestureRecognizer *)longPress {
/*
說(shuō)明:長(zhǎng)按手勢(shì)的常用狀態(tài)如下
開始:UIGestureRecognizerStateBegan
改變:UIGestureRecognizerStateChanged
結(jié)束:UIGestureRecognizerStateEnded
取消:UIGestureRecognizerStateCancelled
失敿钒病:UIGestureRecognizerStateFailed
*/
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"longPress");
}
}
// 設(shè)置同時(shí)可以有兩個(gè)手勢(shì)同時(shí)識(shí)別
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者