你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS編程技術 >> iOS七大手勢識別

iOS七大手勢識別

編輯:IOS編程技術

也沒有什麼好說的,方法都差不多,只要記得當你想要同時實現兩個或多個手勢的話,要遵守<UIGestureRecognizerDelegate>協議,閒言休敘,直接上代碼:

 #import "RootViewController.h"
 #import "RootView.h"
 
 @interface RootViewController () <UIGestureRecognizerDelegate>  // 手勢識別器代理
 @property (nonatomic, strong) RootView *rootView;
 
 @end
 
 @implementation RootViewController
 
 - (void)loadView {
     self.rootView = [[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds];
     self.view = self.rootView;
 }
 
 
 - (void)viewDidLoad {
     [super viewDidLoad];
     
     // 輕拍
     [self tapGesture];
     
     // 平移
     //[self panGesture];
     
     // 縮放(捏合)
     [self pinchGesture];
     
     // 旋轉
     [self rotationGesture];
     
     // 長按
     [self longPressGesture];
     
     // 輕掃(滑動)
     [self swipeGesture];
     
     // 屏幕邊緣輕掃(滑動)
     [self screenEdgePanGesture];
 }
 
 
 #pragma mark - 輕拍手勢 UITapGestureRecognizer
 // 添加輕拍手勢
 - (void)tapGesture {
     // 1.創建手勢識別對象
     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAction:)];
     
     // 2.將手勢添加到相應視圖上
     [self.rootView.imageView addGestureRecognizer:tap];
 }
 
 // 實現輕拍手勢事件
 - (void)tapGestureAction:(UITapGestureRecognizer *)tap {
     NSLog(@"輕拍成功");
 }
 
 
 #pragma mark - 平移手勢 UIPanGestureRecognizer
 // 添加平移手勢
 - (void)panGesture {
     // 1.創建手勢識別對象
     UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureAction:)];
     
     // 2.將手勢添加到相應的視圖上
     [self.rootView.imageView addGestureRecognizer:pan];
     
 }
 
 // 實現平移手勢事件
 - (void)panGestureAction:(UIPanGestureRecognizer *)pan {
     
     NSLog(@"平移成功");
     
     // 在view上面挪動的距離
     CGPoint p = [pan translationInView:pan.view];
     pan.view.transform = CGAffineTransformTranslate(pan.view.transform, p.x, p.y);
     
     // 清空移動的距離
     [pan setTranslation:CGPointZero inView:pan.view];
 }
 
 
 #pragma mark - 縮放手勢(捏合)UIPinchGestureRecognizer
 // 添加平移手勢
 - (void)pinchGesture {
     // 1.創建手勢識別對象
     UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGestureAction:)];
     
     // 設置代理
     pinch.delegate = self;
     
     // 2.將手勢添加到相應的視圖上
     [self.rootView.imageView addGestureRecognizer:pinch];
     
 }
 
 // 實現縮放手勢事件
 - (void)pinchGestureAction:(UIPinchGestureRecognizer *)pinch {
     
     pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
     pinch.scale = 1;
     
 }
 
 
 #pragma mark - 旋轉手勢 UIRotationGestureRecognizer
 // 添加旋轉手勢
 - (void)rotationGesture {
     // 1.創建手勢識別對象
     UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGestureAction:)];
     
     // 設置代理
     rotation.delegate = self;
     
     // 2.將手勢添加到相應的視圖上
     [self.rootView.imageView addGestureRecognizer:rotation];
     
 }
 
 // 實現旋轉手勢事件
 - (void)rotationGestureAction:(UIRotationGestureRecognizer *)rotation {
     
     rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
     rotation.rotation = 0;
 }
 
 
 #pragma mark - 長按手勢 UILongPressGestureRecognizer
 // 添加長按手勢
 - (void)longPressGesture {
     // 1.創建手勢識別對象
     UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureAction:)];
     
     // 2.將手勢添加到相應的視圖上
     [self.rootView.imageView addGestureRecognizer:longPress];
 }
 
 // 實現長按手勢事件
 - (void)longPressGestureAction:(UILongPressGestureRecognizer *)longPress {
     // 開始長按的時候執行
     if (longPress.state == UIGestureRecognizerStateBegan) {
         NSLog(@"長按成功");
     }
 }
 
 
 #pragma mark - 輕掃手勢 UISwipeGestureRecognizer
 // 添加輕掃手勢
 - (void)swipeGesture {
     // 向上滑動
     // 1.創建手勢識別對象
     UISwipeGestureRecognizer *upSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureAction:)];
     upSwipe.direction = UISwipeGestureRecognizerDirectionUp;
     // 2.將手勢添加到相應的視圖上
     [self.rootView.imageView addGestureRecognizer:upSwipe];
     
     
     // 向下滑動
     // 1.創建手勢識別對象
     UISwipeGestureRecognizer *downSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureAction:)];
     downSwipe.direction = UISwipeGestureRecognizerDirectionDown;
     // 2.將手勢添加到相應的視圖上
     [self.rootView.imageView addGestureRecognizer:downSwipe];
     
     
     // 向右滑動
     // 1.創建手勢識別對象
     UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureAction:)];
     rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
     // 2.將手勢添加到相應的視圖上
     [self.rootView.imageView addGestureRecognizer:rightSwipe];
     
     
     // 向左滑動
     // 1.創建手勢識別對象
     UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureAction:)];
     leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
     // 2.將手勢添加到相應的視圖上
     [self.rootView.imageView addGestureRecognizer:leftSwipe];
 
 }
 
 // 實現輕掃手勢事件
 - (void)swipeGestureAction:(UISwipeGestureRecognizer *)swipe {
     if (swipe.direction == UISwipeGestureRecognizerDirectionUp) {
         NSLog(@"向上滑動");
     }
     
     if (swipe.direction == UISwipeGestureRecognizerDirectionDown) {
         NSLog(@"向下滑動");
     }
     
     if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
         NSLog(@"向左滑動");
     }
     
     if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
         NSLog(@"向右滑動");
     }
 }
 
 
 #pragma mark - 屏幕邊緣輕掃 UIScreenEdgePanGestureRecognizer
 // 添加屏幕邊緣輕掃手勢(也有多個方式,我們這裡只介紹一下,從屏幕頂部和左邊輕掃,其他的都一樣)
 - (void)screenEdgePanGesture {
     // 從屏幕頂部輕掃屏幕邊緣
     // 1.創建手勢識別對象
     UIScreenEdgePanGestureRecognizer *TopScreenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgePanGestureAction:)];
     TopScreenEdgePan.edges = UIRectEdgeTop;
     // 2.將手勢添加到相應的視圖上
     [self.rootView addGestureRecognizer:TopScreenEdgePan];
     
     
     // 從屏幕左邊輕掃屏幕邊緣
     // 1.創建手勢識別對象
     UIScreenEdgePanGestureRecognizer *leftScreenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgePanGestureAction:)];
     leftScreenEdgePan.edges = UIRectEdgeLeft;
     // 2.將手勢添加到相應的視圖上
     [self.rootView addGestureRecognizer:leftScreenEdgePan];
     
 }
 
 // 實現屏幕邊緣輕掃手勢
 - (void)screenEdgePanGestureAction:(UIScreenEdgePanGestureRecognizer *)screenEdgePan {
     if (screenEdgePan.edges == UIRectEdgeTop) {
         NSLog(@"從屏幕頂部輕掃屏幕邊緣");
     }
     
     if (screenEdgePan.edges == UIRectEdgeLeft) {
         NSLog(@"從屏幕左邊輕掃屏幕邊緣");
     }
 }
 
 
 #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. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved