使用UIView animateWithDuration进行动画处理时,无法触摸UIButton

问题描述 投票:41回答:9

我有以下代码:

[UIView animateWithDuration:0.3
                      delay:0.0
                    options:UIViewAnimationCurveEaseOut | UIViewAnimationOptionAllowUserInteraction
                 animations:^{
                     CGRect r = [btn frame];
                     r.origin.y -= 40;
                     [btn setFrame: r];
                 }
                 completion:^(BOOL done){
                     if(done){
                         [UIView animateWithDuration:0.3
                                               delay:1
                                             options:UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAllowUserInteraction
                                          animations:^{
                                              CGRect r = [btn frame];
                                              r.origin.y += 40;
                                              [btn setFrame: r];
                                          }
                                          completion:^(BOOL done){if(done) zombiePopping = 0; }];
                     }

                 }];

问题是,即使我使用的是UIViewAnimationOptionAllowInteraction,按钮也不会在动画时响应触摸,这对我来说有点奇怪。

也许最好用Core Animation来完成这项工作?如果是的话,我该怎么做呢?

iphone objective-c ios core-animation uiviewanimation
9个回答
52
投票

按钮的可触摸部分在动画时不会与按钮的可见帧重合。

在内部,按钮的框架将设置为动画的最终值。你会发现你可以点击这个区域,它会起作用。

要点击一个移动按钮,你需要对按钮的.layer.presentationLayer属性进行命中测试(需要导入QuartzCore框架才能执行此操作)。这通常在视图控制器中的触摸处理方法中完成。

如果您需要更多,我很乐意扩展这个答案。

以下是如何通过命中测试表示层来响应触摸事件。此代码位于管理按钮的视图控制器子类中。当我这样做时,我对初始触摸而不是触摸结束感兴趣(这通常是当水龙头会注册时),但原理是相同的。

记得导入QuartzCore框架并将其添加到项目中。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];
    for (UIButton *button in self.buttonsOutletCollection)
    {
        if ([button.layer.presentationLayer hitTest:touchLocation])
        {
            // This button was hit whilst moving - do something with it here
            break;
        }
    }
}

50
投票

在我的情况下,我设置superView.alpha = 0,它停止按钮的交互,虽然我设置UIViewAnimationOptionAllowUserInteraction

解?简单,只需将alpha减少到0.1而不是0

[UIView animateWithDuration:durationTime delay:0 options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionOverrideInheritedOptions | UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse animations: ^{
            superView.alpha = 0.1; // 0.0 will make the button unavailable to touch
        } completion:nil];

26
投票

选项的顺序是重要的,您必须先放置UIViewAnimationOptionAllowUserInteraction,然后添加其他选项。

在Swift 3中使用:.allowUserInteraction


1
投票

我用NSTimer实现了这个:首先,执行开始动画,然后你应该启动一个计时器,这是演示代码:

-(void)fun···
{
    [UIView animateWithDuration:0.3
                      delay:0.0
                    options:UIViewAnimationCurveEaseOut |  UIViewAnimationOptionAllowUserInteraction
                 animations:^{
                     CGRect r = [btn frame];
                     r.origin.y -= 40;
                     [btn setFrame: r];
                 }
                 completion:^(BOOL done){}];

    then , you should start an timer,example:
    //start timer
   if (timer != nil)
   {
       [timer invalidate];
       timer = nil;
   }
   [self performSelector:@selector(closeButton) withObject:nil afterDelay:0];
}


- (void)closeButton
{
    if (timer != nil)
    {
        return;
    }

    //start timer
    timer = [NSTimer scheduledTimerWithTimeInterval:1.5f target:self selector:@selector(timerEvent) userInfo:nil repeats:NO];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

- (void)timerEvent
{
    [UIView animateWithDuration:0.3
                                               delay:1
                                             options:UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAllowUserInteraction
                                          animations:^{
                                              CGRect r = [btn frame];
                                              r.origin.y += 40;
                                              [btn setFrame: r];
                                          }
                                          completion:^(BOOL done){if(done) zombiePopping = 0; }];
    [timer invalidate];
    timer = nil;
}

1
投票

对于懒惰,Swift(2.0)中的答案相同。如有必要,请更换环和出口集合:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    let touch = touches.first
    let touchLocation = touch!.locationInView(self.view)
    if self.button.layer.presentationLayer()!.hitTest(touchLocation) != nil {
        action() // Your action, preferably the original button action.
    }
}

1
投票

Swift 3.0在viewdidload中创建一个superview

let superView = UIView(frame: view.frame)
    superView.isUserInteractionEnabled = true
    superView.backgroundColor = UIColor.clear
    superView.alpha = 0.1
    view.addSubview(superView)

现在像这样实施touchesbagan

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    guard let touch = (touches as NSSet).anyObject() as? UITouch else {
        return
    }

    let touchLocation = touch.location(in: self.view)
    if btnone.layer.presentation()?.hitTest(touchLocation) != nil {
        print("1") // Do stuff for button 
    }
    if btntwo.layer.presentation()?.hitTest(touchLocation) != nil {
        print("2") // Do stuff
    }
    if btnthree.layer.presentation()?.hitTest(touchLocation) != nil {
        print(3) // Do stuff
    }
    if btnfour.layer.presentation()?.hitTest(touchLocation) != nil {
        print(4) // Do stuff
    }
}

0
投票

Swift版本:'999'是用于标识目标视图的标记值。

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let touch = touches.first
        let touchLocation = (touch?.locationInView(self.rootView))!

        for view in self.view.subviews {
            if let layer = view.layer.presentationLayer()?.hitTest(touchLocation) where view.tag == 999 {
                // Here view is the tapped view
                print(view)
            }
        }
    }

0
投票

奇迹般有效

override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? { if bounds.contains(point) && !hidden && userInteractionEnabled && enabled { return self } return super.hitTest(point, withEvent: event) }

虽然this答案也是正确的


0
投票

如果您有UIButton的子类,最简单的方法是覆盖hitTest之类的

public override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        return self.layer.presentation()?.hitTest(self.convert(point, to: superview)).flatMap { _ in return self } ?? nil
    }
© www.soinside.com 2019 - 2024. All rights reserved.