使用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

目录
相关文章
|
13天前
|
供应链 监控 安全
对话|企业如何构建更完善的容器供应链安全防护体系
阿里云与企业共筑容器供应链安全
171329 12
|
16天前
|
供应链 监控 安全
对话|企业如何构建更完善的容器供应链安全防护体系
随着云计算和DevOps的兴起,容器技术和自动化在软件开发中扮演着愈发重要的角色,但也带来了新的安全挑战。阿里云针对这些挑战,组织了一场关于云上安全的深度访谈,邀请了内部专家穆寰、匡大虎和黄竹刚,深入探讨了容器安全与软件供应链安全的关系,分析了当前的安全隐患及应对策略,并介绍了阿里云提供的安全解决方案,包括容器镜像服务ACR、容器服务ACK、网格服务ASM等,旨在帮助企业构建涵盖整个软件开发生命周期的安全防护体系。通过加强基础设施安全性、技术创新以及倡导协同安全理念,阿里云致力于与客户共同建设更加安全可靠的软件供应链环境。
150295 32
|
24天前
|
弹性计算 人工智能 安全
对话 | ECS如何构筑企业上云的第一道安全防线
随着中小企业加速上云,数据泄露、网络攻击等安全威胁日益严重。阿里云推出深度访谈栏目,汇聚产品技术专家,探讨云上安全问题及应对策略。首期节目聚焦ECS安全性,提出三道防线:数据安全、网络安全和身份认证与权限管理,确保用户在云端的数据主权和业务稳定。此外,阿里云还推出了“ECS 99套餐”,以高性价比提供全面的安全保障,帮助中小企业安全上云。
201960 14
对话 | ECS如何构筑企业上云的第一道安全防线
|
6天前
|
存储 人工智能 安全
对话|无影如何助力企业构建办公安全防护体系
阿里云无影助力企业构建办公安全防护体系
1251 8
|
2天前
|
机器学习/深度学习 自然语言处理 PyTorch
深入剖析Transformer架构中的多头注意力机制
多头注意力机制(Multi-Head Attention)是Transformer模型中的核心组件,通过并行运行多个独立的注意力机制,捕捉输入序列中不同子空间的语义关联。每个“头”独立处理Query、Key和Value矩阵,经过缩放点积注意力运算后,所有头的输出被拼接并通过线性层融合,最终生成更全面的表示。多头注意力不仅增强了模型对复杂依赖关系的理解,还在自然语言处理任务如机器翻译和阅读理解中表现出色。通过多头自注意力机制,模型在同一序列内部进行多角度的注意力计算,进一步提升了表达能力和泛化性能。
|
7天前
|
人工智能 自然语言处理 程序员
通义灵码2.0全新升级,AI程序员全面开放使用
通义灵码2.0来了,成为全球首个同时上线JetBrains和VSCode的AI 程序员产品!立即下载更新最新插件使用。
1275 23
|
8天前
|
机器学习/深度学习 自然语言处理 搜索推荐
自注意力机制全解析:从原理到计算细节,一文尽览!
自注意力机制(Self-Attention)最早可追溯至20世纪70年代的神经网络研究,但直到2017年Google Brain团队提出Transformer架构后才广泛应用于深度学习。它通过计算序列内部元素间的相关性,捕捉复杂依赖关系,并支持并行化训练,显著提升了处理长文本和序列数据的能力。相比传统的RNN、LSTM和GRU,自注意力机制在自然语言处理(NLP)、计算机视觉、语音识别及推荐系统等领域展现出卓越性能。其核心步骤包括生成查询(Q)、键(K)和值(V)向量,计算缩放点积注意力得分,应用Softmax归一化,以及加权求和生成输出。自注意力机制提高了模型的表达能力,带来了更精准的服务。
|
6天前
|
消息中间件 人工智能 运维
1月更文特别场——寻找用云高手,分享云&AI实践
我们寻找你,用云高手,欢迎分享你的真知灼见!
520 22
1月更文特别场——寻找用云高手,分享云&AI实践
|
6天前
|
机器学习/深度学习 人工智能 自然语言处理
|
12天前
|
人工智能 自然语言处理 API
阿里云百炼xWaytoAGI共学课DAY1 - 必须了解的企业级AI应用开发知识点
本课程旨在介绍阿里云百炼大模型平台的核心功能和应用场景,帮助开发者和技术小白快速上手,体验AI的强大能力,并探索企业级AI应用开发的可能性。