使用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 实时事件分析平台的内容进行介绍。
178 0
重新定义分析 - EventBridge实时事件分析平台发布
|
iOS开发 容器
iOS开发CoreAnimation解读之二——对CALayer的分析(一)
iOS开发CoreAnimation解读之二——对CALayer的分析
116 0
iOS开发CoreAnimation解读之二——对CALayer的分析(一)
|
Android开发 iOS开发 数据库
直播平台制作中的直播间礼物功能开发基本介绍
直播平台之所以能够吸引大量的用户流量,很大程度上受益于它独有的礼物打赏机制。而直播平台中的礼物系统也是平台和主播的重要收益来源。在实际的直播平台制作过程中,礼物系统常见的方案有哪几类呢?礼物又是如何在客户端实现发送的呢?
4155 0
|
iOS开发
【iOS】用strong和weak来修饰成员变量的对比
对于纯代码布局,用@property声明成员变量时,我是很自然的用strong来修饰的。然后突然有人问我用weak来修饰可不可以,我第一反应是不可以,因为用weak来修饰,初始化过后就会被释放掉,就算我第一句写了初始化的方法,立即执行addSubView也是没办法将其添加上去的。
1602 0
|
8天前
|
人工智能 自然语言处理 API
深入浅出LangChain与智能Agent:构建下一代AI助手
LangChain为大型语言模型提供了一种全新的搭建和集成方式,通过这个强大的框架,我们可以将复杂的技术任务简化,让创意和创新更加易于实现。本文从LangChain是什么到LangChain的实际案例到智能体的快速发展做了全面的讲解。
279540 52
深入浅出LangChain与智能Agent:构建下一代AI助手
|
9天前
|
设计模式 人工智能 JSON
一文掌握大模型提示词技巧:从战略到战术
本文将用通俗易懂的语言,带你从战略(宏观)和战术(微观)两个层次掌握大模型提示词的常见技巧,真正做到理论和实践相结合,占领 AI 运用的先机。
237784 4
|
9天前
|
NoSQL Cloud Native Redis
Redis核心开发者的新征程:阿里云与Valkey社区的技术融合与创新
阿里云瑶池数据库团队后续将持续参与Valkey社区,如过往在Redis社区一样耕耘,为开源社区作出持续贡献。
Redis核心开发者的新征程:阿里云与Valkey社区的技术融合与创新
|
9天前
|
关系型数据库 分布式数据库 数据库
PolarDB闪电助攻,《香肠派对》百亿好友关系实现毫秒级查询
PolarDB分布式版助力《香肠派对》实现百亿好友关系20万QPS的毫秒级查询。
PolarDB闪电助攻,《香肠派对》百亿好友关系实现毫秒级查询
|
2天前
|
机器人 Linux API
基于Ollama+AnythingLLM轻松打造本地大模型知识库
Ollama是开源工具,简化了在本地运行大型语言模型(ile优化模型运行,支持GPU使用和热加载。它轻量、易用,可在Mac和Linux上通过Docker快速部署。AnythingLLM是Mintplex Labs的文档聊天机器人,支持多用户、多种文档格式,提供对话和查询模式,内置向量数据库,可高效管理大模型和文档。它也是开源的,能与Ollama结合使用,提供安全、低成本的LLM体验。这两款工具旨在促进本地高效利用和管理LLMs。
72564 19
|
10天前
|
消息中间件 Cloud Native Serverless
RocketMQ 事件驱动:云时代的事件驱动有啥不同?
本文深入探讨了云时代 EDA 的新内涵及它在云时代再次流行的主要驱动力,包括技术驱动力和商业驱动力,随后重点介绍了 RocketMQ 5.0 推出的子产品 EventBridge,并通过几个云时代事件驱动的典型案例,进一步叙述了云时代事件驱动的常见场景和最佳实践。
246778 2