【问题标题】:When animating UIButton can't be touched动画 UIButton 时无法触摸
【发布时间】:2014-12-18 14:23:39
【问题描述】:

我查看了UIButton can't be touched while animated with UIView animateWithDuration,但答案对我没有帮助。

for (UIButton *button in self.buttonsOutletCollection)

这部分是令人困惑的部分。我的按钮不是 IBOutletCollection 的一部分,所以这不起作用。我用它来移动按钮:

- (void) squareOneMover {
    CGRect oldFrame = _squareOne.frame;
    [UIView animateWithDuration:2
                          delay:0.0
                        options:UIViewAnimationOptionAllowUserInteraction
                     animations:^{
                         CGRect newFrame = CGRectMake(oldFrame.origin.x , oldFrame.origin.y + 500, oldFrame.size.width, oldFrame.size.height);
                         _squareOne.frame = newFrame;
                     }
                     completion:nil
    ];
}

我不想少量移动按钮,因为我需要五个按钮,而且效率很低。

有人可以为我提供类似于上述问题的答案,即UIButtons 不包含在任何数组中吗?

编辑: UIButton在接口文件中定义并连接到Interface Builder,分别是strongnonamatic

编辑 2:我现在正在使用 animateWithDuration:delay:options:animations:completion:,但它仍然不起作用。

我使用此代码来检测按钮是否被按下。

- (void)squareOnePressed:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];
    if ([self.squareOne.layer.presentationLayer hitTest:touchLocation])
    {
        NSLog(@"YO");
    }
}

但是当按下按钮时仍然没有任何反应......那么我该如何编程^^^^

【问题讨论】:

  • 在哪里定义以及如何定义 UIButton?你的问题太宽泛了。
  • 那你有没有在 animateWithDuration:delay:options:animations:completion: 中添加 UIViewAnimationOptionAllowUserInteraction 的选项?
  • UIButton 是否连接到 Interface Builder?如果是这样,您需要将其设为weak
  • @ray 它是连接的,但它必须保持强大,因为我使用以编程方式编写的约束。 self.squareOne = [[UIButton alloc] init]; 按钮弱时产生错误
  • 如果你在 IB 中创建了那个按钮,你不应该在 self.squareOne 上调用 alloc init ——它已经实例化了,你为什么要创建一个新的?

标签: ios objective-c uibutton


【解决方案1】:

我发现了问题。约束阻止我与按钮交互。当我从按钮中删除以编程方式编写的约束时,它工作正常。

【讨论】:

  • 如果您对按钮有限制,那么您不应该在动画中设置新帧。您应该改为调整约束。您不应该在使用布局约束的视图上设置任何框架。
  • @rdelmar 不,我的意思是所有约束都是以编程方式声明的。没有使用界面生成器进行任何约束
  • 我在评论中没有说任何关于界面生成器的内容。无论您是在 IB 中还是在代码中创建约束,都适用相同的规则——如果您使用约束,则不要设置框架。
  • @rdelmar 那么你能在不设置框架的情况下回答stackoverflow.com/questions/26499193/…吗?因为当我摆脱那段代码时,约束就会搞砸
  • 您已经接受了张对那个问题的回答,并且这个回答没有设置任何框架。我在这里回答了一个类似的问题,stackoverflow.com/questions/26268387/…,它使用了间隔视图,因此无论屏幕大小如何,它都能保持按钮的间距相等。
【解决方案2】:

为什么你的方法命名为squareOneTouched:?你的IBAction 触发了吗?

如果你要继承 UIViewController,你应该重写 touchesBegan: 方法。

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

【讨论】:

  • 我只是将它命名为squareOneTouched,因为我需要其中的 5 段代码,而且我担心它会让人困惑。另外,当我使用它时,它仍然不起作用
  • 那么你应该在上面的方法中为每个正方形添加一个if 语句。您是否按照其他线程的建议导入了QuartzCore
  • 是的,在两个文件中。我不能做多个 if 语句。这是因为如果条件为真,我实际上希望方法的类型为 BOOLreturn YES;。我只是使用voidNSLog 来查看是否可以检测到按钮按下
【解决方案3】:

尝试在选项参数中使用UIViewAnimationOptionAllowUserInteraction 调用animateWithDuration:delay:options:animations:completion:

将 IBAction 添加到您的视图控制器并将您的按钮连接到该控制器。将您的 NSLog 放入 IBAction 方法中。 (你知道怎么做吗?)

【讨论】:

  • 现在我想我已经弄清楚了,但是我要完成什么?
  • NULL 很好,如果你没有什么具体的事情你想做的动画已经完成。
  • 但是,我刚刚意识到:我正在使用此代码来确保在动画期间可以按下按钮,但如果我使用- (IBAction),则无论如何都不会检测到按钮按下。跨度>
  • 如果你使用UIViewAnimationOptionAllowUserInteraction,如果按下按钮,IBAction 将在动画期间被调用。
  • NSLog 是指整个方法吗?或者只是一部分。如果是一部分,这包括哪几行代码?
猜你喜欢
  • 2012-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-12
  • 1970-01-01
相关资源
最近更新 更多