【问题标题】:Disable a button in iOS when a certain image is shown显示特定图像时禁用iOS中的按钮
【发布时间】:2013-09-17 22:45:58
【问题描述】:

我有一个 animationImages 动画,其中我的 UIImage *imageView 在动画的最后一帧结束。

当动画的最后一个图像显示时,我想禁用启动动画的按钮(buttonOne),并在按下另一个按钮(buttonTwo)时重置动画。 ButtonTwo 将动画的第一个图像显示出来,因此它不显示最后一个图像。所以基本上,我正在想办法让用户重置动画。

我的问题:如何在显示最后一张图片时禁用 buttonOne,并在显示第一张图片时再次启用 buttonOne?

这是我的第一次尝试:

- (IBAction)buttonOne:(UIButton *)sender {
if (self.imageView.image == [self.imageView.animationImages lastObject]) {[self.buttonOne     setEnabled:NO];}
else {[self.buttonOne setEnabled:YES];}

到目前为止,该按钮仍处于禁用状态。有什么想法吗?

【问题讨论】:

    标签: ios objective-c animation


    【解决方案1】:

    您不能将启用按钮的代码放在按钮的操作方法中,因为此时它将被禁用。我认为没有任何方法可以在动画完成时获得回调,但是由于您设置了动画持续时间,因此您可以在同一时间段后调用一种方法来禁用按钮。像这样的:

    -(IBAction)animatePcs:(id)sender {
        self.iv.animationImages = self.pics;
        self.iv.animationDuration = 2.0;
        self.iv.animationRepeatCount = 1;
        [self.iv startAnimating];
        [self performSelector:@selector(disableButton) withObject:nil afterDelay:2.0];
    }
    
    -(void)disableButton {
        self.button1.enabled = NO;
        [self.button1 setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    }
    
    -(IBAction)reenableButton1:(id)sender { // button 2 method
        self.button1.enabled = YES;
        [self.button1 setTitleColor:self.buttonTextColor forState:UIControlStateNormal];
    }
    

    我使用 [self.button1 titleColorForState:UIControlStateNormal] 在 vi​​ewDidLoad 中设置 buttonTextColor 的值。

    【讨论】:

    • 我只需要在 button1 方法的末尾说 [self.button1 setEnabled:NO],在 button2 方法的末尾说 [self.button1 setEnabled:Yes]!再次感谢。谜题解决了
    • @MScottWaller,在动画开始时立即禁用按钮,不是吗(我添加了标题颜色更改,以便我可以直观地判断按钮何时被禁用)?
    • 动画与按下 button1 相关。所以你点击 button1,它会启动动画,然后它会自行禁用。并且只有在按下button2后才会启用
    • @MScottWaller,不过,在你的问题中,你说你想在最后一个图像在图像视图中时禁用按钮,这将在动画结束时——如果你只是设置button to enabled = NO 在按钮的方法中,它将在动画开始时被禁用。这就是我使用延迟调用 disableButton 的原因。无论如何,如果我的回答有助于解决你的问题,你应该接受它。
    【解决方案2】:
    - (void) startanimation {
    
      [buttonOne.userInteractionEnabled = NO];
        [UIView animateWithDuration:5. delay:0. options:UIViewAnimationOptionCurveEaseInOut animations:^{
            // do your animation here
        } completion:^(BOOL finished) {
             // When your animation finished, allow users to interact with the button
            [buttonOne.userInteractionEnabled = YES];
        }];
    
    }
    

    【讨论】:

      猜你喜欢
      • 2016-03-30
      • 1970-01-01
      • 2013-03-23
      • 1970-01-01
      • 1970-01-01
      • 2013-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多