如何以这样的方式为uilabel视图制作动画,使其能够从屏幕的右侧向左移动,在对象中

时间:2011-06-30 13:45:59

标签: iphone objective-c animation

如何以这样的方式为uilabel视图设置动画,使其从屏幕右侧向左移动,

我使用了以下代码,但它没有工作

 UILabel * lbl =[UILabel alloc]init];

        CALayer *cloud = [CALayer layer];
        cloud.contents = lbl.text;
        cloud.bounds = CGRectMake(0, 40, 100, 30);

        cloud.position = CGPointMake(-45 ,30);
        //cloud.position = CGPointMake(self.view.bounds.size.width / 2,cloudImage.size.height / 2);

        [self.view.layer addSublayer:cloud];

        CGPoint startPt = CGPointMake(self.view.bounds.size.width + cloud.bounds.size.width / 2,
                                      cloud.position.y);
        CGPoint endPt = CGPointMake(cloud.bounds.size.width / -2,
                                    cloud.position.y);

        CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position"];
        anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
        anim.fromValue = [NSValue valueWithCGPoint:startPt];
        anim.toValue = [NSValue valueWithCGPoint:endPt];
        anim.repeatCount = 1;
        anim.duration = 5.0;
        [cloud addAnimation:anim forKey:@"position"];

2 个答案:

答案 0 :(得分:0)

如果你可以在ios4中使用animateWithDuration,那就容易多了

  

UILabel * label = //新标签

     

[UIView animateWithDuration:1.0动画:^ {

   label.frame = //new frame
     

}];

答案 1 :(得分:0)

你可以这样做:

    UILabel *label = [[UILabel alloc] initWithFrame:theRect];
[self.view addSubview:label];
[UIView animateWithDuration:0.5 
                      delay:0.0 
                    options:UIViewAnimationCurveEaseIn 
                 animations:^{label.bounds = targetRect;} 
                 completion:^(BOOL finished) { /* completion stuff */}];

有更多的动画方法可以减少参数。您可以在UIView文档中找到它们。