CoreAnimation - Opacity Fade In and Out Animation Not Working

后端 未结 11 1828
悲&欢浪女
悲&欢浪女 2020-12-08 05:02

I\'m attempting to create a fairly simple CoreAnimation for use in an AVComposition. My goal is to create a CALayer which, through various sublayer

相关标签:
11条回答
  • 2020-12-08 05:55

    Look at my answer https://stackoverflow.com/a/44204846/667483

    In summary: to use beginTime you should set fillMode to kCAFillModeBackwards on you animation object.

    0 讨论(0)
  • 2020-12-08 05:57

    The point is key name. You have to set the opacity key.

    layer.add(animation, forKey: nil)         // Not Working
    layer.add(animation, forKey: "opacity")   // Working
    

    Check the sample code. I tested in Swift 4

        let animation                   = CAKeyframeAnimation()
        animation.duration              = 1.53
        animation.autoreverses          = false
        animation.keyTimes              = [0, 0.51, 0.85, 1.0]
        animation.values                = [0.5, 0.5, 1.0, 0.5]
        animation.beginTime             = 0
        animation.isRemovedOnCompletion = false
        animation.fillMode              = kCAFillModeBoth
        animation.repeatCount           = .greatestFiniteMagnitude
        layer.add(animation, forKey: "opacity")
    
    0 讨论(0)
  • 2020-12-08 05:57

    Try:

    fadeInAnimation.beginTime = CACurrentMediaTime()+1.0;
    
    0 讨论(0)
  • 2020-12-08 05:58

    I'm faced the same issue and the problem is - one layer can't contain fade in and out. So you can add the other animation to the parent layer as i did

    CALayer *parentLayer = [CALayer layer];
    CALayer *animtingLayer = [CALayer layer];
    
    //FADE IN
            CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
            animation.beginTime = CMTimeGetSeconds(img.startTime);
            animation.duration = CMTimeGetSeconds(_timeline.transitionDuration);
            animation.fromValue = [NSNumber numberWithFloat:0.0f];
            animation.toValue = [NSNumber numberWithFloat:1.0f];
            animation.removedOnCompletion = NO;
            animation.fillMode = kCAFillModeBoth;
            animation.additive = NO;
            [parentLayer addAnimation:animation forKey:@"opacityIN"];
    
    //FADE OUT
            CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
            animation.beginTime = CMTimeGetSeconds(CMTimeAdd(img.passTimeRange.start, img.passTimeRange.duration));
            animation.duration = CMTimeGetSeconds(_timeline.transitionDuration);
            animation.fromValue = [NSNumber numberWithFloat:1.0f];
            animation.toValue = [NSNumber numberWithFloat:0.0f];
            animation.removedOnCompletion = NO;
            animation.fillMode = kCAFillModeBoth;
            animation.additive = NO;
            [animtingLayer addAnimation:animation forKey:@"opacityOUT"];
    
        [parentLayer addSublayer:animtingLayer];
    
    0 讨论(0)
  • 2020-12-08 05:58

    Man, so many complicated answers. The simplest way is just to add autoreverse. Voila.

    CABasicAnimation *fadeInAndOut = [CABasicAnimation animationWithKeyPath:@"opacity"];
    fadeInAndOut.duration = 5.0;
    fadeInAndOut.autoreverses = YES;
    fadeInAndOut.fromValue = [NSNumber numberWithFloat:0.0];
    fadeInAndOut.toValue = [NSNumber numberWithFloat:1.0];
    fadeInAndOut.repeatCount = HUGE_VALF;
    fadeInAndOut.fillMode = kCAFillModeBoth;
    [titleLayer addAnimation:fadeInAndOut forKey:@"myanimation"];
    
    0 讨论(0)
提交回复
热议问题