如何让UILabel淡化&与CATransition一起出去?

时间:2014-02-09 16:59:32

标签: ios iphone objective-c uilabel catransition

我正试图让AM / PM车道淡入& out,但我似乎无法让它双向工作。

如果我只使用一个它可以工作,但是当我尝试添加两个时,它只是在没有正确的淡入淡出动画的情况下来回翻转。

任何人都可以告诉我为什么以及如何解决这个问题?

下面是我的代码。

- (void)setState:(MonringNightLabelState)state animated:(BOOL)animated {

    CATransition *animationAM = [CATransition animation];
    animationAM.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animationAM.type = kCATransitionFade;
    animationAM.duration = 0.3;

    CATransition *animationPM = [CATransition animation];
    animationPM.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animationPM.type = kCATransitionFade;
    animationPM.duration = 0.3;

    if (animated)
    {
        [UIView animateWithDuration:0.3
                              delay:0.0
                            options:UIViewAnimationOptionBeginFromCurrentState
                         animations:^{
                             [self setState:state animated:NO];
                         }
                         completion:^(BOOL finished) {

                         }];
    }

    switch (state)
    {
        case MorningNightLabelStateAM:
        {
            [self.morningNightLabel.layer addAnimation:animationAM forKey:@"kCATransitionFade"];

            self.morningNightLabel.text = @"AM";

        }
            break;
        case MorningNightLabelStatePM:
        {
            [self.morningNightLabel.layer addAnimation:animationPM forKey:@"kCATransitionFade"];

            self.morningNightLabel.text = @"PM";
        }
            break;
    }
}

1 个答案:

答案 0 :(得分:0)

这是我在swift中修复它的方式:

var transitionAnimation = CATransition()
transitionAnimation.type = kCATransitionFade
transitionAnimation.duration = 0.2
transitionAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
transitionAnimation.fillMode = kCAFillModeBoth
self.titleLabel.layer.addAnimation(transitionAnimation, forKey: "fadeAnimation")
self.titleLabel.text = newTitle

这对我有用。

可能这应该足够了:

CATransition *animationPM = [CATransition animation];
animationPM.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animationPM.type = kCATransitionFade;
animationPM.duration = 0.3;
[self.morningNightLabel.layer addAnimation:animationAM forKey:@"kCATransitionFade"];
self.morningNightLabel.text = (state == MorningNightLabelStateAM) ? @"AM" : @"PM";