- iOS Development coreanimation interpretation of the three--the use of several commonly used layer analysis
- First, Caemitterlayer
- Second, Cagradientlayer
- Third, Careplicatorlayer
- Iv. Cashapelayer
- Wu, Catextlayer
iOS Development coreanimation interpretation of the third--the use of several commonly used layers analysis one, caemitterlayer
Caemitterlayer is the particle emission layer in the Coreanimation framework, which is described in detail in a previous blog post, and is no longer repeated here, with the following address:
Application and flame examples of particle effects: http://my.oschina.net/u/2340880/blog/485095
Second, Cagradientlayer
Cagradientlayer are layer layers for color gradient display, and with Cagradientlayer, we can easily create color maps with transitions. The properties are as follows:
?
| 1234567891011121314151617181920 |
/*颜色数组,设置我们需要过的的颜色,必须是CGColor对象*/@property(nullable, copy) NSArray *colors;/*颜色开始进行过渡的位置这个数组中的元素是NSNumber类型,单调递增的,并且在0——1之间例如,如果我们设置两个颜色进行过渡,这个数组中写入0.5,则第一个颜色会在达到layer一半的时候开始向第二个颜色过渡*/@property(nullable, copy) NSArray<NSNumber *> *locations;/*下面两个参数用于设置渲染颜色的起点和终点 取值范围均为0——1默认起点为(0.5 ,0) 终点为(0.5 ,1),颜色的过渡范围就是沿y轴从上向下*/@property CGPoint startPoint;@property CGPoint endPoint;/*渲染风格 iOS中只支持一种默认的kCAGradientLayerAxial,我们无需手动设置*/@property(copy) NSString *type; |
Create an effect that spends the view with the following code:
?
| 12345678 |
CAGradientLayer * layer = [CAGradientLayer layer]; layer.colors = @[(id)[UIColor redColor].CGColor,(id)[UIColor blueColor].CGColor,(id)[UIColor greenColor].CGColor]; layer.locations = @[@0.1,@0.7,@1]; layer.bounds = CGRectMake(0, 0, 100, 100); layer.position = CGPointMake(100, 100); layer.startPoint = CGPointMake(0, 0); layer.endPoint = CGPointMake(1, 1); [self.view.layer addSublayer:layer]; |
The effect is as follows:
Third, Careplicatorlayer
Careplocatorlayer is a copy view container, where we can copy the sub-layer and make some difference processing, where the common attribute method is as follows:
?
| 12345678910111213141516 |
//拷贝的次数@property NSInteger instanceCount;//是否开启景深效果@property BOOLpreservesDepth;//当CAReplicatorLayer的子Layer层进行动画的时候,拷贝的副本执行动画的延时@property CFTimeInterval instanceDelay;//拷贝副本的3D变换@property CATransform3D instanceTransform;//拷贝副本的颜色变换@property(nullable) CGColorRef instanceColor;//每个拷贝副本的颜色偏移参数@property floatinstanceRedOffset;@property floatinstanceGreenOffset;@property floatinstanceBlueOffset;//每个拷贝副本的透明度偏移参数@property float instanceAlphaOffset; |
For example, by copying a color block to produce a panning arrangement:
?
| 1234567891011121314 |
CAReplicatorLayer *reLayer = [CAReplicatorLayer layer]; reLayer.position = CGPointMake(0, 0); CALayer * layer= [CALayer layer]; [reLayer addSublayer:layer]; [self.view.layer addSublayer:reLayer]; layer.bounds = CGRectMake(0, 0, 20, 20); layer.position = CGPointMake(30, 100); layer.backgroundColor = [UIColor redColor].CGColor; //每个副本向右平移25px reLayer.instanceTransform=CATransform3DMakeTranslation(25, 0, 0); //如果进行动画,副本延时一秒执行 reLayer.instanceDelay = 1; //拷贝十个副本 reLayer.instanceCount = 10; |
The effect is as follows:
Iv. Cashapelayer
Cashapelayer is a graphics layer, and we can customize the shape of this layer. First look at the properties and methods that we can use:
?
| 1 |
@property(nullable) CGPathRef path; |
The Path property sets a boundary path for Cashapelayer, for example, we can create a path to a triangle by using the following code:
?
| 12345678 |
CAShapeLayer * layer = [CAShapeLayer layer]; layer.position=CGPointMake(0,0); CGMutablePathRef path = CGPathCreateMutable(); CGPathMoveToPoint(path, 0, 100, 100); CGPathAddLineToPoint(path, 0, 300, 100); CGPathAddLineToPoint(path, 0, 200, 200); CGPathAddLineToPoint(path, 0, 100, 100); layer.path=path; |
There are only paths, we cannot draw the shapes we want, and some of the properties below can be set for some of the underlying properties of the graph:
?
| 12345678910111213141516171819202122232425262728293031323334353637383940 |
//设置图形的填充颜色@property(nullable) CGColorRef fillColor;/*设置图形的填充规则 选项如下:非零填充 NSString *const kCAFillRuleNonZero; 奇偶填充 NSString *const kCAFillRuleEvenOdd;*/@property(copy) NSString *fillRule;//设置线条颜色@property(nullable) CGColorRef strokeColor;//设置线条的起点与终点 0-1之间@property CGFloat strokeStart;@property CGFloat strokeEnd;//设置线条宽度@property CGFloat lineWidth;//设置两条线段相交时锐角斜面长度@property CGFloat miterLimit;/*设置线条首尾的外观可选参数如下无形状 NSString *const kCALineCapButt; 圆形 NSString *const kCALineCapRound; 方形 NSString *const kCALineCapSquare;*/@property(copy) NSString *lineCap;/*设置线段的链接方式棱角 NSString *const kCALineJoinMiter; 平滑 NSString *const kCALineJoinRound; 折线 NSString *const kCALineJoinBevel;*/@property(copy) NSString *lineJoin; |
Modify the above code as follows:
?
| 1234567891011121314151617 |
CAShapeLayer * layer = [CAShapeLayer layer]; layer.position=CGPointMake(0,0); CGMutablePathRef path = CGPathCreateMutable(); CGPathMoveToPoint(path, 0, 100, 100); CGPathAddLineToPoint(path, 0, 300, 100); CGPathAddLineToPoint(path, 0, 200, 200); CGPathAddLineToPoint(path, 0, 100, 100); layer.path=path; layer.fillColor= [UIColor redColor].CGColor; layer.fillRule = kCAFillRuleEvenOdd; layer.strokeColor = [UIColor blueColor].CGColor; layer.strokeStart =0; layer.strokeEnd =0.5; layer.lineWidth = 5; layer.miterLimit = 1; layer.lineJoin = kCALineJoinMiter; [self.view.layer addSublayer:layer]; |
The effect is as follows:
In addition, we can also set the boundary lines as dashed lines, through the following two properties:
?
| 1234567 |
&NBSP;&NBSP;&NBSP;&NBSP; //sets the width of the segment to 5px spacing of 10px &NBSP;&NBSP;&NBSP;&NBSP; /* &NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP; */ &NBSP;&NBSP;&NBSP;&NBSP; layer.linedashpattern = @[@05,@10]; &NBSP;&NBSP;&NBSP;&NBSP; //setting from where to start &NBSP;&NBSP;&NBSP;&NBSP; layer.linedashphase =5; |
As follows:
Wu, Catextlayer
Catextlayer can be used to draw text, with the following property methods:
?
| 123456789101112131415161718192021222324252627282930 |
//渲染的文字字符串@property(nullable, copy) id string;//设置字体@property(nullable) CFTypeRef font;//设置字号@property CGFloat fontSize;//设置文字颜色@property(nullable) CGColorRef foregroundColor;//是否换行@property(getter=isWrapped) BOOLwrapped;/*设置截断模式 NSString * const kCATruncationNone; 截断前部分 NSString * const kCATruncationStart; 截断后部分 NSString * const kCATruncationEnd; 截断中间 NSString * const kCATruncationMiddle;*/@property(copy) NSString *truncationMode;/*设置文字对齐模式 NSString * const kCAAlignmentNatural; NSString * const kCAAlignmentLeft; NSString * const kCAAlignmentRight; NSString * const kCAAlignmentCenter; NSString * const kCAAlignmentJustified;*/@property(copy) NSString *alignmentMode; |
iOS Development coreanimation interpretation of the three--the use of several commonly used layer analysis