如何动画UILabel的动作

时间:2012-07-27 02:28:33

标签: iphone objective-c cocoa

我正在我的应用程序中移动UILabel,我希望它能够在几帧而不是跳跃位置上更改位置和大小。我正在为Iphone使用Objective-c和cocoa开发。任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:9)

最简单的方法是:

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

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

答案 1 :(得分:2)

此代码对您的问题有帮助

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