美文网首页
ios单击手势和双击手势共存问题

ios单击手势和双击手势共存问题

作者: 少年你这是喜脉啊 | 来源:发表于2017-03-17 10:27 被阅读0次

    - (void)viewDidLoad

    {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 200, 110, 200)];

    imageView.contentMode = UIViewContentModeScaleAspectFit;

    [imageView setImage:[UIImage imageNamed:@"xxx.bundle/1.jpg"]];

    imageView.userInteractionEnabled = YES;

    [self.view addSubview:imageView];

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

    [singleTapGestureRecognizer setNumberOfTapsRequired:1];

    [imageView addGestureRecognizer:singleTapGestureRecognizer];

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

    [doubleTapGestureRecognizer setNumberOfTapsRequired:2];

    [imageView addGestureRecognizer:doubleTapGestureRecognizer];

    //这行很关键,意思是只有当没有检测到doubleTapGestureRecognizer 或者 检测doubleTapGestureRecognizer失败,singleTapGestureRecognizer才有效

    [singleTapGestureRecognizer requireGestureRecognizerToFail:doubleTapGestureRecognizer];

    }

    - (void)singleTap:(UIGestureRecognizer*)gestureRecognizer

    {

    NSLog(@"-----singleTap-----");

    [self.view setBackgroundColor:[UIColor redColor]];

    }

    - (void)doubleTap:(UIGestureRecognizer*)gestureRecognizer

    {

    [self.view setBackgroundColor:[UIColor blueColor]];

    NSLog(@"-----doubleTap-----");

    }

    相关文章

      网友评论

          本文标题:ios单击手势和双击手势共存问题

          本文链接:https://www.haomeiwen.com/subject/whqlnttx.html