7的六大手势操作功能 ios_ios手势操作 - CSDN
精华内容
参与话题
  • 现在使用默认模板创建的iOS App都支持手势返回功能,如果导航栏的返回按钮是自定义的那么则会失效,也可以参考这里手动设置无效。 [cpp] view plain copy   if ([self....


    现在使用默认模板创建的iOS App都支持手势返回功能,如果导航栏的返回按钮是自定义的那么则会失效,也可以参考这里手动设置无效。

    [cpp] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {     self.navigationController.interactivePopGestureRecognizer.enabled = NO;  
    2. }  

    如果是因为自定义导航按钮而导致手势返回失效,那么可以在NavigationController的viewDidLoad函数中添加如下代码:

    [cpp] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. - (void)viewDidLoad  
    2. {  
    3.     [super viewDidLoad];  
    4.     // Do any additional setup after loading the view.  
    5.       
    6.     __weak typeof (self) weakSelf = self;  
    7.     if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {  
    8.         self.interactivePopGestureRecognizer.delegate = weakSelf;  
    9.     }  
    10. }  

    这样写了以后就可以通过手势滑动返回上一层了,但是如果在push过程中触发手势滑动返回,会导致导航栏崩溃(从日志中可以看出)。针对这个问题,我们需要在pop过程禁用手势滑动返回功能:

    [cpp] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated  
    2. {    
    3.     // fix 'nested pop animation can result in corrupted navigation bar'  
    4.     if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {  
    5.         self.interactivePopGestureRecognizer.enabled = NO;  
    6.     }  
    7.       
    8.     [super pushViewController:viewController animated:animated];  
    9. }  

    [cpp] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. - (void)navigationController:(UINavigationController *)navigationController  
    2.        didShowViewController:(UIViewController *)viewController  
    3.                     animated:(BOOL)animated  
    4. {  
    5.     if ([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {  
    6.         navigationController.interactivePopGestureRecognizer.enabled = YES;  
    7.     }  
    8. }  

    除了使用系统默认的动画,还可以使用自定义过渡动画(丰满的文档):

    [cpp] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController  
    2.                                   animationControllerForOperation:(UINavigationControllerOperation)operation  
    3.                                                fromViewController:(UIViewController *)fromVC  
    4.                                                  toViewController:(UIViewController *)toVC  
    5. {  
    6.     if (operation == UINavigationControllerOperationPop) {  
    7.         if (self.popAnimator == nil) {  
    8.             self.popAnimator = [WQPopAnimator new];  
    9.         }  
    10.         return self.popAnimator;  
    11.     }  
    12.       
    13.     return nil;  
    14. }  
    15.   
    16. - (id<UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController  
    17.                          interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController  
    18. {  
    19.     return self.popInteractionController;  
    20. }  
    21.   
    22. #pragma mark -   
    23.   
    24. - (void)enablePanToPopForNavigationController:(UINavigationController *)navigationController  
    25. {  
    26.     UIScreenEdgePanGestureRecognizer *left2rightSwipe = [[UIScreenEdgePanGestureRecognizer alloc]  
    27.                                                          initWithTarget:self  
    28.                                                          action:@selector(didPanToPop:)];  
    29.     //[left2rightSwipe setDelegate:self];  
    30.     [left2rightSwipe setEdges:UIRectEdgeLeft];  
    31.     [navigationController.view addGestureRecognizer:left2rightSwipe];  
    32.       
    33.     self.popAnimator = [WQPopAnimator new];  
    34.     self.supportPan2Pop = YES;  
    35. }  
    36.   
    37. - (void)didPanToPop:(UIPanGestureRecognizer *)panGesture  
    38. {  
    39.     if (!self.supportPan2Pop) return ;  
    40.       
    41.     UIView *view = self.navigationController.view;  
    42.       
    43.     if (panGesture.state == UIGestureRecognizerStateBegan) {  
    44.         self.popInteractionController = [UIPercentDrivenInteractiveTransition new];  
    45.         [self.navigationController popViewControllerAnimated:YES];  
    46.     } else if (panGesture.state == UIGestureRecognizerStateChanged) {  
    47.         CGPoint translation = [panGesture translationInView:view];  
    48.         CGFloat d = fabs(translation.x / CGRectGetWidth(view.bounds));  
    49.         [self.popInteractionController updateInteractiveTransition:d];  
    50.     } else if (panGesture.state == UIGestureRecognizerStateEnded) {  
    51.         if ([panGesture velocityInView:view].x > 0) {  
    52.             [self.popInteractionController finishInteractiveTransition];  
    53.         } else {  
    54.             [self.popInteractionController cancelInteractiveTransition];  
    55.         }  
    56.         self.popInteractionController = nil;  
    57.     }  
    58. }  

    如下这个代理方法是用来提供一个非交互式的过渡动画的:

    [cpp] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController  
    2.                                   animationControllerForOperation:(UINavigationControllerOperation)operation  
    3.                                                fromViewController:(UIViewController *)fromVC  
    4.                                                  toViewController:(UIViewController *)toVC  
    5. {  
    6.     if (operation == UINavigationControllerOperationPop) {  
    7.         if (self.popAnimator == nil) {  
    8.             self.popAnimator = [WQPopAnimator new];  
    9.         }  
    10.         return self.popAnimator;  
    11.     }  
    12.       
    13.     return nil;  
    14. }  

    而下面这个代理方法则是提供交互式动画:

    [cpp] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. - (id<UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController  
    2.                          interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController  
    3. {  
    4.     return self.popInteractionController;  
    5. }  

    这两个组合起来使用。首先,我们需要有个动画:

    [cpp] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. @interface WQPopAnimator : NSObject <UIViewControllerAnimatedTransitioning>  
    2.   
    3. @end  

    [cpp] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. #import "WQPopAnimator.h"  
    2.   
    3. @implementation WQPopAnimator  
    4.   
    5. - (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext  
    6. {  
    7.     return 0.4;  
    8. }  
    9.   
    10. - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext  
    11. {  
    12.     UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];  
    13.     UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];  
    14.     [[transitionContext containerView] insertSubview:toViewController.view belowSubview:fromViewController.view];  
    15.       
    16.     __block CGRect toRect = toViewController.view.frame;  
    17.     CGFloat originX = toRect.origin.x;  
    18.     toRect.origin.x -= toRect.size.width / 3;  
    19.     toViewController.view.frame = toRect;  
    20.       
    21.     [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{  
    22.         CGRect fromRect = fromViewController.view.frame;  
    23.         fromRect.origin.x += fromRect.size.width;  
    24.         fromViewController.view.frame = fromRect;  
    25.           
    26.         toRect.origin.x = originX;  
    27.         toViewController.view.frame = toRect;  
    28.     } completion:^(BOOL finished) {  
    29.         [transitionContext completeTransition:![transitionContext transitionWasCancelled]];  
    30.     }];  
    31. }  
    32.   
    33. @end  

    其次,交互式动画是通过
    [cpp] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. UIPercentDrivenInteractiveTransition  
    来维护的,在滑动过程中根据滑动距离来进行更新:
    [cpp] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. else if (panGesture.state == UIGestureRecognizerStateChanged) {  
    2.         CGPoint translation = [panGesture translationInView:view];  
    3.         CGFloat d = fabs(translation.x / CGRectGetWidth(view.bounds));  
    4.         [self.popInteractionController updateInteractiveTransition:d];  
    当手势结束时要做出收尾动作:

    [cpp] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. else if (panGesture.state == UIGestureRecognizerStateEnded) {  
    2.         if ([panGesture velocityInView:view].x > 0) {  
    3.             [self.popInteractionController finishInteractiveTransition];  
    4.         } else {  
    5.             [self.popInteractionController cancelInteractiveTransition];  
    6.         }  
    7.         self.popInteractionController = nil;  
    8.     }  

    同样地,自定义的动画也会有上面提到的导航栏崩溃问题,也可以通过类似的方法来解决:

    [cpp] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. - (void)navigationController:(UINavigationController *)navigationController  
    2.        didShowViewController:(UIViewController *)viewController  
    3.                     animated:(BOOL)animated  
    4. {  
    5.     if (viewController == self.navigationController.pushingViewController) {  
    6.         self.supportPan2Pop = YES;  
    7.         self.navigationController.pushingViewController = nil;  
    8.     }  

    补充:位于当前navgationController的第一个([0])viewController时需要设置手势代理,不响应。






    转载自:http://blog.csdn.net/jasonblog/article/details/28282147

    展开全文
  • iOS 手势详解 UIGestureRecognizer
    • UITapGestureRecognizer   轻拍

    • UIPinchGestureRecognizer  捏合

    • UIRotationGestureRecognizer  旋转

    • UISwipeGestureRecognizer   轻扫

    • UIPanGestureRecognizer   拖拽

    • UILongPressGestureRecognizer  长按


    具体实现代码如下(需要哪种就把注释部分粘贴下就可以了): 

    - (void)viewDidLoad 

        [super viewDidLoad]; 

        self.view.backgroundColor = [UIColor grayColor]; 

    // Do any additional setup after loading the view. 

        UIImage *image = [UIImage imageNamed:@"iphone.jpg"]; 

        UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 

         

        //设置UIImage可接受交互 

        imageView.userInteractionEnabled = YES; 

         

        [imageView setFrame:CGRectMake(20, 20, 280, 440)]; 

        [self.view addSubview:imageView]; 

        [imageView release]; 

         

    /*   

     *  轻拍手势 

      

    //  轻拍 单击 

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)]; 

        

    //  单击 

        tap.numberOfTapsRequired = 1; 

    //  试图添加一个手势,UIView的方法 

        [imageView addGestureRecognizer:tap]; 

        [tap release]; 

         

    //  轻拍 双击 

        UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap1:)]; 

         

    //  双击 

        tap1.numberOfTapsRequired = 2; 

    //  试图添加一个手势,UIView的方法 

        [imageView addGestureRecognizer:tap1]; 

        [tap1 release]; 

    */ 

         

         

    /*   

     *  拖拽手势   

        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)]; 

        [imageView addGestureRecognizer:pan]; 

        [pan release]; 

    */    

      

         

    /* 

     *  捏合手势 

      

        UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)]; 

        [imageView addGestureRecognizer:pinch]; 

        [pinch release]; 

      

    */ 

         

    /* 

     *  旋转手势 

      

         

        UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)]; 

        [imageView addGestureRecognizer:rotation]; 

        [rotation release]; 

    */ 

         

    /* 

     *  轻扫手势 

      

      

        UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)]; 

        //设置轻扫方向 

        swipe.direction = UISwipeGestureRecognizerDirectionDown;  

        [imageView addGestureRecognizer:swipe]; 

        [swipe release]; 

    */ 

         

    /* 

     *  长按手势 

     */ 

      

        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)]; 

        [imageView addGestureRecognizer:longPress]; 

        longPress.minimumPressDuration = 1.0; 

        [longPress release]; 

         

      

    //   轻拍手势响应单击方法 

    -(void) tap:(UITapGestureRecognizer *) tap 

        NSLog(@"是否相应 Tap !"); 

      

    //  轻拍手势响应双击方法 

    -(void) tap1:(UITapGestureRecognizer *) tap 

    //  随机颜色 

        UIColor *randomColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha: 1.0]; 

         

        self.view.backgroundColor = randomColor; 

      

    //  拖拽手势响应事件 

    -(void) pan:(UIPanGestureRecognizer *) pan 

        CGPoint point = [pan translationInView:pan.view]; 

        NSLog(@"point = %@", NSStringFromCGPoint(point)); 

        NSLog(@"center = %@", NSStringFromCGPoint(pan.view.center)); 

        pan.view.transform = CGAffineTransformMakeTranslation(point.x, point.y); 

      

    //  捏合手势响应事件 

    -(void) pinch:(UIPinchGestureRecognizer *)pinch 

        pinch.view.transform = CGAffineTransformMakeScale(pinch.scale, pinch.scale); 

      

    //  旋转响应事件 

    -(void) rotation:(UIRotationGestureRecognizer *) rotation 

    //  旋转是多少弧度 

        rotation.view.transform = CGAffineTransformMakeRotation(rotation.rotation); 

      

    //  轻扫手势响应事件 

    -(void) swipe:(UISwipeGestureRecognizer*) swipe 

        //  随机颜色 

        UIColor *randomColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha: 1.0]; 

         

        self.view.backgroundColor = randomColor; 

      

    //  长按手势响应事件 

    -(void) longPress:(UILongPressGestureRecognizer*) longPress 

        if(longPress.state == UIGestureRecognizerStateEnded) 

        { 

            //  随机颜色 

            UIColor *randomColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha: 1.0]; 

             

            self.view.backgroundColor = randomColor; 

        } 

    展开全文
  • 手势识别在 iOS 中非常重要,他极地提高了移动设备的使用便捷性。 iOS 系统在 3.2 以后,他提供了一些常用的手势(UIGestureRecognizer 的子类),开发者可以直接使用他们进行手势操作。 ...

    1、UIGestureRecognizer 介绍

    手势识别在 iOS 中非常重要,他极大地提高了移动设备的使用便捷性。

    iOS 系统在 3.2 以后,他提供了一些常用的手势(UIGestureRecognizer 的子类),开发者可以直接使用他们进行手势操作。

    • UIPanGestureRecognizer(拖动)

    • UIPinchGestureRecognizer(捏合)

    • UIRotationGestureRecognizer(旋转)

    • UITapGestureRecognizer(点按)

    • UILongPressGestureRecognizer(长按)

    • ​UISwipeGestureRecognizer(轻扫)

    另外,可以通过继承 UIGestureRecognizer 类,实现自定义手势(手势识别器类)。

    PS:自定义手势时,需要 #import <UIKit/UIGestureRecognizerSubclass.h>,一般需实现如下方法:

    1 - (void)reset;
    2 
    3 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
    4 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
    5 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
    6 - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
    7 //以上方法在分类 UIGestureRecognizer (UIGestureRecognizerProtected) 中声明,更多方法声明请自行查看

    UIGestureRecognizer 的继承关系如下:

     

    2、手势状态

    在六种手势识别中,只有一种手势是离散型手势,他就是 UITapGestureRecognizer。

    离散型手势的特点就是:一旦识别就无法取消,而且只会调用一次手势操作事件(初始化手势时指定的回调方法)。

    ​换句话说其他五种手势是连续型手势,而连续型手势的特点就是:会多次调用手势操作事件,而且在连续手势识别后可以取消手势。从下图可以看出两者调用操作事件的次数是不同的:

    手势状态枚举如下:

    1 typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
    2     UIGestureRecognizerStatePossible,   // 尚未识别是何种手势操作(但可能已经触发了触摸事件),默认状态
    3     UIGestureRecognizerStateBegan,      // 手势已经开始,此时已经被识别,但是这个过程中可能发生变化,手势操作尚未完成
    4     UIGestureRecognizerStateChanged,    // 手势状态发生转变
    5     UIGestureRecognizerStateEnded,      // 手势识别操作完成(此时已经松开手指)
    6     UIGestureRecognizerStateCancelled,  // 手势被取消,恢复到默认状态
    7     UIGestureRecognizerStateFailed,     // 手势识别失败,恢复到默认状态
    8     UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // 手势识别完成,同UIGestureRecognizerStateEnded
    9 };
    • 对于离散型手势 UITapGestureRecgnizer 要么被识别,要么失败,点按(假设点按次数设置为1,并且没有添加长按手势)下去一次不松开则此时什么也不会发生,松开手指立即识别并调用操作事件,并且状态为3(已完成)。
    • 但是连续型手势要复杂一些,就拿旋转手势来说,如果两个手指点下去不做任何操作,此时并不能识别手势(因为我们还没旋转)但是其实已经触发了触摸开始事件,此时处于状态0;如果此时旋转会被识别,也就会调用对应的操作事件,同时状态变成1(手势开始),但是状态1只有一瞬间;紧接着状态变为2(因为我们的旋转需要持续一会),并且重复调用操作事件(如果在事件中打印状态会重复打印2);松开手指,此时状态变为3,并调用1次操作事件。

     

    3、使用手势的步骤

    使用手势很简单,分为三步:

    1. 创建手势识别器对象实例。创建时,指定一个回调方法,当手势开始,改变、或结束时,执行回调方法。

    2. 设置手势识别器对象实例的相关属性(可选部分)

    3. 添加到需要识别的 View 中。每个手势只对应一个 View,当屏幕触摸在 View 的边界内时,如果手势和预定的一样,那就会执行回调方法。

    PS:一个手势只能对应一个 View,但是一个 View 可以有多个手势。建议在真机上测试这些手势,模拟器操作不太方便,可能导致认为手势失效的情况。(模拟器测试捏合和旋转手势时,按住 option 键,再用触摸板或鼠标操作)

     

    4、举例说明 

    功能描述:

    附加到两个图片视图 UIImageView 的有『拖动』、『捏合』、『旋转』、『点按』;

    而『轻扫』和『自定义手势 KMGestureRecognizer』附加在根视图 UIView 中。

    1. 拖动:进行当前图片视图位置移动

    2. 捏合:进行当前图片视图缩放

    3. 旋转:进行当前图片视图角度旋转

    4. 点按:双击恢复当前图片视图的缩放、角度旋转、不透明度

    5. 长按:设置当前图片视图的不透明度为0.7

    6. 轻扫:左右轻扫设置两个图片视图为居中,同时以垂直居中的特定偏移量定位

    7. 自定义手势:挠痒功能,左右扫动共3次或以上,设置两个图片视图为居中,同时以水平居中的特定偏移量定位

     

    效果如下:

    KMGestureRecognizer.h

     1 #import <UIKit/UIKit.h>
     2 
     3 typedef NS_ENUM(NSUInteger, Direction) {
     4     DirectionUnknown,
     5     DirectionLeft,
     6     DirectionRight
     7 };
     8 
     9 @interface KMGestureRecognizer : UIGestureRecognizer
    10 @property (assign, nonatomic) NSUInteger tickleCount; //挠痒次数
    11 @property (assign, nonatomic) CGPoint currentTickleStart; //当前挠痒开始坐标位置
    12 @property (assign, nonatomic) Direction lastDirection; //最后一次挠痒方向
    13 
    14 @end
    
    

    KMGestureRecognizer.m


     1 #import "KMGestureRecognizer.h"
     2 #import <UIKit/UIGestureRecognizerSubclass.h>
     3 
     4 @implementation KMGestureRecognizer
     5 #define kMinTickleSpacing 20.0
     6 #define kMaxTickleCount 3
     7 
     8 - (void)reset {
     9     _tickleCount = 0;
    10     _currentTickleStart = CGPointZero;
    11     _lastDirection = DirectionUnknown;
    12     
    13     if (self.state == UIGestureRecognizerStatePossible) {
    14         self.state = UIGestureRecognizerStateFailed;
    15     }
    16 }
    17 
    18 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    19     UITouch *touch = [touches anyObject];
    20     _currentTickleStart = [touch locationInView:self.view]; //设置当前挠痒开始坐标位置
    21 }
    22 
    23 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    24     //『当前挠痒开始坐标位置』和『移动后坐标位置』进行 X 轴值比较,得到是向左还是向右移动
    25     UITouch *touch = [touches anyObject];
    26     CGPoint tickleEnd = [touch locationInView:self.view];
    27     CGFloat tickleSpacing = tickleEnd.x - _currentTickleStart.x;
    28     Direction currentDirection = tickleSpacing < 0 ? DirectionLeft : DirectionRight;
    29     
    30     //移动的 X 轴间距值是否符合要求,足够大
    31     if (ABS(tickleSpacing) >= kMinTickleSpacing) {
    32         //判断是否有三次不同方向的动作,如果有则手势结束,将执行回调方法
    33         if (_lastDirection == DirectionUnknown ||
    34             (_lastDirection == DirectionLeft && currentDirection == DirectionRight) ||
    35             (_lastDirection == DirectionRight && currentDirection == DirectionLeft)) {
    36             _tickleCount++;
    37             _currentTickleStart = tickleEnd;
    38             _lastDirection = currentDirection;
    39             
    40             if (_tickleCount >= kMaxTickleCount && self.state == UIGestureRecognizerStatePossible) {
    41                 self.state = UIGestureRecognizerStateEnded;
    42                 //NSLog(@"自定义手势成功,将执行回调方法");
    43             }
    44         }
    45     }
    46 }
    47 
    48 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    49     [self reset];
    50 }
    51 
    52 - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    53     [self reset];
    54 }
    55 
    56 @end
    
    
    

    ViewController.h

    1 #import <UIKit/UIKit.h>
    2 #import "KMGestureRecognizer.h"
    3 
    4 @interface ViewController : UIViewController
    5 @property (strong, nonatomic) UIImageView *imgV;
    6 @property (strong, nonatomic) UIImageView *imgV2;
    7 @property (strong, nonatomic) KMGestureRecognizer *customGestureRecognizer;
    8 
    9 @end
    
    

    ViewController.m

      1 #import "ViewController.h"
      2 
      3 @interface ViewController ()
      4 - (void)handlePan:(UIPanGestureRecognizer *)recognizer;
      5 - (void)handlePinch:(UIPinchGestureRecognizer *)recognizer;
      6 - (void)handleRotation:(UIRotationGestureRecognizer *)recognizer;
      7 - (void)handleTap:(UITapGestureRecognizer *)recognizer;
      8 - (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer;
      9 - (void)handleSwipe:(UISwipeGestureRecognizer *)recognizer;
     10 - (void)handleCustomGestureRecognizer:(KMGestureRecognizer *)recognizer;
     11 
     12 - (void)bindPan:(UIImageView *)imgVCustom;
     13 - (void)bindPinch:(UIImageView *)imgVCustom;
     14 - (void)bindRotation:(UIImageView *)imgVCustom;
     15 - (void)bindTap:(UIImageView *)imgVCustom;
     16 - (void)bindLongPress:(UIImageView *)imgVCustom;
     17 - (void)bindSwipe;
     18 - (void)bingCustomGestureRecognizer;
     19 - (void)layoutUI;
     20 @end
     21 
     22 @implementation ViewController
     23 
     24 - (void)viewDidLoad {
     25     [super viewDidLoad];
     26     
     27     [self layoutUI];
     28 }
     29 
     30 - (void)didReceiveMemoryWarning {
     31     [super didReceiveMemoryWarning];
     32     // Dispose of any resources that can be recreated.
     33 }
     34 
     35 #pragma mark - 处理手势操作
     36 /**
     37  *  处理拖动手势
     38  *
     39  *  @param recognizer 拖动手势识别器对象实例
     40  */
     41 - (void)handlePan:(UIPanGestureRecognizer *)recognizer {
     42     //视图前置操作
     43     [recognizer.view.superview bringSubviewToFront:recognizer.view];
     44     
     45     CGPoint center = recognizer.view.center;
     46     CGFloat cornerRadius = recognizer.view.frame.size.width / 2;
     47     CGPoint translation = [recognizer translationInView:self.view];
     48     //NSLog(@"%@", NSStringFromCGPoint(translation));
     49     recognizer.view.center = CGPointMake(center.x + translation.x, center.y + translation.y);
     50     [recognizer setTranslation:CGPointZero inView:self.view];
     51     
     52     if (recognizer.state == UIGestureRecognizerStateEnded) {
     53         //计算速度向量的长度,当他小于200时,滑行会很短
     54         CGPoint velocity = [recognizer velocityInView:self.view];
     55         CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
     56         CGFloat slideMult = magnitude / 200;
     57         //NSLog(@"magnitude: %f, slideMult: %f", magnitude, slideMult); //e.g. 397.973175, slideMult: 1.989866
     58         
     59         //基于速度和速度因素计算一个终点
     60         float slideFactor = 0.1 * slideMult;
     61         CGPoint finalPoint = CGPointMake(center.x + (velocity.x * slideFactor),
     62                                          center.y + (velocity.y * slideFactor));
     63         //限制最小[cornerRadius]和最大边界值[self.view.bounds.size.width - cornerRadius],以免拖动出屏幕界限
     64         finalPoint.x = MIN(MAX(finalPoint.x, cornerRadius),
     65                            self.view.bounds.size.width - cornerRadius);
     66         finalPoint.y = MIN(MAX(finalPoint.y, cornerRadius),
     67                            self.view.bounds.size.height - cornerRadius);
     68         
     69         //使用 UIView 动画使 view 滑行到终点
     70         [UIView animateWithDuration:slideFactor*2
     71                               delay:0
     72                             options:UIViewAnimationOptionCurveEaseOut
     73                          animations:^{
     74                              recognizer.view.center = finalPoint;
     75                          }
     76                          completion:nil];
     77     }
     78 }
     79 
     80 /**
     81  *  处理捏合手势
     82  *
     83  *  @param recognizer 捏合手势识别器对象实例
     84  */
     85 - (void)handlePinch:(UIPinchGestureRecognizer *)recognizer {
     86     CGFloat scale = recognizer.scale;
     87     recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, scale, scale); //在已缩放大小基础下进行累加变化;区别于:使用 CGAffineTransformMakeScale 方法就是在原大小基础下进行变化
     88     recognizer.scale = 1.0;
     89 }
     90 
     91 /**
     92  *  处理旋转手势
     93  *
     94  *  @param recognizer 旋转手势识别器对象实例
     95  */
     96 - (void)handleRotation:(UIRotationGestureRecognizer *)recognizer {
     97     recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
     98     recognizer.rotation = 0.0;
     99 }
    100 
    101 /**
    102  *  处理点按手势
    103  *
    104  *  @param recognizer 点按手势识别器对象实例
    105  */
    106 - (void)handleTap:(UITapGestureRecognizer *)recognizer {
    107     UIView *view = recognizer.view;
    108     view.transform = CGAffineTransformMakeScale(1.0, 1.0);
    109     view.transform = CGAffineTransformMakeRotation(0.0);
    110     view.alpha = 1.0;
    111 }
    112 
    113 /**
    114  *  处理长按手势
    115  *
    116  *  @param recognizer 点按手势识别器对象实例
    117  */
    118 - (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer {
    119     //长按的时候,设置不透明度为0.7
    120     recognizer.view.alpha = 0.7;
    121 }
    122 
    123 /**
    124  *  处理轻扫手势
    125  *
    126  *  @param recognizer 轻扫手势识别器对象实例
    127  */
    128 - (void)handleSwipe:(UISwipeGestureRecognizer *)recognizer {
    129     //代码块方式封装操作方法
    130     void (^positionOperation)() = ^() {
    131         CGPoint newPoint = recognizer.view.center;
    132         newPoint.y -= 20.0;
    133         _imgV.center = newPoint;
    134         
    135         newPoint.y += 40.0;
    136         _imgV2.center = newPoint;
    137     };
    138     
    139     //根据轻扫方向,进行不同控制
    140     switch (recognizer.direction) {
    141         case UISwipeGestureRecognizerDirectionRight: {
    142             positionOperation();
    143             break;
    144         }
    145         case UISwipeGestureRecognizerDirectionLeft: {
    146             positionOperation();
    147             break;
    148         }
    149         case UISwipeGestureRecognizerDirectionUp: {
    150             break;
    151         }
    152         case UISwipeGestureRecognizerDirectionDown: {
    153             break;
    154         }
    155     }
    156 }
    157 
    158 /**
    159  *  处理自定义手势
    160  *
    161  *  @param recognizer 自定义手势识别器对象实例
    162  */
    163 - (void)handleCustomGestureRecognizer:(KMGestureRecognizer *)recognizer {
    164     //代码块方式封装操作方法
    165     void (^positionOperation)() = ^() {
    166         CGPoint newPoint = recognizer.view.center;
    167         newPoint.x -= 20.0;
    168         _imgV.center = newPoint;
    169         
    170         newPoint.x += 40.0;
    171         _imgV2.center = newPoint;
    172     };
    173     
    174     positionOperation();
    175 }
    176 
    177 
    178 #pragma mark - 绑定手势操作
    179 /**
    180  *  绑定拖动手势
    181  *
    182  *  @param imgVCustom 绑定到图片视图对象实例
    183  */
    184 - (void)bindPan:(UIImageView *)imgVCustom {
    185     UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self
    186                                                                                  action:@selector(handlePan:)];
    187     [imgVCustom addGestureRecognizer:recognizer];
    188 }
    189 
    190 /**
    191  *  绑定捏合手势
    192  *
    193  *  @param imgVCustom 绑定到图片视图对象实例
    194  */
    195 - (void)bindPinch:(UIImageView *)imgVCustom {
    196     UIPinchGestureRecognizer *recognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self
    197                                                                                      action:@selector(handlePinch:)];
    198     [imgVCustom addGestureRecognizer:recognizer];
    199     //[recognizer requireGestureRecognizerToFail:imgVCustom.gestureRecognizers.firstObject];
    200 }
    201 
    202 /**
    203  *  绑定旋转手势
    204  *
    205  *  @param imgVCustom 绑定到图片视图对象实例
    206  */
    207 - (void)bindRotation:(UIImageView *)imgVCustom {
    208     UIRotationGestureRecognizer *recognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self
    209                                                                                            action:@selector(handleRotation:)];
    210     [imgVCustom addGestureRecognizer:recognizer];
    211 }
    212 
    213 /**
    214  *  绑定点按手势
    215  *
    216  *  @param imgVCustom 绑定到图片视图对象实例
    217  */
    218 - (void)bindTap:(UIImageView *)imgVCustom {
    219     UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
    220                                                                                  action:@selector(handleTap:)];
    221     //使用一根手指双击时,才触发点按手势识别器
    222     recognizer.numberOfTapsRequired = 2;
    223     recognizer.numberOfTouchesRequired = 1;
    224     [imgVCustom addGestureRecognizer:recognizer];
    225 }
    226 
    227 /**
    228  *  绑定长按手势
    229  *
    230  *  @param imgVCustom 绑定到图片视图对象实例
    231  */
    232 - (void)bindLongPress:(UIImageView *)imgVCustom {
    233     UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
    234     recognizer.minimumPressDuration = 0.5; //设置最小长按时间;默认为0.5秒
    235     [imgVCustom addGestureRecognizer:recognizer];
    236 }
    237 
    238 /**
    239  *  绑定轻扫手势;支持四个方向的轻扫,但是不同的方向要分别定义轻扫手势
    240  */
    241 - (void)bindSwipe {
    242     //向右轻扫手势
    243     UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
    244                                                                                      action:@selector(handleSwipe:)];
    245     recognizer.direction = UISwipeGestureRecognizerDirectionRight; //设置轻扫方向;默认是 UISwipeGestureRecognizerDirectionRight,即向右轻扫
    246     [self.view addGestureRecognizer:recognizer];
    247     [recognizer requireGestureRecognizerToFail:_customGestureRecognizer]; //设置以自定义挠痒手势优先识别
    248     
    249     //向左轻扫手势
    250     recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
    251                                                            action:@selector(handleSwipe:)];
    252     recognizer.direction = UISwipeGestureRecognizerDirectionLeft;
    253     [self.view addGestureRecognizer:recognizer];
    254     [recognizer requireGestureRecognizerToFail:_customGestureRecognizer]; //设置以自定义挠痒手势优先识别
    255 }
    256 
    257 /**
    258  *  绑定自定义挠痒手势;判断是否有三次不同方向的动作,如果有则手势结束,将执行回调方法
    259  */
    260 - (void)bingCustomGestureRecognizer {
    261     //当 recognizer.state 为 UIGestureRecognizerStateEnded 时,才执行回调方法 handleCustomGestureRecognizer:
    262     
    263     //_customGestureRecognizer = [KMGestureRecognizer new];
    264     _customGestureRecognizer = [[KMGestureRecognizer alloc] initWithTarget:self
    265                                                                     action:@selector(handleCustomGestureRecognizer:)];
    266     [self.view addGestureRecognizer:_customGestureRecognizer];
    267 }
    268 
    269 - (void)layoutUI {
    270     //图片视图 _imgV
    271     UIImage *img = [UIImage imageNamed:@"Emoticon_tusiji_icon"];
    272     CGFloat cornerRadius = img.size.width;
    273     _imgV = [[UIImageView alloc] initWithImage:img];
    274     _imgV.frame = CGRectMake(20.0, 20.0,
    275                              cornerRadius * 2, cornerRadius * 2);
    276     _imgV.userInteractionEnabled = YES;
    277     _imgV.layer.masksToBounds = YES;
    278     _imgV.layer.cornerRadius = cornerRadius;
    279     _imgV.layer.borderWidth = 2.0;
    280     _imgV.layer.borderColor = [UIColor grayColor].CGColor;
    281     [self.view addSubview:_imgV];
    282     
    283     //图片视图 _imgV2
    284     img = [UIImage imageNamed:@"Emoticon_tusiji_icon2"];
    285     cornerRadius = img.size.width;
    286     _imgV2 = [[UIImageView alloc] initWithImage:img];
    287     _imgV2.frame = CGRectMake(20.0, 40.0 + _imgV.frame.size.height,
    288                               cornerRadius * 2, cornerRadius * 2);
    289     _imgV2.userInteractionEnabled = YES;
    290     _imgV2.layer.masksToBounds = YES;
    291     _imgV2.layer.cornerRadius = cornerRadius;
    292     _imgV2.layer.borderWidth = 2.0;
    293     _imgV2.layer.borderColor = [UIColor orangeColor].CGColor;
    294     [self.view addSubview:_imgV2];
    295     
    296     
    297     [self bindPan:_imgV];
    298     [self bindPinch:_imgV];
    299     [self bindRotation:_imgV];
    300     [self bindTap:_imgV];
    301     [self bindLongPress:_imgV];
    302     
    303     [self bindPan:_imgV2];
    304     [self bindPinch:_imgV2];
    305     [self bindRotation:_imgV2];
    306     [self bindTap:_imgV2];
    307     [self bindLongPress:_imgV2];
    308     
    309     //为了处理手势识别优先级的问题,这里需先绑定自定义挠痒手势
    310     [self bingCustomGestureRecognizer];
    311     [self bindSwipe];
    312 }
    313 
    314 @end

    展开全文
  • iOS开发之UI篇(5)—— 添加手势

    千次阅读 2017-11-23 21:42:56
    种常用手势: UITapGestureRecognizer:点击手势 UIPinchGestureRecognizer:捏合手势(放大缩小) UIRotationGestureRecognizer:旋转手势 UISwipeGestureRecognizer:滑动手势 UIPanGestureRecognizer:拖动手势...

    版本

    Xcode 9.1

    1. 六种常用手势:

    • UITapGestureRecognizer:点击手势
    • UIPinchGestureRecognizer:捏合手势(放大缩小)
    • UIRotationGestureRecognizer:旋转手势
    • UISwipeGestureRecognizer:滑动手势
    • UIPanGestureRecognizer:拖动手势
    • UILongPressGestureRecognizer:长按手势

    2. 解决手势冲突

    手势识别实际上是调用触摸事件来实现的。如果一个手势A的识别部分是另一个手势B的子部分时,默认情况下A就会先识别,B就无法识别了,造成手势冲突。例如拖动手势(UIPanGestureRecognizer)的操作事件是在手势的开始状态(UIGestureRecognizerStateBegan)开始执行的,而滑动手势(UISwipeGestureRecognizer)的操作事件只有在手势结束状态(UIGestureRecognizerStateEnded)才能执行,因此能识别拖动手势而不能识别滑动手势。
    解决手势冲突办法:

    使用requireGestureRecognizerToFail:方法,指定某个手势执行的前提是另一个手势失败后才会执行。

    例如:

        /* 解决手势冲突 */
        // 解决 单击 与 双击 之间的冲突
        [singleTap requireGestureRecognizerToFail:doubleTap];
        // 解决 拖动 与 滑动 之间的冲突
        [pan requireGestureRecognizerToFail:swipeToLeft];
        [pan requireGestureRecognizerToFail:swipeToRight];
        // 解决 拖动 和 长按 之间的冲突
        [longPress requireGestureRecognizerToFail:pan];

    3. 手势在视图控件之间的传递(响应链)

    和触摸事件一样,默认情况下,子视图(上层视图)触摸事件执行后就不再向父视图(下层视图)传递。如果想继续往下传递手势,可利用代理方法gestureRecognizer: shouldRecognizeSimultaneouslyWithGestureRecognizer:来实现。此代理方法默认返回NO,会阻断继续向下识别手势,如果返回YES则可以继续向下传递手势。

    示例
    注意UIImageView默认userInteractionEnabled为NO,这就无法识别手势。本例使用storyboard创建UIImageView并设置userInteractionEnabled为YES。

    GestureVC.m

    #import "GestureVC.h"
    
    @interface GestureVC () <UIGestureRecognizerDelegate> {
    
        NSInteger _currentIndex;
    }
    
    @property (weak, nonatomic) IBOutlet UILabel *label;
    @property (weak, nonatomic) IBOutlet UIImageView *imageView;
    
    @end
    
    @implementation GestureVC
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        /* ----- 点击手势 ----- */
        // 默认单击
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGesture:)];
        [self.imageView addGestureRecognizer:singleTap];
        // 双击
        UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapGesture:)];
        doubleTap.numberOfTapsRequired = 2;
        // 需要2根手指一起敲击
    //    tap.numberOfTouchesRequired = 2;
        // 把点击手势添加到imageView上
        [self.imageView addGestureRecognizer:doubleTap];
    
        /* ----- 捏合手势 ----- */
        // 模拟器触发:按住option键,再点鼠标左键
        UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];
        [self.imageView addGestureRecognizer:pinch];
    
        /* ----- 旋转手势 ----- */
        // 模拟器触发:也是按住option键
        UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];
        [self.imageView addGestureRecognizer:rotation];
    
        /* ----- 滑动手势 ----- */
        // 默认往右滑
        UISwipeGestureRecognizer *swipeToRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeToRightGesture:)];
        [self.imageView addGestureRecognizer:swipeToRight];
        // 改成往左滑
        UISwipeGestureRecognizer *swipeToLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeToLeftGesture:)];
        swipeToLeft.direction = UISwipeGestureRecognizerDirectionLeft;
        [self.imageView addGestureRecognizer:swipeToLeft];
        // 支持两个方向(左+右/上+下),不同时支持横向和竖向,优先支持横向
    //    swipe.direction = UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight;
    
        /* ----- 拖动手势 ----- */
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
        [self.imageView addGestureRecognizer:pan];
    
        /* ----- 长按手势 ----- */
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];
        // 设置长按时间
        longPress.minimumPressDuration = 1.0f;
        [self.imageView addGestureRecognizer:longPress];
    
        /* 解决手势冲突 */
        // 解决 单击 与 双击 之间的冲突
        [singleTap requireGestureRecognizerToFail:doubleTap];
        // 解决 拖动 与 滑动 之间的冲突
        [pan requireGestureRecognizerToFail:swipeToLeft];
        [pan requireGestureRecognizerToFail:swipeToRight];
        // 解决 拖动 和 长按 之间的冲突
        [longPress requireGestureRecognizerToFail:pan];
    }
    
    
    #pragma mark - 手势识别响应方法
    
    // 单击手势
    - (void)singleTapGesture:(UITapGestureRecognizer *)gesture {
    
        NSLog(@"singleTapGesture");
    
        self.label.hidden = !self.label.hidden;
    }
    
    
    // 双击手势
    - (void)doubleTapGesture:(UITapGestureRecognizer *)gesture {
    
        NSLog(@"doubleTapGesture");
    
        // 取消一切形变
        self.imageView.transform = CGAffineTransformIdentity;
    }
    
    
    // 捏合手势
    - (void)pinchGesture:(UIPinchGestureRecognizer *)gesture {
    
        NSLog(@"pinchGesture");
    
        if (gesture.state == UIGestureRecognizerStateChanged) {         // 手势进行中
            // 捏合手势中scale属性记录了缩放比例
            self.imageView.transform = CGAffineTransformMakeScale(gesture.scale, gesture.scale);
        }else if(gesture.state == UIGestureRecognizerStateEnded) {      // 手势结束
        }
    }
    
    
    // 旋转手势
    - (void)rotationGesture:(UIRotationGestureRecognizer *)gesture {
    
        NSLog(@"rotationGesture");
    
        if (gesture.state == UIGestureRecognizerStateChanged) {         // 手势进行中
            // 旋转手势中rotation属性记录了旋转弧度
            self.imageView.transform = CGAffineTransformMakeRotation(gesture.rotation);
        }else if(gesture.state == UIGestureRecognizerStateEnded) {      // 手势结束
        }
    }
    
    
    // 向左滑动手势
    - (void)swipeToLeftGesture:(UISwipeGestureRecognizer *)gesture {
    
        NSLog(@"swipeToLeftGesture");
    
        // 上一张图片
        _currentIndex--;
        if (_currentIndex < 0) {
            _currentIndex = 2;
        }else if (_currentIndex >2) {
            _currentIndex = 0;
        }
        self.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld.jpg",_currentIndex]];
    }
    
    
    // 向右滑动手势
    - (void)swipeToRightGesture:(UISwipeGestureRecognizer *)gesture {
    
        NSLog(@"swipeToRightGesture");
    
        // 下一张图片
        _currentIndex++;
        if (_currentIndex < 0) {
            _currentIndex = 2;
        }else if (_currentIndex >2) {
            _currentIndex = 0;
        }
        self.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld.jpg",_currentIndex]];
    }
    
    
    // 拖动手势
    - (void)panGesture:(UIPanGestureRecognizer *)gesture {
    
        NSLog(@"panGesture");
    
        if (gesture.state == UIGestureRecognizerStateChanged) {     // 手势进行中
            // 取得在相对根视图(self.view)的移动
            CGPoint translation = [gesture translationInView:self.view];
            // 改变imageView坐标
            self.imageView.transform = CGAffineTransformMakeTranslation(translation.x, translation.y);
        }else if(gesture.state == UIGestureRecognizerStateEnded){
            // 0.5s后取消一切形变
            [UIView animateWithDuration:0.5 animations:^{
                self.imageView.transform = CGAffineTransformIdentity;
            }];
        }
    }
    
    
    // 长按手势
    - (void)longPressGesture:(UILongPressGestureRecognizer *)gesture {
    
        NSLog(@"longPressGesture");
    
        // 此方法会调用两次(Began & Ended),需判断其手势状态
        if (gesture.state == UIGestureRecognizerStateBegan) {
    
            // 创建alertController
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定删除这张图片吗?" preferredStyle:UIAlertControllerStyleActionSheet];
            // 添加确认按钮
            [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
                // 这里啥也不做
            }]];
            // 添加取消按钮
            [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                // 这里啥也不做
            }]];
            // 显示alertController
            [self presentViewController:alertController animated:YES completion:nil];
        }
    }
    
    
    @end


    效果图
    效果图

    展开全文
  • 手势iOS开发中是一个比较常用的功能,不过相对来说大家用的比较少,经常刷网易新闻,上次用了一下捏合手势才发现可以调整字体大小。昨天看到一个介绍摇一摇这个功能的,没看到之前一直都觉得摇一摇是微信的专有的...
  • IOS 七种手势详解(动图+Demo下载)

    千次阅读 2015-03-04 20:55:30
    原创Blog,转载请注明出处 blog.csdn.net/hello_hwc ...本文介绍了7种常用手势,多数手势我都配合Core Animation举了一个例子。给读者一些参考。最后,Demo的链接我会放到最后。Demo源代码下载 CSDN下
  • 关于手势响应IOS中封装了一个类,能响应一般手势UIGestureRecognizer 下面说一下这个UIGestureRecognizer 类 这个类有几个子类,这几个子类分别表示几种不同的基本手势 1、UITapGestureRecognizer //点击手势识别...
  • 触控事件(单点、多点触控以及各种手势操作) 传感器事件(重力、加速度传感器等) 远程控制事件(远程遥控iOS设备多媒体播放等) 这三类事件共同构成了iOS设备丰富的操作方式和使用体验,本次就首先来针对第一...
  • 点击手势 //创建点击手势 UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(TapMe)]; ... tap.numberOfTapsRequired=3; //创建图片控件 UIImageView *
  • ios 七种手势详解

    千次阅读 2017-02-17 13:33:22
    //ios 七种手势简单总结 {  //创建imageView  UIImage *image = [UIImage imageNamed:@"01.jpeg"];  UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 200, 200)];  ...
  • 手势识别在iOS上非常重要,手势操作移动设备的重要特征,极的增加了移动设备使用便捷性。 iOS系统在3.2以后,为方便开发这使用一些常用的手势,提供了UIGestureRecognizer类。手势识别UIGestureRecognizer类是...
  • iOS开发之手势识别

    2017-04-24 16:41:12
    感觉有必要把iOS开发中的手势识别做一个小小的总结。在上一篇iOS开发之自定义表情键盘(组件封装与自动布局)博客中用到了一个轻击手势,就是在轻击TextView时从表情键盘回到系统键盘,在TextView中的手是用storyboard...
  • 手势识别在iOS上非常重要,手势操作移动设备的重要特征,极的增加了移动设备使用便捷性。 iOS系统在3.2以后,为方便开发这使用一些常用的手势,提供了UIGestureRecognizer类。手势识别UIGestureRecognizer类是个...
  • iOS手势

    2015-04-30 19:49:54
    手势识别在iOS上非常重要,手势操作是移动设备的重要特征,极的增加了移动设备使用便捷性。
  • iOS 手势添加(代码和storyboard)

    千次阅读 2016-02-02 16:41:10
    IOS手势一共种: 轻击手势(TapGestureRecognizer) 轻扫手势(SwipeGestureRecognizer) 长按手势(LongPressGestureRecognizer) 拖动手势(PanGestureRecognizer) 捏合手势(PinchGestureRecognizer) 旋转...
  • iOS 手势及触摸

    2013-12-19 09:22:49
    iOS 手势及触摸
  • 使用手势很简单,分为两步: 创建手势实例。当创建手势时,指定一个回调方法,当手势开始,改变、或结束时,回调方法被调用。添加到需要识别的View中。每个手势只对应一个View,当屏幕触摸在View的边界内时,...
  • iOS中常用的手势

    2015-01-25 16:22:52
    iOS中常用的手势分别为点击,长按,清扫,捏合,旋转,拖拽,手势类继承于UIController父类,所以有触发事件.所有控件都可以添加手势,但是UIImageView和UILable的用户交互是默认关闭的,只有把他们的...
1 2 3 4 5 ... 20
收藏数 3,443
精华内容 1,377
关键字:

7的六大手势操作功能 ios