IOS 手勢學習(點擊,長按,輕掃,拖拽,旋轉,捏合縮放)
點擊 UITapGestureRecognizer長按 UILongPressGestureRecognizer
輕掃 UISwipeGestureRecognizer
拖拽 UIPanGestureRecognizer
旋轉 UIRotationGestureRecognizer
捏合縮放 UIPinchGestureRecognizer
詳細代碼如下:
import "ViewController.h"
@interface ViewController () <UIGestureRecognizerDelegate> { UIView * _view; } @end
@implementation ViewController
(void)viewDidLoad { [super viewDidLoad];
//創建視圖 _view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)]; _view.backgroundColor=[UIColor blueColor]; _view.center=self.view.center; [self.view addSubview:_view];
//點擊 UITapGestureRecognizer * tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick)];
//當前需要點擊次數
tap.numberOfTapsRequired=2;
//[tap setNumberOfTapsRequired:2]; //同上
//設置當前出發時間需要的手指數
tap.numberOfTouchesRequired=2; // option鍵 模擬手指
//[tap setNumberOfTouchesRequired:2]; //同上
[_view addGestureRecognizer:tap];
//長按 UILongPressGestureRecognizer * longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressClick)];
//在長按過程中移動10個單位也算長按
longPress.allowableMovement=10;
//[longPress setAllowableMovement:10]; //同上
//長按的最短時間
longPress.minimumPressDuration=2;
[_view addGestureRecognizer:longPress];
//輕掃 UISwipeGestureRecognizer * swipe=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction)];
//往右邊的方向 ——>
swipe.direction=UISwipeGestureRecognizerDirectionRight;
[_view addGestureRecognizer:swipe];
//輕掃 UISwipeGestureRecognizer * swipe2=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction2)];
//往左邊的方向 <——
swipe2.direction=UISwipeGestureRecognizerDirectionLeft;
[_view addGestureRecognizer:swipe2];
//輕掃 UISwipeGestureRecognizer * swipe3=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction3)];
//往上邊的方向 ??
swipe3.direction=UISwipeGestureRecognizerDirectionUp;
[_view addGestureRecognizer:swipe3];
//輕掃 UISwipeGestureRecognizer * swipe4=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction4)];
//往下邊的方向 ??
swipe4.direction=UISwipeGestureRecognizerDirectionDown;
[_view addGestureRecognizer:swipe4];
//拖拽 UIPanGestureRecognizer * pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
[_view addGestureRecognizer:pan];
//旋轉 UIRotationGestureRecognizer * rotation=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)]; rotation.delegate=self; [_view addGestureRecognizer:rotation];
//捏合縮放 UIPinchGestureRecognizer * pinch=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)]; pinch.delegate=self; [_view addGestureRecognizer:pinch];
}
pragma mark - 點擊
- (void) tapClick { NSLog(@"點擊"); } #pragma mark - 長按
- (void)longPressClick { NSLog(@"長按"); }
pragma mark - 向右滑
- (void) swipeAction { NSLog(@"right"); } #pragma mark - 向左滑
- (void) swipeAction2 { NSLog(@"left"); } #pragma mark - 向上滑
- (void) swipeAction3 { NSLog(@"up"); } #pragma mark - 向下滑
- (void) swipeAction4 { NSLog(@"down"); }
pragma mark - 拖拽移動
(void) panAction:(UIPanGestureRecognizer *) pan { //獲取移動的大小 CGPoint point=[pan translationInView:pan.view];
//更改視圖的中心點坐標 CGPoint points=_view.center; points.x+=point.x; points.y+=point.y; _view.center=points;
//每次都清空一下,消除坐標疊加 [pan setTranslation:CGPointZero inView:pan.view]; }
pragma mark - 旋轉
(void) rotation:(UIRotationGestureRecognizer *) rote { //獲取當前旋轉的度數 CGFloat rotation=rote.rotation;
//通過仿射變換實現旋轉 _view.transform=CGAffineTransformRotate(_view.transform, rotation); //防止旋轉疊加,清零 rote.rotation=0;
}
pragma mark - 縮放捏合
(void) pinch:(UIPinchGestureRecognizer *) pinch { //獲取比例 CGFloat scale=pinch.scale;
//通過仿射變換實現縮放 _view.transform=CGAffineTransformScale(_view.transform, scale, scale);
//防止比例疊加 pinch.scale=1;
}
pragma mark - 代理方法實現旋轉 + 縮放捏合 可同時進行
- (BOOL) gestureRecognizer:(UIGestureRecognizer )gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer )otherGestureRecognizer { return YES; }
- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }
@end
需要注意點:
1.鍵盤上的option鍵模擬手指
2.對同一個view來說,拖拽時,向左向右向上向下的手勢失效。
</pre>