在CAShapeLayer中展开就地动画 [英] Expand in place animation in CAShapeLayer

查看:78
本文介绍了在CAShapeLayer中展开就地动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为CAShape图层设置动画,但是无法正确处理。我有一个图层。在UIView子类中,我按如下方式创建CALayer-

I'm trying hard to animate a CAShape Layer but couldn't get it right.I have a layer. In a UIView subclass i create CALayer as follows -

CAShapeLayer *shape1 = [CAShapeLayer layer];
shape1.fillColor = [[UIColor clearColor] CGColor];
shape1.strokeColor = [[UIColor purpleColor] CGColor];

CGRect frame1 = CGRectMake(13, 13, self.bounds.size.width - 26, self.bounds.size.height - 26);
shape1.frame = frame1;
shape1.position = CGPointMake(0,0);
shape1.anchorPoint = CGPointMake(self.tableContainerView.frame.size.width/2.0,
                                 self.tableContainerView.frame.size.height/2.0);
shape1.path = [self pathForFrame:frame1];
shape1.strokeEnd = 0.0f;
shape1.lineWidth = 1.0;

[self.layer addSublayer:shape1];

我想对其进行动画处理,以使其达到比初始稍大的位置,同时保持中心,这会产生就地扩展效果。我的缩放动画代码是-

I want to animate it so that it reaches some where it looks slightly bigger than initial while maintaining the center, this would create expand in place effect. My scaling animation code is -

CABasicAnimation *animation2 = [CABasicAnimation animationWithKeyPath:@"transform.scale"];

//animation2.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
animation2.toValue =  [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1, 1.1, 1.1)];
animation2.repeatCount = 1;
animation2.removedOnCompletion = NO;
animation2.fillMode = kCAFillModeForwards;
animation2.duration = 10;
animation2.beginTime = CACurrentMediaTime() + 4;
[shape1 addAnimation:animation2 forKey:nil];

任何帮助表示赞赏。

推荐答案

您可以使用 CAKeyFrameAnimation 进行更好的控制

You can use CAKeyFrameAnimation to have better control over the animation.

您可以轻松做到:

 CAMediaTimingFunction *linearTiming = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
 CAKeyframeAnimation *scaleXAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale.x"];
    scaleXAnimation.duration = 0.300;
    scaleXAnimation.values = @[@(0.100), @(0.120), @(0.140), @(0.160)];
    scaleXAnimation.keyTimes = @[@(0.000), @(0.333), @(0.667), @(1.000)];
    scaleXAnimation.timingFunctions = @[linearTiming, linearTiming, linearTiming];
    scaleXAnimation.beginTime = 0;
    scaleXAnimation.fillMode = kCAFillModeBoth;

    [[self.yourView layer] addAnimation: scaleXAnimation forKey:@"ScaleUp_ScaleX"];

     //or if you are subclassing the uiview

     [[self layer] addAnimation: scaleXAnimation forKey:@"ScaleUp_ScaleX"];

这篇关于在CAShapeLayer中展开就地动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆