0

当按下“错误答案”按钮时,我希望按钮的文本“消失”。

在我的问题演示代码中,我的项目有两个按钮,一个带有出口“myBtn”,没有任何操作,一个带有TouchUpInside操作。动作处理程序如下所示:

- (IBAction)goPressed:(UIButton*)sender {

//UILabel *lbl = self.myBtn.titleLabel;
UILabel *lbl = sender.titleLabel;
[UIView animateWithDuration:1.0
                      delay:0.0
                    options:UIViewAnimationOptionCurveEaseOut
                 animations:^{
                     lbl.center = CGPointMake(lbl.center.x-60, lbl.center.y);
                     lbl.alpha = 0;
                 }
                 completion:nil];
}

我正在尝试为两个属性设置动画:“alpha”从 1 变为 0,文本的位置向左移动 60 点。

如果我取消注释第一行“UILAbel”并注释第二行,那么按下按钮会在第二个按钮中运行一个漂亮的动画。

但是,如果我将代码保持原样,尝试为按下按钮本身的文本设置动画,则 alpha 的动画效果很好,但位置没有改变。

任何帮助将不胜感激!

4

2 回答 2

2

我在iOS7上看到过这种问题。在 IBAction 中运行良好的动画在 iOS7 上无法运行。我不得不将我所有的动画代码移动到不同的方法并在延迟后调用选择器。如果您这样做,您的代码将正常工作 -

- (IBAction) goPressed:(UIButton*)sender {
[self performSelector:@selector(animateButton:) withObject:sender afterDelay:0.1];
}

- (void) animateButton:(UIButton *) button{
UILabel *lbl = button.titleLabel;
[UIView animateWithDuration:1.0
                      delay:0.0
                    options:UIViewAnimationOptionCurveEaseOut
                 animations:^{
                     lbl.center = CGPointMake(lbl.center.x-60, lbl.center.y);
                     lbl.alpha = 0;
                 }
                 completion:nil];
}
于 2013-10-14T06:43:31.073 回答
0

您的问题是UIButtonUILabel.

在这种情况下,方法参数(UIButton*)sender是引用 a 。UIButton原因UILabel *lbl = sender.titleLabel;不起作用是因为senderUIButton参考。要访问标签对象,您必须通过层次结构引用UILabel嵌入的。UIButtonsender > UIButton > UILabel

所以你应该使用的代码是:

UIButton *button = sender;
UILabel *lbl = sender.titleLabel;
[UIView animateWithDuration:1.0
                  delay:0.0
                options:UIViewAnimationOptionCurveEaseOut
             animations:^{
                 lbl.center = CGPointMake(lbl.center.x-60, lbl.center.y);
                 lbl.alpha = 0;
             }
         completion:nil];
}

代码出现异常的原因是因为它是s 和salpha的属性。因此,即使您错误地引用.UIButtonUILabelsender

于 2013-10-14T06:44:44.983 回答