motionpotion
motionpotion

Reputation: 2736

Slide UILabel Onto View Controller

I'm trying to move a UILabel from a starting position of off the top of my UIViewController in a smooth slide type animation so that it slides down from the top when the view loads and then stops at a y position for about 10 seconds. After 10 seconds I want to slide back off the view again.

-(void) animateInstructionLabel
{
float newX = 50.0f;
float newY = 100.0f;

[UIView transitionWithView:self.lblInstruction
                  duration:10.0f
                   options:UIViewAnimationCurveEaseInOut
                animations:^(void) {
                    lblInstruction.center = CGPointMake(newX, newY);
                }
                completion:^(BOOL finished) {
                    // Do nothing
                }];
} 

But I don't know how to do the 10 second delay and then move back within the method above. The end result is that I want this to be a label that appears like a notification and then moves off screen again.

Could someone hep me put these pieces described above together?

EDIT

I am adding this based on answer below:

-(void) animateInstructionLabel
{
float newX = lblInstruction.frame.origin.x;
float newY = 20.0f;

lblInstruction.center = CGPointMake(lblInstruction.frame.origin.x, -20.0f);
lblInstruction.bounds = CGRectMake(lblInstruction.frame.origin.x, -20.0f, 650.0f, 40.0f);

[UIView animateWithDuration:3.0f
                      delay:0
                    options:UIViewAnimationCurveEaseInOut
                 animations:^(void) {
                     lblInstruction.center = CGPointMake(newX, newY);
                 }
                 completion:^(BOOL finished) {
                     [UIView animateWithDuration:5.0f
                                           delay:5.0
                                         options:UIViewAnimationCurveEaseInOut
                                      animations:^(void) {
                                          lblInstruction.center = CGPointMake(newX, -20);
                                      }
                                      completion:^(BOOL finished) {
                                          // Do nothing
                                      }
                      ];
                 }
 ];
}

But even though I am setting the label.center off the screen before the animation starts I am seeing that it moves from top left corner of the viewcontroller to the center of the viewcontroller. It is not keeping the x position that it should be set to before the animation begins.

Upvotes: 1

Views: 846

Answers (3)

motionpotion
motionpotion

Reputation: 2736

Here is what worked in case it helps somebody else. I had to use .frame instead of .center to set the initial position and then the subsequent animation back to that position.

-(void) animateInstructionLabel
{

lblInstruction.frame = CGRectMake(lblInstruction.frame.origin.x, -40.0f, 650.0f, 40.0f);
[self.view addSubview:lblInstruction];
lblInstruction.hidden = NO;

float newX = lblInstruction.frame.origin.x;
float newY = 20.0f;

[UIView animateWithDuration:3.0f
                      delay:0
                    options:UIViewAnimationCurveEaseInOut
                 animations:^(void) {
                     lblInstruction.center = CGPointMake(newX, newY);
                 }
                 completion:^(BOOL finished) {
                     [UIView animateWithDuration:3.0f
                                           delay:5.0
                                         options:UIViewAnimationCurveEaseInOut
                                      animations:^(void) {
                                          lblInstruction.frame = CGRectMake(lblInstruction.frame.origin.x, -40.0f, 650.0f, 40.0f);
                                      }
                                      completion:^(BOOL finished) {
                                          lblInstruction.hidden = YES;
                                      }
                      ];
                 }
 ];
}

This smoothly makes the label slide down from off the view, stop for 5 seconds and then smoothly moves it back vertically off the view.

Thanks for the previous answers which lead me to the above final solution.

Upvotes: 0

rmaddy
rmaddy

Reputation: 318944

Something like this should work:

-(void) animateInstructionLabel {
    float newX = 50.0f;
    float newY = 100.0f;

    labelInstruction.center = CGPointMake(newX, -20); // start off screen

    [UIView animateWithDuration:10.0f
                delay:0
                options:UIViewAnimationCurveEaseInOut
                animations:^(void) {
                    lblInstruction.center = CGPointMake(newX, newY);
                }
                completion:^(BOOL finished) {
                    [UIView animateWithDuration:10.0f
                                delay:10.0
                                options:UIViewAnimationCurveEaseInOut
                                animations:^(void) {
                                    lblInstruction.center = CGPointMake(newX, -20);
                                }
                                completion:^(BOOL finished) {
                                    // Do nothing
                                }
                    ];
                }
    ];
}

Upvotes: 1

Stavash
Stavash

Reputation: 14314

Try adding this after your animation block (oldX,oldY are the old coordinates, before animating the label in):

double delayInSeconds = 10.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    [UIView transitionWithView:self.lblInstruction
                      duration:10.0f
                       options:UIViewAnimationCurveEaseInOut
                    animations:^(void) {
                        lblInstruction.center = CGPointMake(oldX, oldY);
                    }
                    completion:^(BOOL finished) {
                        // Do nothing
                    }];
});

Upvotes: 1

Related Questions