简体   繁体   English

iOS中的图层动画

[英]Animation for Layer in iOS

I masked an image with BezierPath. 我用BezierPath遮盖了图像。 I want to animate that mask image. 我想为该蒙版图像制作动画。

My code is as follow: 我的代码如下:

    UIBezierPath* bezierPath = [UIBezierPath bezierPath];
    [bezierPath moveToPoint: CGPointMake(84.95, 205.11)];
    [bezierPath addCurveToPoint: CGPointMake(122.89, 232.14) controlPoint1: CGPointMake(84.95, 205.11) controlPoint2: CGPointMake(110.81, 223.56)];
    [bezierPath addCurveToPoint: CGPointMake(124.33, 233.04) controlPoint1: CGPointMake(123.37, 232.46) controlPoint2: CGPointMake(123.85, 232.78)];
    [bezierPath closePath];

    CAShapeLayer *maskImage = [CAShapeLayer layer];
    maskImage.path = bezierPath.CGPath;
    maskImage.fillColor = [[UIColor blackColor] CGColor];
    _myImageView.layer.mask = maskImage;

    CAShapeLayer *border = [CAShapeLayer layer];
    border.path = bezierPath.CGPath;
    border.strokeColor = [UIColor redColor].CGColor;
    border.fillColor = [[UIColor clearColor] CGColor];
    border.lineWidth = 5;
    [_myImageView.layer addSublayer:border];

How can I animate this? 我该如何制作动画?

Thank you 谢谢

Use following piece of code: 使用以下代码:

@property (nonatomic, retain) CAShapeLayer *animationLayer;

- (IBAction)drawPattern:(id)sender{
[self setup];

[self.animationLayer removeAllAnimations];

CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
pathAnimation.duration = 10.0;
pathAnimation.fromValue = @(0);
pathAnimation.toValue = @(1);
[self.animationLayer addAnimation:pathAnimation forKey:@"strokeEnd"];
}

- (void)setup{
[self.animationLayer removeFromSuperlayer];
self.animationLayer = nil;

UIBezierPath* bezierPath = [UIBezierPath bezierPath];
[bezierPath moveToPoint: CGPointMake(84.95, 205.11)];
[bezierPath addCurveToPoint: CGPointMake(122.89, 232.14) controlPoint1: CGPointMake(84.95, 205.11) controlPoint2: CGPointMake(110.81, 223.56)];
[bezierPath addCurveToPoint: CGPointMake(124.33, 233.04) controlPoint1: CGPointMake(123.37, 232.46) controlPoint2: CGPointMake(123.85, 232.78)];

CAShapeLayer *pathLayer = [CAShapeLayer layer];

// provide frame & bounds so that path can be drawn over that.
pathLayer.frame = CGRectMake(35, 100, 250, 200);
pathLayer.bounds = CGRectMake(35, 100, 250, 200);
pathLayer.path = bezierPath.CGPath;
pathLayer.strokeColor = [UIColor redColor].CGColor;
pathLayer.fillColor = [[UIColor clearColor] CGColor];
pathLayer.lineWidth = 6.f;
pathLayer.lineJoin = kCALineJoinRound;

[self.view.layer addSublayer:pathLayer];
[self setAnimationLayer:pathLayer];
}

Please let me know if this works for you & also if anything else comes up. 请让我知道这是否适合您以及是否还有其他问题。

At the end of your code try below one 在代码末尾,尝试下面的代码之一

CAShapeLayer *pathLayer;
CAShapeLayer *shapeLayer = [CAShapeLayer layer];

shapeLayer.path = [aPath CGPath];
shapeLayer.strokeColor = [[UIColor greenColor] CGColor];
shapeLayer.fillColor = nil;
shapeLayer.lineWidth = 5.0f;
shapeLayer.lineJoin = kCALineJoinBevel;

[myImageView.layer addSublayer:shapeLayer];
pathLayer = shapeLayer;
CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
pathAnimation.duration = 3.0;
pathAnimation.fromValue = @(0.0f);
pathAnimation.toValue = @(1.0f);
[pathLayer addAnimation:pathAnimation forKey:@"strokeEnd"];

We can animate the image with path using CABasicAnimation. 我们可以使用CABasicAnimation使用路径对图像进行动画处理。

CABasicAnimation has position,path,transform,opacity,shadow......which including all layers keypaths,shape layer keypaths,CA layer keypaths,Text layer keypaths,Mask layer keypaths,UIView layer keypaths,Effect,Emit and replicator keypaths. CABasicAnimation具有位置,路径,变换,不透明度,阴影...,其中包括所有图层关键路径,形状图层关键路径,CA图层关键路径,文本图层关键路径,遮罩图层关键路径,UIView图层关键路径,效果,发射和复制器关键路径。

According to your question for drawing path we can use path and strokeEnd.But most prepare strokeEnd for accessing shape style properties. 根据您对绘制路径的问题,我们可以使用path和strokeEnd。但是大多数都准备一下strokeEnd以访问形状样式属性。

So I think strokeEnd is the best one for animating the mask image with bezier path. 因此,我认为strokeEnd是用贝塞尔曲线路径对蒙版图像进行动画处理的最佳方法。

Below I give the code for path and strokeEnd also I diffrecicate both. 在下面,我给出path和strokeEnd的代码,并对两者进行区分。

If I use animationWithKeyPath is path 如果我使用animationWithKeyPath是路径

The shape to be rendered and animatable.It is specifying the shape path. 要渲染和可动画化的形状。它指定形状路径。

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];
animation.fromValue = @(0);
animation.toValue = @(1);
animation.duration = 6.0f;
[maskImage addAnimation:animation forKey:@"animatePath"];

The Path is a collection of individual components like lines and arcs that can be drawn and also animated.It is fundamental concept of Quartz 2D. 路径是可以绘制和动画化的单个组件(如直线和圆弧)的集合,是Quartz 2D的基本概念。

In below code I use strokeEnd 在下面的代码中,我使用strokeEnd

Generally if we want to access the shape style properties we can use strokeEnd 通常,如果要访问形状样式属性,可以使用strokeEnd

The relative location at which to stop stroking the path and animatable. 停止抚摸路径的相对位置并且可以设置动画。

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
animation.fromValue = @(0);
animation.toValue = @(1);
animation.duration = 6.0f;
[maskImage addAnimation:animation forKey:@"strokeEnd"];

The value of this property must be in the range 0.0 to 1.0. 此属性的值必须在0.0到1.0的范围内。 The default value of this property is 1.0. 此属性的默认值为1.0。

Also strokeEnd defines following thing strokeEnd也定义以下内容

Combined with the strokeStart property, this property defines the subregion of the path to stroke. 与strokeStart属性结合使用,此属性定义笔画路径的子区域。 The value in this property indicates the relative point along the path at which to finish stroking while the strokeStart property defines the starting point . 此属性中的值指示笔划完成时沿路径的相对点,而strokeStart属性定义起点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM