使用CAReplicatorLayer [2]
使用CAReplicatorLayer [2]

工具类
//
// Math.h
// MathEquation
//
// Created by YouXianMing on 15/11/20.
// Copyright © 2015年 YouXianMing. All rights reserved.
// #import <Foundation/Foundation.h>
#import <UIKit/UIKit.h> struct MATHPoint { CGFloat x;
CGFloat y; }; typedef struct MATHPoint MATHPoint; static inline MATHPoint MATHPointMake(CGFloat x, CGFloat y) { MATHPoint p; p.x = x; p.y = y; return p;
} @interface Math : NSObject #pragma mark - Radian & degree. /**
* Convert radian to degree.
*
* @param radian Radian.
*
* @return Degree.
*/
+ (CGFloat)degreeFromRadian:(CGFloat)radian; /**
* Convert degree to radian.
*
* @param degree Degree.
*
* @return radian.
*/
+ (CGFloat)radianFromDegree:(CGFloat)degree; #pragma mark - Calculate radian. /**
* Radian value from math 'tan' function.
*
* @param sideA Side A
* @param sideB Side B
*
* @return Radian value.
*/
+ (CGFloat)radianValueFromTanSideA:(CGFloat)sideA sideB:(CGFloat)sideB; #pragma mark - Calculate once linear equation (Y = kX + b). @property (nonatomic) CGFloat k;
@property (nonatomic) CGFloat b; /**
* Calculate constant & slope by two math point for once linear equation.
*
* @param pointA Point A.
* @param pointB Point B.
*
* @return Math object.
*/
+ (instancetype)mathOnceLinearEquationWithPointA:(MATHPoint)pointA PointB:(MATHPoint)pointB; /**
* Get X value when Y equal some number.
*
* @param yValue Some number.
*
* @return X number.
*/
- (CGFloat)xValueWhenYEqual:(CGFloat)yValue; /**
* Get Y value when X equal some number.
*
* @param xValue Some number.
*
* @return Y number.
*/
- (CGFloat)yValueWhenXEqual:(CGFloat)xValue; #pragma mark - Reset size. /**
* Get the new size with the fixed width.
*
* @param size Old size.
* @param width The fixed width.
*
* @return New size.
*/
+ (CGSize)resetFromSize:(CGSize)size withFixedWidth:(CGFloat)width; /**
* Get the new size with the fixed height.
*
* @param size Old size.
* @param height The fixed width.
*
* @return New size.
*/
+ (CGSize)resetFromSize:(CGSize)size withFixedHeight:(CGFloat)height; @end
//
// Math.m
// MathEquation
//
// Created by YouXianMing on 15/11/20.
// Copyright © 2015年 YouXianMing. All rights reserved.
// #import "Math.h" @implementation Math + (CGFloat)degreeFromRadian:(CGFloat)radian { return ((radian) * (180.0 / M_PI));
} + (CGFloat)radianFromDegree:(CGFloat)degree { return ((degree) * M_PI / .f);
} + (CGFloat)radianValueFromTanSideA:(CGFloat)sideA sideB:(CGFloat)sideB { return atan2f(sideA, sideB);
} CGFloat calculateSlope(CGFloat x1, CGFloat y1, CGFloat x2, CGFloat y2) { if (x2 == x1) { return ;
} return (y2 - y1) / (x2 - x1);
} CGFloat calculateConstant(CGFloat x1, CGFloat y1, CGFloat x2, CGFloat y2) { if (x2 == x1) { return ;
} return (y1*(x2 - x1) - x1*(y2 - y1)) / (x2 - x1);
} + (instancetype)mathOnceLinearEquationWithPointA:(MATHPoint)pointA PointB:(MATHPoint)pointB { Math *equation = [[[self class] alloc] init]; CGFloat x1 = pointA.x; CGFloat y1 = pointA.y;
CGFloat x2 = pointB.x; CGFloat y2 = pointB.y; equation.k = calculateSlope(x1, y1, x2, y2);
equation.b = calculateConstant(x1, y1, x2, y2); return equation;
} - (CGFloat)xValueWhenYEqual:(CGFloat)yValue { if (_k == ) { return ;
} return (yValue - _b) / _k;
} - (CGFloat)yValueWhenXEqual:(CGFloat)xValue { return _k * xValue + _b;
} + (CGSize)resetFromSize:(CGSize)size withFixedWidth:(CGFloat)width { CGFloat newHeight = size.height * (width / size.width);
CGSize newSize = CGSizeMake(width, newHeight); return newSize;
} + (CGSize)resetFromSize:(CGSize)size withFixedHeight:(CGFloat)height { float newWidth = size.width * (height / size.height);
CGSize newSize = CGSizeMake(newWidth, height); return newSize;
} @end
进行角度旋转

//
// ViewController.m
// CAReplicatorLayer
//
// Created by YouXianMing on 16/1/13.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import "ViewController.h"
#import "Math.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Create CAReplicatorLayer.
CAReplicatorLayer *replicatorLayer = [CAReplicatorLayer layer];
replicatorLayer.frame = CGRectMake(, , , );
replicatorLayer.borderWidth = 0.5f;
replicatorLayer.borderColor = [UIColor blackColor].CGColor;
replicatorLayer.position = self.view.center;
[self.view.layer addSublayer:replicatorLayer]; // Create Layer.
CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(, , , );
layer.backgroundColor = [UIColor redColor].CGColor;
[replicatorLayer addSublayer:layer]; replicatorLayer.instanceCount = ;
CATransform3D transform = CATransform3DIdentity;
transform = CATransform3DRotate(transform, [Math radianFromDegree:.f], , , );
replicatorLayer.instanceTransform = transform;
} @end
进行颜色设置

//
// ViewController.m
// CAReplicatorLayer
//
// Created by YouXianMing on 16/1/13.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import "ViewController.h"
#import "Math.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Create CAReplicatorLayer.
CAReplicatorLayer *replicatorLayer = [CAReplicatorLayer layer];
replicatorLayer.frame = CGRectMake(, , , );
replicatorLayer.borderWidth = 0.5f;
replicatorLayer.borderColor = [UIColor blackColor].CGColor;
replicatorLayer.position = self.view.center;
[self.view.layer addSublayer:replicatorLayer]; // Create Layer.
CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(, , , );
layer.backgroundColor = [UIColor whiteColor].CGColor;
[replicatorLayer addSublayer:layer]; replicatorLayer.instanceCount = ;
CATransform3D transform = CATransform3DIdentity;
transform = CATransform3DRotate(transform, [Math radianFromDegree:.f], , , );
replicatorLayer.instanceTransform = transform;
replicatorLayer.instanceBlueOffset = -0.2;
replicatorLayer.instanceGreenOffset = -0.1;
replicatorLayer.instanceRedOffset = 0.1; } @end
设置第一个对象的颜色

//
// ViewController.m
// CAReplicatorLayer
//
// Created by YouXianMing on 16/1/13.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import "ViewController.h"
#import "Math.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Create CAReplicatorLayer.
CAReplicatorLayer *replicatorLayer = [CAReplicatorLayer layer];
replicatorLayer.frame = CGRectMake(, , , );
replicatorLayer.borderWidth = 0.5f;
replicatorLayer.borderColor = [UIColor blackColor].CGColor;
replicatorLayer.position = self.view.center;
[self.view.layer addSublayer:replicatorLayer]; // Create Layer.
CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(, , , );
layer.backgroundColor = [UIColor whiteColor].CGColor;
[replicatorLayer addSublayer:layer]; replicatorLayer.instanceCount = ;
CATransform3D transform = CATransform3DIdentity;
transform = CATransform3DRotate(transform, [Math radianFromDegree:.f], , , );
replicatorLayer.instanceTransform = transform;
replicatorLayer.instanceColor = [[UIColor redColor] colorWithAlphaComponent:0.3f].CGColor;
replicatorLayer.instanceBlueOffset = -0.3f;
replicatorLayer.instanceGreenOffset = -0.3f;
replicatorLayer.instanceRedOffset = -0.3f;
} @end
综合使用

//
// ViewController.m
// CAReplicatorLayer
//
// Created by YouXianMing on 16/1/13.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import "ViewController.h"
#import "Math.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Create CAReplicatorLayer.
CAReplicatorLayer *replicatorLayer = [CAReplicatorLayer layer];
replicatorLayer.frame = CGRectMake(, , , );
replicatorLayer.borderWidth = 0.5f;
replicatorLayer.borderColor = [UIColor blackColor].CGColor;
replicatorLayer.position = self.view.center;
[self.view.layer addSublayer:replicatorLayer]; // Create Layer.
CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(, , , );
layer.backgroundColor = [UIColor whiteColor].CGColor;
[replicatorLayer addSublayer:layer]; {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position.y"];
animation.toValue = @(layer.position.y - .f);
animation.duration = 0.5f;
animation.autoreverses = true;
animation.repeatCount = CGFLOAT_MAX;
[layer addAnimation:animation forKey:nil];
} replicatorLayer.instanceCount = ;
CATransform3D transform = CATransform3DIdentity;
transform = CATransform3DRotate(transform, [Math radianFromDegree:.f], , , );
replicatorLayer.instanceTransform = transform;
replicatorLayer.instanceColor = [[UIColor redColor] colorWithAlphaComponent:0.3f].CGColor;
replicatorLayer.instanceBlueOffset = -0.3f;
replicatorLayer.instanceGreenOffset = -0.3f;
replicatorLayer.instanceRedOffset = -0.3f;
replicatorLayer.instanceDelay = 0.1f; {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"instanceCount"];
animation.fromValue = @(replicatorLayer.instanceCount);
animation.toValue = @();
animation.duration = 0.3f;
animation.autoreverses = true;
animation.repeatCount = CGFLOAT_MAX;
[replicatorLayer addAnimation:animation forKey:nil];
}
} @end

//
// ViewController.m
// CAReplicatorLayer
//
// Created by YouXianMing on 16/1/13.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; CGFloat width = self.view.frame.size.width;
CGFloat height = ; CAReplicatorLayer *replicatorLayer = [CAReplicatorLayer layer];
[self.view.layer addSublayer:replicatorLayer]; replicatorLayer.frame = CGRectMake(, , width, height);
replicatorLayer.position = self.view.center;
replicatorLayer.borderWidth = 0.5f;
replicatorLayer.instanceCount = width / ;
replicatorLayer.masksToBounds = YES;
replicatorLayer.instanceTransform = CATransform3DMakeTranslation(-3.0, 0.0, 0.0);
replicatorLayer.instanceDelay = 0.025f; CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(width - , height, , );
layer.backgroundColor = [UIColor redColor].CGColor;
[replicatorLayer addSublayer:layer]; CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position.y"];
animation.toValue = @(layer.position.y - .f);
animation.duration = 0.5f;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.autoreverses = true;
animation.repeatCount = CGFLOAT_MAX;
[layer addAnimation:animation forKey:nil];
} @end

https://github.com/YouXianMing/GCD-Program
//
// ViewController.m
// CAReplicatorLayer
//
// Created by YouXianMing on 16/1/13.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import "ViewController.h"
#import "GCD.h" @interface ViewController () @property (nonatomic, strong) GCDTimer *timer; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; CGFloat width = self.view.frame.size.width;
CGFloat height = ; CAReplicatorLayer *replicatorLayer = [CAReplicatorLayer layer];
[self.view.layer addSublayer:replicatorLayer]; replicatorLayer.frame = CGRectMake(, , width, height);
replicatorLayer.position = self.view.center;
replicatorLayer.borderWidth = 0.5f;
replicatorLayer.instanceCount = width / ;
replicatorLayer.masksToBounds = YES;
replicatorLayer.instanceTransform = CATransform3DMakeTranslation(-3.0, 0.0, 0.0);
replicatorLayer.instanceDelay = 0.025f; CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(width - , height, , );
layer.backgroundColor = [UIColor redColor].CGColor;
[replicatorLayer addSublayer:layer]; self.timer = [[GCDTimer alloc] initInQueue:[GCDQueue mainQueue]];
[self.timer event:^{ CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position.y"];
animation.toValue = @(layer.position.y - arc4random() % );
animation.duration = 0.5f;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.autoreverses = true;
animation.repeatCount = CGFLOAT_MAX;
[layer addAnimation:animation forKey:nil]; } timeIntervalWithSecs:.f delaySecs:.f];
[self.timer start];
} @end

//
// ViewController.m
// CAReplicatorLayer
//
// Created by YouXianMing on 16/1/13.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import "ViewController.h"
#import "Math.h"
#import "GCD.h" @interface ViewController () @property (nonatomic, strong) GCDTimer *timer; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; CGFloat width = self.view.frame.size.width;
CGFloat height = ; CAReplicatorLayer *replicatorLayer = [CAReplicatorLayer layer];
[self.view.layer addSublayer:replicatorLayer]; replicatorLayer.frame = CGRectMake(, , width, height);
replicatorLayer.position = self.view.center;
replicatorLayer.borderWidth = 0.5f;
replicatorLayer.instanceCount = width / ;
replicatorLayer.masksToBounds = YES;
replicatorLayer.instanceTransform = CATransform3DMakeTranslation(-3.0, 0.0, 0.0);
replicatorLayer.instanceDelay = 0.5f; CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(width - , height, , );
layer.backgroundColor = [UIColor redColor].CGColor;
[replicatorLayer addSublayer:layer]; self.timer = [[GCDTimer alloc] initInQueue:[GCDQueue mainQueue]];
[self.timer event:^{ CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position.y"];
animation.toValue = @(layer.position.y - arc4random() % );
animation.duration = 0.5f;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.autoreverses = true;
animation.repeatCount = CGFLOAT_MAX;
[layer addAnimation:animation forKey:nil]; } timeIntervalWithSecs:.f delaySecs:.f];
[self.timer start];
} @end
使用CAReplicatorLayer [2]的更多相关文章
- 利用CAReplicatorLayer实现的加载动画
在上一篇中,笔者简要介绍了CAReplicatorLayer,在本篇中,将介绍具体的实用价值. 实用CAReplicatorLayer作为核心技术实现加载动画. 首先,创建一个UIView的子类 @i ...
- CoreAnimation 之CAReplicatorLayer
CAReplicatorLayer: 主要作用有以下两个: CAReplicatorLayer的目的是为了高效生成许多相似的图层,它会绘制一个或多个图层的子图层 并在每个复制体上应用不同的变换 使用C ...
- CA*Layer(CAReplicatorLayer--)
CAReplicatorLayer (反射应用) 指定一个继承于UIView的ReflectionView,它会自动产生内容的反射效果: + (Class)layerClass//我们也可以通过重写V ...
- CAReplicatorLayer复制Layer和动画, 实现神奇的效果
今天我们看下CAReplicatorLayer, 官方的解释是一个高效处理复制图层的中间层.他能复制图层的所有属性,包括动画. 一样我们先看下头文件 @interface CAReplicatorLa ...
- iOS CAReplicatorLayer 实现脉冲动画效果
iOS CAReplicatorLayer 实现脉冲动画效果 效果图 脉冲数量.速度.半径.透明度.渐变颜色.方向等都可以设置.可以用于地图标注(Annotation).按钮长按动画效果(例如录音按钮 ...
- 用drawRect以及CAReplicatorLayer绘制动态水波纹
用drawRect以及CAReplicatorLayer绘制动态水波纹 大大简化了写水波纹效果的难度,你可以根据示例自己组装水波纹效果,本设计是几个工具组合在一起完成的效果, DrawRectObje ...
- CAReplicatorLayer
CAReplicatorLayer CAReplicatorLayer的目的是为了高效生成许多相似的图层.它会绘制一个或多个图层的子图层,并在每个复制体上应用不同的变换.看上去演示能够更加解释这些,我 ...
- 使用CAReplicatorLayer [1]
使用CAReplicatorLayer [1] 说明 https://developer.apple.com/library/ios/documentation/GraphicsImaging/Ref ...
- iOS CAReplicatorLayer 简单动画
代码地址如下:http://www.demodashi.com/demo/11601.html 写在最前面,最近在看学习的时候,偶然间发现一个没有用过的Layer,于是抽空研究了下,本来应该能提前记录 ...
随机推荐
- JDK的spi实现
SPI的全名为Service Provider Interface.大多数开发人员可能不熟悉,因为这个是针对厂商或者插件的.在java.util.ServiceLoader的文档里有比较详细的介绍.简 ...
- HTML自己整理
1.margin:0 auto 表示什么意思?? margin后面如果只有两个参数的话,第一个表示top和bottom,第二个表示left和right因为0 auto,表示上下边界为0,左右则根据宽度 ...
- 笔记四:python乱码深度剖析二
一:学习内容 获取更改系统编码 判断字符的编码类型 文件存储和读取的编码 二:获取更改系统编码 1. 获取系统编码 import sys print sys.getdefaultencoding() ...
- 从weblogic的一个教训
部署后一定要检查解压后的文件是否修改了.常常出现部署中存在缓存的情况. weblogic8.1可能出现没有删除缓存情况.血的教训.
- [转]ASP.NET Core 指定环境发布(hosting environment)
本文转自:https://www.cnblogs.com/xishuai/p/asp-net-core-set-hosting-environment-with-publish.html ASP.NE ...
- asp查找</td>标记时遇到以外的文件结尾
正确的<td></td> 错误的<td> 没有写完整
- Java Swing实战(二)下拉菜单组件JComboBox及其事件监听
接下来给"数据源配置"面板添加下拉框. /** * @author: lishuai * @date: 2018/11/26 13:51 */ public class Weimi ...
- mysql information_schema介绍
mysql information_schema介绍 一.information_schema是什么 information_schema是MySQL自带的一个信息数据库,其保存着关于MySQL服务器 ...
- 对工厂方法模式的一些思考(java语法表示)
同为创造型设计模式的简单工厂模式可以理解为对new关键字的代替. 本着重复三次即重构的原则,如果一个对象在不同的地方被new了两次以上,那就可以考虑使用它.那我们为什么要用简单工厂模式代替new呢?就 ...
- 深入理解MyBatis的原理(一): 独立的入门demo
前言:不结合spring,只有 mybatis+maven.数据库使用 oracle.不尝试永远不知道会发生什么事,其中遇到两个小问题,也记录下来了.转载请注明出处:https://www.cnblo ...