1

我有一组要添加到 CATextlayer 的动画;一个不透明动画,使图层在几秒钟后消失,以及一个“摇晃”动画,我希望图层在访问时稍微摇晃一下。不透明动画效果很好;震动没有任何作用——我希望我能解释为什么。

这是我的图层和不透明度动画的代码,效果很好:

    CATextLayer *subtitle1Text = [[CATextLayer alloc] init];
    [subtitle1Text setFont:@"Helvetica-Bold"];
    [subtitle1Text setFontSize:25];
    [subtitle1Text setFrame:CGRectMake(0, 0, self.screenSize.width, 100)];
    [subtitle1Text setString: self.videoDescription1.text ];
    [subtitle1Text setAlignmentMode:kCAAlignmentCenter];
    [subtitle1Text setForegroundColor:[[UIColor whiteColor] CGColor]];

    CABasicAnimation *animation1 =[CABasicAnimation animationWithKeyPath:@"opacity"];
    animation1.duration= 0 ;
    animation1.fromValue=[NSNumber numberWithFloat:1.0];
    animation1.toValue=[NSNumber numberWithFloat:0.0];
    animation1.beginTime = 3;
    animation1.removedOnCompletion = NO;
    animation1.fillMode = kCAFillModeBoth;
    animation1.additive = NO;
    animation1.autoreverses = NO;

这是我的摇晃动画,它什么也没做:

    CAKeyframeAnimation * shakeAnim = [ CAKeyframeAnimation animationWithKeyPath:@"transform" ] ;
    shakeAnim.values = @[ [ NSValue valueWithCATransform3D:CATransform3DMakeTranslation(-10.0f, 0.0f, 0.0f) ], [ NSValue valueWithCATransform3D:CATransform3DMakeTranslation(10.0f, 0.0f, 0.0f) ] ] ;
    shakeAnim.autoreverses = YES ;
    shakeAnim.repeatCount = 2.0f ;
    shakeAnim.duration = 0.07f ;
    shakeAnim.beginTime = 0;
    shakeAnim.fillMode = kCAFillModeBoth;

在这里我添加动画:

    CALayer *overlayLayer1 = [CALayer layer];
    [overlayLayer1 addSublayer:subtitle1Text];
    overlayLayer1.frame = CGRectMake(0, 0,
                                    self.screenSize.width, self.screenSize.height);
    [overlayLayer1 setMasksToBounds:YES];
    [overlayLayer1 addAnimation:shakeAnim forKey:@"transform" ] ;
    [overlayLayer1 addAnimation:animation1 forKey:@"opacityOUT"];

    CALayer *parentLayer = [CALayer layer];
    CALayer *videoLayer = [CALayer layer];
    parentLayer.frame = CGRectMake(0, 0, self.screenSize.width, self.screenSize.height);
    videoLayer.frame = CGRectMake(0, 0, self.screenSize.width, self.screenSize.height);
    [parentLayer addSublayer:videoLayer];
    [parentLayer addSublayer:overlayLayer1];

很想知道为什么摇晃不起作用。

4

0 回答 0