【问题标题】:Repeating UIButton animation and change background重复 UIButton 动画并更改背景
【发布时间】:2014-11-05 00:32:25
【问题描述】:

谁能告诉我为什么这不起作用?:

//Changing the Images
- (void) squareOneColour {
    NSUInteger r = arc4random_uniform(5);
    [self.squareOne setBackgroundImage:[self.colorArray objectAtIndex:r] forState:UIControlStateNormal];
}

//Moving buttons
- (void) squareOneMover {
    NSUInteger r = arc4random_uniform(3);

    [self.squareOne setHidden:NO];
    CGPoint originalPosition = self.squareOne.center;
    originalPosition.y = -55;

    [self.squareOne setCenter:originalPosition];
    CGPoint position = self.squareOne.center;
    position.y += 760;

    [UIView animateWithDuration:r + 3
                          delay:0.0
                        options:UIViewAnimationOptionAllowUserInteraction
                     animations:^{
                         [self.squareOne setCenter:position];
                     }
                     completion:^(BOOL complete) {
                         if (complete) {
                             [self squareOneColour];
                             [self squareOneMover];
                         }
                     }
    ];
}

我正在尝试让UIButton 在屏幕上设置动画,并在动画完成后重复播放,但背景不同。上面的代码似乎应该可以工作,但第二个动画从未完成。 (UIButton 动画一次,然后一切都停止)。我尝试将下面显示的代码(放入完成块中),使用外部 NSUInteger 方法为 r 创建一个随机数,但它永远不会工作。但是当我将 r 替换为不同的数字(如 1 或 2)时,它确实有效。

【问题讨论】:

    标签: objective-c animation uibutton


    【解决方案1】:

    我找到了答案。玩了之后,我发现我想要UIButton 做的功能只适合UIImageView。我想我必须在UIImageView 上制作一个隐形按钮

    【讨论】:

    • 这与它无关。您的代码中的任何内容都不适用于 UIButton。我试过了,效果很好(当然是我的改动)。
    • 而且图像视图上的按钮很愚蠢。如果您想要可点击性,请使用手势识别器。
    【解决方案2】:

    你是在踩自己的脚。跳出runloop的下一个循环,一切都会好起来的:

    [self squareOneColour];
     dispatch_async(dispatch_get_main_queue(), ^{
          [self squareOneMover];
    });
    

    【讨论】:

    • 我试试看。你能解释一下这是做什么的吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-08
    • 1970-01-01
    • 1970-01-01
    • 2014-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多