標籤:
代碼:
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 *)pinch103 {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)setUpPan116 {117 UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];118 [self.imageView addGestureRecognizer:pan];119 }120 - (void)pan:(UIPanGestureRecognizer *)pan121 {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 *)touch15 {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 }
iOS開發小功能之八:手勢的簡單使用(6種)以及代理方法