简体   繁体   中英

uilabel animating to original position

I'm trying to animate a UILabel frame to a position in the middle of the screen. Instead, it seems to be animating from somewhere outside the view back to its original position.

[UIView beginAnimations:@"labelAnim" context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationCurve: UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:1.0];
[self.scoreLabel setFrame:self.endScoreFrame];
[UIView commitAnimations];

Here are the starting and ending frames:

Start Frame: {{10, 30}, {144, 21}}
Final Frame: {{93.75, 158.71438598632812}, {187.5, 46.875}}

Try this code

[UIView animateWithDuration:1.0
                          delay:0.0
                        options:UIViewAnimationCurveEaseIn | UIViewAnimationOptionBeginFromCurrentState
                     animations:^{
                         [self.scoreLabel setFrame:self.endScoreFrame];
                     } completion:^(BOOL finished) {
                         NSlog("completed");
                     }];

And make sure your endScoreFrame have the correct new position coordinates:

  NSLog(@"%@", NSStringFromCGRect(self.endScoreFrame)); 

I have tested this code with the values of your old and new frames and it works fine:

@property (nonatomic, strong) UILabel *lbl;

ViewDidLoad:

self.lbl = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 144, 21)];
self.lbl.text = @"mylabel";

[self.view addSubview:self.lbl] ;

Action

-(void)btnClick:(id)sender
{ 

    CGRect newFrame = CGRectMake(93.75, 158.71438598632812, 187.5, 46.875);


    [UIView animateWithDuration:1.0
                          delay:0.0
                        options:UIViewAnimationCurveEaseIn | UIViewAnimationOptionBeginFromCurrentState
                     animations:^{
                         self.lbl.frame = newFrame ;
                     } completion:^(BOOL finished) {

                     }];

}

I found the problem. I had forgotten that I had the label I was trying to animate in Interface Builder, so it was being overridden.

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