简体   繁体   中英

iOS: Animating text position of UIButton doesn't work

I want to have the text of the button 'go away' when pressing a 'wrong answer' button.

In my problem-demo code, my project has two buttons, one with outlet 'myBtn' without any action, and one with TouchUpInside action. The action handler looks like this:

- (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];
}

I am trying to animate two properties: 'alpha' to go from 1 to 0, and position of text to move 60 points to the left.

If I uncomment the first "UILAbel" line and comment the second, then pressing the button runs a nice animation in the second button.

But if I leave the code as it appears, trying to animate the text of the pressed button itself, the alpha is animating fine, but the position is not changing.

Any help would be highly appreciated!

I have seen this kind of problem occurring on iOS7. Animations that worked fine inside an IBAction, don't work on iOS7. I had to move all my animation code to a different method and call the selector after a delay. Your code will work fine if you do this -

- (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];
}

Your problem is mixing up the UIButton with the UILabel .

The method parameter (UIButton*)sender is referencing a UIButton in this case. The reason UILabel *lbl = sender.titleLabel; doesn't work is because the sender is a UIButton reference. To access the label object, you must reference the the UILabel embedded in the UIButton via the hirarchy sender > UIButton > UILabel .

So the code you should use is:

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];
}

The reason why the code as it appears behaves strangly is because alpha is a property of both UIButton s and UILabel s. So it will work even if you are incorrectly refenrencing the sender .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM