使用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,于是抽空研究了下,本来应该能提前记录 ...
随机推荐
- 梯度下降法的三种形式-BGD、SGD、MBGD
在应用机器学习算法时,我们通常采用梯度下降法来对采用的算法进行训练.其实,常用的梯度下降法还具体包含有三种不同的形式,它们也各自有着不同的优缺点. 下面我们以线性回归算法来对三种梯度下降法进行比较. ...
- cordova打包APK,报错:Cannot evaluate module CordovaLib : Configuration with name 'debug' not found.
原因:之前做其他项目的时候把环境(gradle)升级了. 解决方案:将gradle降低回原来的版本.
- c#读取html文件内容替换之后再写入
string sss = File.ReadAllText("E:\\FM\\Mall\\MallSGWeb\\MallSGWeb\\MALL_simple\\File\\11111.htm ...
- FacebookFriendAdderPro
Windows Registry Editor Version 5.00 [HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\Fe ...
- SpringMVC 面试题
SpringMVC 面试题 什么是Spring MVC ?简单介绍下你对springMVC的理解? Spring MVC是一个基于MVC架构的用来简化web应用程序开发的应用开发框架,它是Spring ...
- winform:对dataGridView绑定的泛型List<T> 的简单CRUD
创建对象类,为所有成员封装字段,然后重载该类: 根据已有的对象类(类型参数)创建一个长度可以变化的List数组,并绑定数据源: 设置dataGridView的column属性:对应四个对象类创建相 ...
- 请比较throw 合throws的区别
throw语句用在方法体内,表示抛出异常.throws语句用在方法声明的后面,表示再抛出异常,由该方法的调用者来处理.throws主要声明这个方法会抛出这种类型的异常,使它的调用者知道要捕获这个异常. ...
- hdu 2612
Find a way Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- java,JsonFormat格式化日期问题
今天使用以下代码格式日期字段 @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") private Date createTime; 发现这样渲 ...
- 【PyQt5 学习记录】009:批量创建组件并查找
这里需要在创建组件时使用 setObjectName() 为其添加 objectName 属性,再通过 findChild() 或者 findChildren() 函数来查找. 举个栗子,批量创建10 ...