zoukankan      html  css  js  c++  java
  • ios 手势识别

      1 //
      2 //  ViewController.m
      3 //  testGestures
      4 //
      5 //  Created by shawn li on 13-7-29.
      6 //  Copyright (c) 2013年 bigbellshawn. All rights reserved.
      7 //
      8 
      9 #import "ViewController.h"
     10 
     11 @interface ViewController ()
     12 @property (nonatomic, strong) UIImageView *imageView;
     13 @end
     14 
     15 @implementation ViewController
     16 
     17 @synthesize imageView = _imageView;
     18 
     19 - (void)viewDidLoad
     20 {
     21     [super viewDidLoad];
     22     /* step1: 声明和初始化一个手势识别器 */
     23     UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
     24     UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
     25     UITapGestureRecognizer *twoFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTwoFingerTap:)];
     26     // 坑:selector不小心忘了写冒号,导致找不到handleSwipe */
     27     UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
     28     UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
     29     UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotation:)];
     30     UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
     31     UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
     32     UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
     33     
     34     /* step2:对手势识别器进行属性设定 */
     35     [doubleTap setNumberOfTapsRequired:2];
     36     // 坑:twoFingerTap在模拟器上不灵敏,有时候会识别成singleTap
     37     [twoFingerTap setNumberOfTouchesRequired:2];
     38 
     39     [swipeLeft setNumberOfTouchesRequired:1];
     40     [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
     41     [swipeRight setNumberOfTouchesRequired:1];
     42     [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
     43     
     44     // 坑:此部分代码无用 注:left、right、up、down都是独立的手势,没法一个swipe全代替,如果只有一个swipe默认向右划
     45     /*
     46     UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
     47     [swipe setNumberOfTouchesRequired:1];
     48     [swipe setDirection:UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown];
     49      
     50     [self.view addGestureRecognizer:swipe];
     51      */
     52     
     53     
     54     /* step3:把手势识别器加到view中去 */
     55     [self.view addGestureRecognizer:singleTap];
     56     [self.view addGestureRecognizer:doubleTap];
     57     [self.view addGestureRecognizer:twoFingerTap];
     58     [self.view addGestureRecognizer:swipeLeft];
     59     [self.view addGestureRecognizer:swipeRight];
     60     [self.view addGestureRecognizer:rotation];
     61     [self.view addGestureRecognizer:pan];
     62     [self.view addGestureRecognizer:pinch];
     63     [self.view addGestureRecognizer:longPress];
     64     
     65     /* step4:出现冲突时,要设定优先识别顺序,目前只是doubleTap、swipe */
     66     [singleTap requireGestureRecognizerToFail:doubleTap];
     67     [singleTap requireGestureRecognizerToFail:twoFingerTap];
     68     [pan requireGestureRecognizerToFail:swipeRight];
     69     [pan requireGestureRecognizerToFail:swipeLeft];
     70     
     71     
     72     _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 120, 100, 40)];
     73     [self.view addSubview:_imageView];
     74     
     75 }
     76 
     77 - (void)didReceiveMemoryWarning
     78 {
     79     [super didReceiveMemoryWarning];
     80     // Dispose of any resources that can be recreated.
     81 }
     82 
     83 
     84 #pragma mark handle gestures methods
     85 
     86 /* step5:去实现处理扑捉到手势之后的动作 */
     87 
     88 /* 识别单击 */
     89 - (void)handleSingleTap:(UITapGestureRecognizer *)gestureRecognizer {
     90     CGPoint location = [gestureRecognizer locationInView:self.view];
     91     [self drawImageForGestureRecognizer:gestureRecognizer atPoint:location underAdditionalSituation:@"singleTap"];
     92     
     93     [UIView animateWithDuration:0.5 animations:^{
     94         self.imageView.alpha = 0.0;
     95     }];
     96 }
     97 
     98 /* 识别双击 */
     99 - (void)handleDoubleTap:(UITapGestureRecognizer *)gestureRecognizer {
    100     CGPoint location = [gestureRecognizer locationInView:self.view];
    101     [self drawImageForGestureRecognizer:gestureRecognizer atPoint:location underAdditionalSituation:@"doubleTap"];
    102     
    103     [UIView animateWithDuration:0.5 animations:^{
    104         self.imageView.alpha = 0.0;
    105     }];
    106 }
    107 
    108 /* 识别两个手指击 */
    109 - (void)handleTwoFingerTap:(UITapGestureRecognizer *)gestureRecognizer {
    110     CGPoint location = [gestureRecognizer locationInView:self.view];
    111     [self drawImageForGestureRecognizer:gestureRecognizer atPoint:location underAdditionalSituation:@"twoFingerTap"];
    112     
    113     [UIView animateWithDuration:0.5 animations:^{
    114         self.imageView.alpha = 0.0;
    115     }];
    116 }
    117 
    118 /* 识别侧滑 */
    119 - (void)handleSwipe:(UISwipeGestureRecognizer *)gestureRecognizer {
    120     CGPoint location = [gestureRecognizer locationInView:self.view];
    121     [self drawImageForGestureRecognizer:gestureRecognizer atPoint:location underAdditionalSituation:nil];
    122     
    123     if (gestureRecognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
    124         location.x -= 220.0;
    125     }
    126     else if (gestureRecognizer.direction == UISwipeGestureRecognizerDirectionUp) {
    127         location.x -= 220.0;
    128     }
    129     else if (gestureRecognizer.direction == UISwipeGestureRecognizerDirectionDown) {
    130         location.x -= 220.0;
    131     }
    132     else{
    133         location.x += 220.0;
    134     }
    135     
    136     [UIView animateWithDuration:0.5 animations:^{
    137         self.imageView.alpha = 0.0;
    138         self.imageView.center = location;
    139     }];
    140 }
    141 
    142 /* 识别翻转 */
    143 - (void)handleRotation:(UIRotationGestureRecognizer *)gestureRecognizer {
    144     
    145     CGPoint location = [gestureRecognizer locationInView:self.view];
    146     
    147     CGAffineTransform transform = CGAffineTransformMakeRotation([gestureRecognizer rotation]);
    148     self.imageView.transform = transform;
    149     [self drawImageForGestureRecognizer:gestureRecognizer atPoint:location underAdditionalSituation:nil];
    150     
    151     if (([gestureRecognizer state] == UIGestureRecognizerStateEnded) || ([gestureRecognizer state] == UIGestureRecognizerStateCancelled)) {
    152         [UIView animateWithDuration:0.5 animations:^{
    153             self.imageView.alpha = 0.0;
    154             self.imageView.transform = CGAffineTransformIdentity;
    155         }];
    156     }
    157 
    158 }
    159 
    160 /* 识别拖动 */
    161 - (void)handlePan:(UIPanGestureRecognizer *)gestureRecognizer {
    162     CGPoint location = [gestureRecognizer locationInView:self.view];
    163     [self drawImageForGestureRecognizer:gestureRecognizer atPoint:location underAdditionalSituation:nil];
    164     //gestureRecognizer.view.center = CGPointMake(gestureRecognizer.view.center.x + location.x, gestureRecognizer.view.center.y + location.y);
    165     [gestureRecognizer setTranslation:location inView:self.view];
    166 }
    167 
    168 /* 识别放大缩小 */
    169 - (void)handlePinch:(UIPinchGestureRecognizer *)gestureRecognizer {
    170     CGPoint location = [gestureRecognizer locationInView:self.view];
    171     [self drawImageForGestureRecognizer:gestureRecognizer atPoint:location underAdditionalSituation:nil];
    172     gestureRecognizer.view.transform = CGAffineTransformScale(gestureRecognizer.view.transform, gestureRecognizer.scale, gestureRecognizer.scale);  
    173     gestureRecognizer.scale = 1;
    174 }
    175 
    176 /* 识别长按 */
    177 - (void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer {
    178     CGPoint location = [gestureRecognizer locationInView:self.view];
    179     [self drawImageForGestureRecognizer:gestureRecognizer atPoint:location underAdditionalSituation:nil];
    180     
    181 }
    182 #pragma mark -
    183 #pragma mark Drawing the image view
    184 
    185 /*
    186  Set the appropriate image for the image view for the given gesture recognizer, move the image view to the given point, then dispay the image view by setting its alpha to 1.0.
    187  */
    188 - (void)drawImageForGestureRecognizer:(UIGestureRecognizer *)recognizer
    189 atPoint:(CGPoint)centerPoint underAdditionalSituation:(NSString *)addtionalSituation{
    190     
    191     NSString *imageName;
    192     
    193     if ([recognizer isMemberOfClass:[UITapGestureRecognizer class]]) {
    194         if ([addtionalSituation isEqualToString:@"singleTap"]) {
    195             imageName = @"tap.gif";
    196         }
    197         else if ([addtionalSituation isEqualToString:@"doubleTap"]){
    198             imageName = @"tap2.gif";
    199         }
    200         else if ([addtionalSituation isEqualToString:@"twoFingerTap"]){
    201             imageName = @"twoFingerTap.gif";
    202         }
    203         else{
    204             imageName = @"error.png";
    205         }
    206     }
    207     else if ([recognizer isMemberOfClass:[UIRotationGestureRecognizer class]]) {
    208         imageName = @"rotation.gif";
    209     }
    210     else if ([recognizer isMemberOfClass:[UISwipeGestureRecognizer class]]) {
    211         imageName = @"swipe.gif";
    212     }
    213     else if ([recognizer isMemberOfClass:[UIPinchGestureRecognizer class]]) {
    214         imageName = @"pinch.gif";
    215     }
    216     else if ([recognizer isMemberOfClass:[UIPanGestureRecognizer class]]) {
    217         imageName = @"pan.gif";
    218     }
    219     else if ([recognizer isMemberOfClass:[UILongPressGestureRecognizer class]]) {
    220         imageName = @"longPress.gif";
    221     }
    222     else {
    223         imageName = @"error.png";
    224     }
    225     
    226     self.imageView.image = [UIImage imageNamed:imageName];
    227     self.imageView.center = centerPoint;
    228     self.imageView.alpha = 1.0;
    229 }
    230 @end
  • 相关阅读:
    函数类型和函数对象
    _strdup和strdup还有string.h和string事
    VS包含头文件的路径设置
    (转)C++学习建议
    2.2Python数据处理篇之---math模块的数学函数
    2.1Python数据处理篇之---内建有关数学的函数
    19LaTeX学习系列之---LaTeX的总结
    18LaTeX学习系列之---LaTeX的参考文献
    17LaTeX学习系列之---LaTeX的版面设计
    16LaTeX学习系列之---LaTeX数学公式的补充
  • 原文地址:https://www.cnblogs.com/lihaibo-Leao/p/3270709.html
Copyright © 2011-2022 走看看