King Popsicle
King Popsicle

Reputation: 465

How to animate the movement of a UILabel

I am moving a UILabel around in my app and I would like it to change location and size over a few frames instead of jumping location. I am using Objective-c and cocoa developing for the Iphone. Can anyone help me out?

Upvotes: 2

Views: 3594

Answers (2)

Selkie
Selkie

Reputation: 1783

The easiest way to do it is like:

    CGRect endFrame = /*The frame of your label in end position*/

    [UIView animateWithDuration:0.5 animations:^{ 
        myLabel.frame = endFrame;
   }];

Upvotes: 9

Raju
Raju

Reputation: 759

This Code is Helpful for your problem

#define FONT_SIZE 14
#define DURATION  10
#define DELAY     10
-(void)viewWillAppear:(BOOL)animated {


  NSString* string = @"My Label";

  CGSize framesize = [string sizeWithFont:[UIFont fontWithName:@"Copperplate" size:FONT_SIZE]];

  // Origin
  float x0 = 0;
  float y0 = 0;

  CGRect  appFrame       = [[UIScreen mainScreen] applicationFrame];
  CGFloat appFrameWidth  = appFrame.size.width;
  CGFloat appFrameHeight = appFrame.size.height;

  // Destination
  float x1 = appFrameWidth - framesize.width;
  float y1 = appFrameHeight - framesize.height;

  UILabel* label = [[UILabel alloc]initWithFrame:CGRectMake(x0, y0, framesize.width, framesize.height)];
  label.backgroundColor = [UIColor clearColor];
  label.text = string;
  label.shadowColor = [UIColor grayColor];
  label.shadowOffset = CGSizeMake(1,2);
  label.font = [UIFont fontWithName:@"Copperplate" size:FONT_SIZE];

  [self.view addSubview:label];

  [UIView animateWithDuration:DURATION
                        delay:DELAY
                      options:UIViewAnimationOptionAllowUserInteraction
                   animations:^{
                       label.frame = CGRectMake(x1, y1, framesize.width, framesize.height);
                   }
                   completion:^(BOOL finished){
                     [label removeFromSuperview];
                   }];
}

Upvotes: 3

Related Questions