当前位置:编程学习 > wap >>

UIView关联多个Gesture

如果一个UIView关联多个UIGestureRecognizer, 会发生一个奇怪的问题,如下面代码


[cpp]
    UIPanGestureRecognizer *pang = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panned:)]; 
    [self.view addGestureRecognizer:pang]; 
     
    UISwipeGestureRecognizer *swip = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swip:)]; 
    [self.view addGestureRecognizer:swip]; 
- (void)swip:(UISwipeGestureRecognizer *)gesture { 
    NSLog(@"swip"); 

 
- (void)panned:(UIPanGestureRecognizer *)gesture { 
    NSLog(@"pan"); 

    UIPanGestureRecognizer *pang = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panned:)];
    [self.view addGestureRecognizer:pang];
   
    UISwipeGestureRecognizer *swip = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swip:)];
    [self.view addGestureRecognizer:swip];
- (void)swip:(UISwipeGestureRecognizer *)gesture {
    NSLog(@"swip");
}

- (void)panned:(UIPanGestureRecognizer *)gesture {
    NSLog(@"pan");
}
结果是看不到swip的手势触发。

 


原因是系统event传递是,当有一个相响了,event就不会传递下去了。

要想两个gesturerecognizer都起作用,只需要加几行代码就可以了


[cpp]
swip.delegate = self; 

swip.delegate = self;
然后实现,返回YES,表示还要响应otherGestureRecognizer.


[cpp] 
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { 
    return YES; 

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}
这下就可以看到swip在consol中打印出来。

 

 

 

 

补充:移动开发 , IOS ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,