如何使用动画

时间:2015-07-17 11:02:43

标签: ios objective-c animation shape

我有一个标签:

    self.logInLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 100)];
    self.logInLabel.backgroundColor = [UIColor clearColor];
    self.logInLabel.backgroundColor = Color_Circle_Outer;
    self.logInLabel.textColor = Color_Circle_Outer;
    self.logInLabel.textAlignment = NSTextAlignmentCenter;
    self.logInLabel.lineBreakMode = NSLineBreakByWordWrapping;
    self.logInLabel.numberOfLines = 1;
    self.logInLabel.font = [UIFont boldSystemFontOfSize:12*IPAD];
    self.logInLabel.text = @"Log In";
    [self addSubview:self.logInLabel];
    self.logInLabel.layer.cornerRadius   = self.frame.size.height/2.0;
    self.logInLabel.layer.borderColor    = Color_Circle_Outer.CGColor;
    self.logInLabel.layer.borderWidth    = 2*IPAD;
    self.logInLabel.layer.masksToBounds  = YES;

我想将此形状更改为圆形。我试过了:

    [UIView animateWithDuration:AnimationTime animations:^{
       self.logInLabel.layer.bounds = CGRectMake(0, 0, self.frame.size.height, self.frame.size.height);
    }];

但是没有动画就立即改变了形状,然后将位置改为以动画为中心。然后我尝试了这个:

    [UIView animateWithDuration:AnimationTime animations:^{
       self.logInLabel.bounds = CGRectMake(0, 0, self.logInLabel.frame.size.height, self.logInLabel.frame.size.height);
    }];

我的动画效果非常难看。

我不在乎这个视图是否是标签,我只想实现将矩形形状更改为带有动画的圆形形状。

谢谢。

3 个答案:

答案 0 :(得分:1)

它只适用于CABasicAnimation,而不适用于UIView animateWithDuration

    UIView *logInLabel = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
    logInLabel.backgroundColor = [UIColor blackColor];
    logInLabel.layer.masksToBounds = YES;
    [self.view addSubview:logInLabel];

    CABasicAnimation *animation = [CABasicAnimation  animationWithKeyPath:@"cornerRadius"];
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    animation.fromValue = [NSNumber numberWithFloat:0.0f];
    animation.toValue = [NSNumber numberWithFloat:100.0f];
    animation.duration = 5.0;
    [logInLabel.layer addAnimation:animation forKey:@"cornerRadius"];
    [logInLabel.layer setCornerRadius:0.0];

答案 1 :(得分:0)

从您的代码中移除以下行:

self.logInLabel.layer.cornerRadius = self.logInLabel.frame.size.height / 2.0;

并将其放在像这样的动画块中

您可以设置cornerRadius的{​​{1}}以获得圆形视图。

UILabel

然而,由于[UIView animateWithDuration:AnimationTime animations:^{ self.logInLabel.layer.cornerRadius = self.logInLabel.frame.size.height / 2.0f; }]; 的{​​{1}}和height不是1:1的比例,动画看起来不会很好,最终的框架也不会是精确的圆圈。

如果您仍然没有动画,请尝试检查width变量的值。

答案 2 :(得分:0)

看起来可能对你有帮助

[UIView animateWithDuration:0.4 animations:^{
        CGPoint center = self.logInLabel.center;
        self.logInLabel.layer.cornerRadius = self.logInLabel.frame.size.height/2;
        self.logInLabel.frame = CGRectMake(0, 0, self.logInLabel.frame.size.height, self.logInLabel.frame.size.height);
        self.logInLabel.center = center;
    }];