代碼: 代理方法: ...
註意:全部都是在UIView上操作手勢
代碼:
1 #import "ViewController.h" 2 @interface ViewController () <UIGestureRecognizerDelegate> 4 @property (weak, nonatomic) IBOutlet UIImageView *imageView; 5 @end 7 @implementation ViewController 8 - (void)viewDidLoad { 9 [super viewDidLoad]; 10 //只要是手勢,預設都是相對於原來位置,且預設控制只支持一個手勢!!若想支持多個手勢,詳見下麵的代理方法 11 // [self setUpRotation]; 12 // [self setUpPinch]; 13 [self setUpPan]; 14 } 15 16 #pragma mark - 點按手勢 17 - (void)setUpTap 18 { 19 //創建點按手勢 20 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)]; 21 //需要連續點3次才能觸發 22 // tap.numberOfTapsRequired = 3; 23 //往imageView上添加點按手勢 24 [self.imageView addGestureRecognizer:tap]; 25 //設置點按手勢代理 26 tap.delegate = self; 27 } 28 29 - (void)tap:(UITapGestureRecognizer *)tap 30 { 31 NSLog(@"%s",__func__); 32 } 33 34 #pragma mark - 長按手勢 35 //預設觸發兩次(開始觸摸半秒後一次,結束觸摸一次) 36 - (void)setUpLongPress 37 { 38 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)]; 39 [self.imageView addGestureRecognizer:longPress]; 40 longPress.delegate = self; 41 } 42 43 - (void)longPress:(UILongPressGestureRecognizer *)longPress 44 { 45 開始的時候觸摸 46 if (longPress.state == UIGestureRecognizerStateBegan) { 47 NSLog(@"%s",__func__); 48 } 49 結束的時候觸摸 50 if (longPress.state == UIGestureRecognizerStateEnded) { 51 NSLog(@"%s",__func__); 52 } 53 } 54 55 #pragma mark - 輕掃手勢 56 - (void)setUpSwipe 57 { 58 //預設輕掃手勢往右劃屏幕 59 UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)]; 60 //向上劃 61 swipeUp.direction = UISwipeGestureRecognizerDirectionUp; 62 [_imageView addGestureRecognizer:swipeUp]; 63 //一個清掃手勢只支持一個方向,如果想支持多個方向,必須創建多個 64 UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)]; 65 //向下劃 66 swipeDown.direction = UISwipeGestureRecognizerDirectionDown; 67 [_imageView addGestureRecognizer:swipeDown]; 68 } 69 - (void)swipe:(UISwipeGestureRecognizer *)swipe 70 { 71 NSLog(@"%s",__func__); 72 } 73 74 #pragma mark - 旋轉手勢 75 //預設傳遞的角度都相對於最開始的位置 76 - (void)setUpRotation 77 { 78 UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)]; 79 rotation.delegate = self; 80 [self.imageView addGestureRecognizer:rotation]; 81 } 82 - (void)rotation:(UIRotationGestureRecognizer *)rotation 83 { 84 //讓圖片隨著手勢旋轉而旋轉,旋轉的角度相當於初始位置的值,配合rotation.rotation = 0;無法轉動 85 // self.imageView.transform = CGAffineTransformMakeRotation(rotation.rotation); 86 //手勢旋轉一下能讓圖片高速旋轉N圈,並且不受控制,方向不容易確定。配合rotation.rotation = 0;後角度一直為0 87 self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotation.rotation); 88 //複位 89 rotation.rotation = 0; 90 //獲取手勢旋轉的角度 91 NSLog(@"%f",rotation.rotation); 92 } 93 94 #pragma mark - 捏合手勢 95 //兩根手指放大縮小圖片 96 - (void)setUpPinch 97 { 98 UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)]; 99 pinch.delegate = self; 100 [self.imageView addGestureRecognizer:pinch]; 101 } 102 - (void)pinch:(UIPinchGestureRecognizer *)pinch 103 { 104 //放大的比例相對於原始點,註意:這個不能配合pinch.scale = 1使用,否則不能放大 105 // self.imageView.transform = CGAffineTransformMakeScale(pinch.scale, pinch.scale); 106 //隨著兩手指的距離增大,圖片會無規則的迅速的放大或者縮小(此處無法控制放大還是縮小),註意:配合pinch.scale = 1使用會正常的放大和縮小 107 self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale); 108 //複位 109 pinch.scale = 1; 110 NSLog(@"%f",pinch.scale); 111 } 112 113 #pragma mark - 拖拽 114 //移動視圖 115 - (void)setUpPan 116 { 117 UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)]; 118 [self.imageView addGestureRecognizer:pan]; 119 } 120 - (void)pan:(UIPanGestureRecognizer *)pan 121 { 122 //獲取手勢的移動,相對於原始點 123 CGPoint transP = [pan translationInView:self.imageView]; 124 self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, transP.x, transP.y); 125 //複位 126 [pan setTranslation:CGPointZero inView:self.imageView]; 127 NSLog(@"%@",NSStringFromCGPoint(transP)); 128 } 129 @end
代理方法:
1 是否允許同時支持多個手勢,預設只支持一個手勢,要調用此方法註意設置代理 2 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer 3 { 4 return YES; 5 } 6 7 是否允許開始觸發手勢 8 - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer 9 { 10 return NO; 11 } 12 13 是否允許接收手機的觸摸(可以控制觸摸的範圍) 14 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch 15 { 16 //獲取當前的觸摸點 17 CGPoint currentP = [touch locationInView:self.imageView]; 18 在圖片的左半區域可以接受觸摸 19 if (currentP.x < self.imageView.bounds.size.width * 0.5) { 20 return YES; 21 }else { 22 return NO; 23 } 24 }