使用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 / 180.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 0;
    }
    
    return (y2 - y1) / (x2 - x1);
}

CGFloat calculateConstant(CGFloat x1, CGFloat y1, CGFloat x2, CGFloat y2) {
    
    if (x2 == x1) {
        
        return 0;
    }
    
    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 == 0) {
        
        return 0;
    }
    
    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(0, 0, 100, 100);
    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(0, 0, 8, 40);
    layer.backgroundColor = [UIColor redColor].CGColor;
    [replicatorLayer addSublayer:layer];
    
    replicatorLayer.instanceCount     = 3;
    CATransform3D transform           = CATransform3DIdentity;
    transform                         = CATransform3DRotate(transform, [Math radianFromDegree:45.f], 0, 0, 1);
    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(0, 0, 100, 100);
    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(0, 0, 8, 40);
    layer.backgroundColor = [UIColor whiteColor].CGColor;
    [replicatorLayer addSublayer:layer];
    
    replicatorLayer.instanceCount       = 8;
    CATransform3D transform             = CATransform3DIdentity;
    transform                           = CATransform3DRotate(transform, [Math radianFromDegree:45.f], 0, 0, 1);
    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(0, 0, 100, 100);
    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(0, 0, 8, 40);
    layer.backgroundColor = [UIColor whiteColor].CGColor;
    [replicatorLayer addSublayer:layer];
    
    replicatorLayer.instanceCount       = 8;
    CATransform3D transform             = CATransform3DIdentity;
    transform                           = CATransform3DRotate(transform, [Math radianFromDegree:45.f], 0, 0, 1);
    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(0, 0, 100, 100);
    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(0, 0, 8, 40);
    layer.backgroundColor = [UIColor whiteColor].CGColor;
    [replicatorLayer addSublayer:layer];
    
    {
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position.y"];
        animation.toValue           = @(layer.position.y - 25.f);
        animation.duration          = 0.5f;
        animation.autoreverses      = true;
        animation.repeatCount       = CGFLOAT_MAX;
        [layer addAnimation:animation forKey:nil];
    }
    
    replicatorLayer.instanceCount       = 0;
    CATransform3D transform             = CATransform3DIdentity;
    transform                           = CATransform3DRotate(transform, [Math radianFromDegree:45.f], 0, 0, 1);
    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           = @(9);
        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 = 100;
    
    CAReplicatorLayer *replicatorLayer = [CAReplicatorLayer layer];
    [self.view.layer addSublayer:replicatorLayer];
    
    replicatorLayer.frame              = CGRectMake(0, 0, width, height);
    replicatorLayer.position           = self.view.center;
    replicatorLayer.borderWidth        = 0.5f;
    replicatorLayer.instanceCount      = width / 3;
    replicatorLayer.masksToBounds      = YES;
    replicatorLayer.instanceTransform  = CATransform3DMakeTranslation(-3.0, 0.0, 0.0);
    replicatorLayer.instanceDelay      = 0.025f;
    
    CALayer *layer        = [CALayer layer];
    layer.frame           = CGRectMake(width - 1, height, 2, 100);
    layer.backgroundColor = [UIColor redColor].CGColor;
    [replicatorLayer addSublayer:layer];
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position.y"];
    animation.toValue           = @(layer.position.y - 60.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 = 100;
    
    CAReplicatorLayer *replicatorLayer = [CAReplicatorLayer layer];
    [self.view.layer addSublayer:replicatorLayer];
    
    replicatorLayer.frame              = CGRectMake(0, 0, width, height);
    replicatorLayer.position           = self.view.center;
    replicatorLayer.borderWidth        = 0.5f;
    replicatorLayer.instanceCount      = width / 3;
    replicatorLayer.masksToBounds      = YES;
    replicatorLayer.instanceTransform  = CATransform3DMakeTranslation(-3.0, 0.0, 0.0);
    replicatorLayer.instanceDelay      = 0.025f;
    
    CALayer *layer        = [CALayer layer];
    layer.frame           = CGRectMake(width - 1, height, 2, 100);
    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() % 100);
        animation.duration          = 0.5f;
        animation.timingFunction    = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        animation.autoreverses      = true;
        animation.repeatCount       = CGFLOAT_MAX;
        [layer addAnimation:animation forKey:nil];
        
    } timeIntervalWithSecs:1.f delaySecs:1.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 = 100;
    
    CAReplicatorLayer *replicatorLayer = [CAReplicatorLayer layer];
    [self.view.layer addSublayer:replicatorLayer];
    
    replicatorLayer.frame              = CGRectMake(0, 0, width, height);
    replicatorLayer.position           = self.view.center;
    replicatorLayer.borderWidth        = 0.5f;
    replicatorLayer.instanceCount      = width / 3;
    replicatorLayer.masksToBounds      = YES;
    replicatorLayer.instanceTransform  = CATransform3DMakeTranslation(-3.0, 0.0, 0.0);
    replicatorLayer.instanceDelay      = 0.5f;
    
    CALayer *layer        = [CALayer layer];
    layer.frame           = CGRectMake(width - 1, height, 2, 100);
    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() % 100);
        animation.duration          = 0.5f;
        animation.timingFunction    = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        animation.autoreverses      = true;
        animation.repeatCount       = CGFLOAT_MAX;
        [layer addAnimation:animation forKey:nil];
        
    } timeIntervalWithSecs:1.f delaySecs:1.f];
    [self.timer start];
}

@end

目录
相关文章
|
消息中间件 弹性计算 运维
重新定义分析 - EventBridge实时事件分析平台发布
为了解决事件领域中针对流式事件做分析的难题,EventBridge 近日发布了针对事件/消息领域的全新分析工具--EventBridge 实时事件分析平台。下面简要对 EventBridge 实时事件分析平台的内容进行介绍。
181 0
重新定义分析 - EventBridge实时事件分析平台发布
|
Android开发 iOS开发 数据库
直播平台制作中的直播间礼物功能开发基本介绍
直播平台之所以能够吸引大量的用户流量,很大程度上受益于它独有的礼物打赏机制。而直播平台中的礼物系统也是平台和主播的重要收益来源。在实际的直播平台制作过程中,礼物系统常见的方案有哪几类呢?礼物又是如何在客户端实现发送的呢?
4171 0
|
iOS开发
【iOS】用strong和weak来修饰成员变量的对比
对于纯代码布局,用@property声明成员变量时,我是很自然的用strong来修饰的。然后突然有人问我用weak来修饰可不可以,我第一反应是不可以,因为用weak来修饰,初始化过后就会被释放掉,就算我第一句写了初始化的方法,立即执行addSubView也是没办法将其添加上去的。
1602 0
|
1天前
|
存储 关系型数据库 MySQL
数据管理的艺术:PolarDB开源版详评与实战部署策略(一)
PolarDB-X是阿里巴巴自研的高性能云原生分布式数据库,基于共享存储的Shared-nothing架构,支持MySQL生态,具备金融级高可用、分布式水平扩展、HTAP混合负载等能力。它通过CN(计算节点)和DN(存储节点)实现计算与存储分离,保证数据强一致性,并支持全局二级索引和多主多写。PolarDB-X开源版提供更高程度的定制化和控制权,适合追求技术自主性和成本优化的开发者。部署方式包括RPM包、PXD工具和Kubernetes,其中PXD工具提供了一键部署的便利性。
35645 10
|
5天前
|
关系型数据库 Serverless 分布式数据库
高峰无忧,探索PolarDB PG版Serverless的弹性魅力
在数字经济时代,数据库成为企业命脉,面对爆炸式增长的数据,企业面临管理挑战。云原生和Serverless技术革新数据库领域,PolarDB PG Serverless作为阿里云的云原生数据库解决方案,融合Serverless与PostgreSQL,实现自动弹性扩展,按需计费,降低运维成本。它通过计算与存储分离技术,提供高可用性、灾备策略和简化运维。PolarDB PG Serverless智能应变业务峰值,实时监控与调整资源,确保性能稳定。通过免费体验,用户可观察其弹性性能和价格力,感受技术优势。
|
15天前
|
存储 缓存 监控
你的Redis真的变慢了吗?性能优化如何做
本文先讲述了Redis变慢的判别方法,后面讲述了如何提升性能。
102247 5
|
15天前
|
机器学习/深度学习 并行计算 算法
Transformer 一起动手编码学原理
学习Transformer,快来跟着作者动手写一个。
94255 8
|
14天前
|
存储 SQL Apache
阿里云数据库内核 Apache Doris 基于 Workload Group 的负载隔离能力解读
阿里云数据库内核 Apache Doris 基于 Workload Group 的负载隔离能力解读
阿里云数据库内核 Apache Doris 基于 Workload Group 的负载隔离能力解读
|
19天前
|
人工智能 弹性计算 算法
一文解读:阿里云AI基础设施的演进与挑战
对于如何更好地释放云上性能助力AIGC应用创新?“阿里云弹性计算为云上客户提供了ECS GPU DeepGPU增强工具包,帮助用户在云上高效地构建AI训练和AI推理基础设施,从而提高算力利用效率。”李鹏介绍到。目前,阿里云ECS DeepGPU已经帮助众多客户实现性能的大幅提升。其中,LLM微调训练场景下性能最高可提升80%,Stable Difussion推理场景下性能最高可提升60%。
124028 50
|
15天前
|
存储 弹性计算 Cloud Native
1 名工程师轻松管理 20 个工作流,创业企业用 Serverless 让数据处理流程提效
为应对挑战,语势科技采用云工作流CloudFlow和函数计算FC,实现数据处理流程的高效管理与弹性伸缩,提升整体研发效能。
64755 2