【问题标题】:iOS gesture recognizer only starts after user touches viewiOS 手势识别器仅在用户触摸视图后启动
【发布时间】:2014-02-15 02:59:58
【问题描述】:

我有一个 UIPinchGestureRecognizer,我已将其添加到自定义 UIView 中,该 UIView 随后被添加为我的主视图的子视图。出于某种原因,当用户第一次尝试捏合时,不会检测到捏合手势,但会在以后的任何尝试中检测到。请参阅下面的代码,用户必须在打印出 nslog 之前至少触摸一次视图,然后用户再捏。

声明:

        self.pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(expandAction:)];
        self.pinchGesture.scale = 0.0;
        self.pinchGesture.delegate = self;
        [self addGestureRecognizer:self.pinchGesture];

行动:

-(void)expandAction:(UIPinchGestureRecognizer *)sender {
    NSLog(@"pinch detected");
    //stop reminding user
    [self stopTimer:self.pinchExpandTimer];
    [UIView animateWithDuration: 0.0
                          delay: 0.0
                        options: UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionBeginFromCurrentState
                     animations: ^{
                         [self.pinchCircle1 removeFromSuperview];
                         [self.pinchCircle2 removeFromSuperview];
                     }
                     completion: ^(BOOL finished){
                     }];
    //calculate progress
    CGFloat progress = MIN(sender.scale/5, 1.0);
    [self setPinchAnimationWithProgress:progress];
 }

代表:

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark UIGestureRecognizer Delegate

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    return YES;
}

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

基本上,感觉就像用户必须“选择”视图然后做手势才能被拾取。我尝试在视图上启用用户交互和/或设置第一响应者,但它仍然不起作用。如果有任何效果,则在此视图中使用其他手势识别器,但我认为它不应该。有人有什么想法吗?

【问题讨论】:

  • 您使用UIView animateWithDuration 块而没有实际设置任何动画是否有原因(durationdelay 都是0)?
  • 它会取消任何在新动画影响的元素上正在进行的动画。在这种情况下,如果检测到捏合手势,我想删除正在动画的项目。

标签: ios iphone objective-c cocoa-touch uigesturerecognizer


【解决方案1】:

已修复。问题是我在初始化后立即在我的 UIPinchGestureRecognizer 上设置了 scale 属性。这可能是 Apple 代码中的错误。

【讨论】:

    猜你喜欢
    • 2018-09-14
    • 1970-01-01
    • 1970-01-01
    • 2018-08-19
    • 1970-01-01
    • 2017-04-23
    • 2018-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多