IOS 屏保动画

前言

最近公司没项目,我们码农处于“农闲”的时期,主管说要我找个动画渲染的引擎,找了许久都没找合适的,于是就自己动手写了个自我感觉比较酷炫的屏保效果。

一、控制界面,这个界面没什么技术含量,主要作用是用来控制动画到底有多炫的,比如动画色彩的丰富度和动画速度的。温馨提示,按照提示填入相应的数字,我没有做越界的处理,你要是crash了请不要找我。

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"设置参数";

    UILabel *labelColor = [[UILabel alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 30)];
    labelColor.text = @"  设置颜色丰富度";
    labelColor.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:labelColor];

    //设置R值的范围

    UIView *viewR = [[UIView alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(labelColor.frame)+10, self.view.frame.size.width-20, 70)];
    viewR.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
    viewR.layer.borderWidth = 1.0;
    viewR.layer.cornerRadius = 2.0;
    [self.view addSubview:viewR];

    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 200, 20)];
    title.text = @"请设置R值的范围(0-255)";
    title.textColor = [UIColor grayColor];
    title.font = [UIFont systemFontOfSize:15.0];
    [viewR addSubview:title];

    UILabel *min = [[UILabel alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(title.frame)+10, 70, 25)];
    min.text = @"最小值:";
    min.textColor = [UIColor grayColor];
    min.font = [UIFont systemFontOfSize:17.0];
    [viewR addSubview:min];

    textFieldMinR = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(min.frame), CGRectGetMinY(min.frame), 50, 25)];
    textFieldMinR.layer.borderWidth = 1.0f;
    textFieldMinR.layer.borderColor = [UIColor grayColor].CGColor;
    textFieldMinR.layer.cornerRadius = 2.0;
    textFieldMinR.textAlignment = NSTextAlignmentCenter;
    [viewR addSubview:textFieldMinR];

    UILabel *max = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(textFieldMinR.frame)+50, CGRectGetMaxY(title.frame)+10, 70, 25)];
    max.text = @"最大值:";
    max.textColor = [UIColor grayColor];
    max.font = [UIFont systemFontOfSize:17.0];
    [viewR addSubview:max];

    textFieldMaxR = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(max.frame), CGRectGetMinY(min.frame), 50, 25)];
    textFieldMaxR.layer.borderWidth = 1.0f;
    textFieldMaxR.layer.borderColor = [UIColor grayColor].CGColor;
    textFieldMaxR.layer.cornerRadius = 2.0;
    textFieldMaxR.textAlignment = NSTextAlignmentCenter;
    [viewR addSubview:textFieldMaxR];

    //设置G值的范围

    UIView *viewG = [[UIView alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(viewR.frame)+10, self.view.frame.size.width-20, 70)];
    viewG.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
    viewG.layer.borderWidth = 1.0;
    viewG.layer.cornerRadius = 2.0;
    [self.view addSubview:viewG];

    UILabel *titleG = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 200, 20)];
    titleG.text = @"请设置G值的范围(0-255)";
    titleG.textColor = [UIColor grayColor];
    titleG.font = [UIFont systemFontOfSize:15.0];
    [viewG addSubview:titleG];

    UILabel *minG = [[UILabel alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(titleG.frame)+10, 70, 25)];
    minG.text = @"最小值:";
    minG.textColor = [UIColor grayColor];
    minG.font = [UIFont systemFontOfSize:17.0];
    [viewG addSubview:minG];

    textFieldMinG = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(minG.frame), CGRectGetMinY(minG.frame), 50, 25)];
    textFieldMinG.layer.borderWidth = 1.0f;
    textFieldMinG.layer.borderColor = [UIColor grayColor].CGColor;
    textFieldMinG.layer.cornerRadius = 2.0;
    textFieldMinG.textAlignment = NSTextAlignmentCenter;
    [viewG addSubview:textFieldMinG];

    UILabel *maxG = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(textFieldMinG.frame)+50, CGRectGetMaxY(titleG.frame)+10, 70, 25)];
    maxG.text = @"最大值:";
    maxG.textColor = [UIColor grayColor];
    maxG.font = [UIFont systemFontOfSize:17.0];
    [viewG addSubview:maxG];

    textFieldMaxG = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(maxG.frame), CGRectGetMinY(minG.frame), 50, 25)];
    textFieldMaxG.layer.borderWidth = 1.0f;
    textFieldMaxG.layer.borderColor = [UIColor grayColor].CGColor;
    textFieldMaxG.layer.cornerRadius = 2.0;
    textFieldMaxG.textAlignment = NSTextAlignmentCenter;
    [viewG addSubview:textFieldMaxG];

    //设置B值的范围

    UIView *viewB = [[UIView alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(viewG.frame)+10, self.view.frame.size.width-20, 70)];
    viewB.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
    viewB.layer.borderWidth = 1.0;
    viewB.layer.cornerRadius = 2.0;
    [self.view addSubview:viewB];

    UILabel *titleB = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 200, 20)];
    titleB.text = @"请设置B值的范围(0-255)";
    titleB.textColor = [UIColor grayColor];
    titleB.font = [UIFont systemFontOfSize:15.0];
    [viewB addSubview:titleB];

    UILabel *minB = [[UILabel alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(titleB.frame)+10, 70, 25)];
    minB.text = @"最小值:";
    minB.textColor = [UIColor grayColor];
    minB.font = [UIFont systemFontOfSize:17.0];
    [viewB addSubview:minB];

    textFieldMinB = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(minB.frame), CGRectGetMinY(minB.frame), 50, 25)];
    textFieldMinB.layer.borderWidth = 1.0f;
    textFieldMinB.layer.borderColor = [UIColor grayColor].CGColor;
    textFieldMinB.layer.cornerRadius = 2.0;
    textFieldMinB.textAlignment = NSTextAlignmentCenter;
    [viewB addSubview:textFieldMinB];

    UILabel *maxB = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(textFieldMinB.frame)+50, CGRectGetMaxY(titleB.frame)+10, 70, 25)];
    maxB.text = @"最大值:";
    maxB.textColor = [UIColor grayColor];
    maxB.font = [UIFont systemFontOfSize:17.0];
    [viewB addSubview:maxB];

    textFieldMaxB = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(maxB.frame), CGRectGetMinY(minB.frame), 50, 25)];
    textFieldMaxB.layer.borderWidth = 1.0f;
    textFieldMaxB.layer.borderColor = [UIColor grayColor].<pre name="code" class="objc">#import "AnimationView.h"

@interface AnimationView ()

@end

@implementation AnimationView

@synthesize minR,minG,minB;
@synthesize maxR,maxG,maxB;
@synthesize suduzhi;

- (void)viewDidLoad {
    [super viewDidLoad];

    i = 0;
    j = 0;
    timeNum = 1;

    self.view.backgroundColor = [UIColor blackColor];

   timer = [NSTimer scheduledTimerWithTimeInterval:[suduzhi floatValue] target:self selector:@selector(drawCirle) userInfo:nil repeats:YES];

}

- (void)drawCirle
{
    timeNum = timeNum +1;

    CALayer *waveLayer=[CALayer layer];

    if (timeNum < 100) {
        waveLayer.frame = CGRectMake(self.view.center.x, self.view.center.y, 10, 10);
    }
    else if (timeNum >100 && timeNum <170){
        waveLayer.frame = CGRectMake(i, i*2, 10, 10);
        i = i +5;
    }
    else if (timeNum >170 && timeNum <240){
        waveLayer.frame = CGRectMake(self.view.frame.size.width-j, j*2, 10, 10);
        j = j +5;
    }
    else if (timeNum >240 && timeNum < 400) {
        int x = (arc4random()%300)+20;
        int y = (arc4random()%500)+20;
        waveLayer.frame = CGRectMake(x, y, 10, 10);
    }
    else if (timeNum>400) {
        [timer invalidate];//停止计时器
    }

    //设置颜色

    int rMin = (int)[minR integerValue];
    int rMax = (int)[maxR integerValue];

    int gMin = (int)[minG integerValue];
    int gMax = (int)[maxG integerValue];

    int bMin = (int)[minB integerValue];
    int bMax = (int)[maxB integerValue];

    int redNum = (arc4random()%rMax) + rMin;
    int greenNum = (arc4random()%gMax) + gMin;
    int blueNum = (arc4random()%bMax) + bMin;

    NSLog(@"%d %d %d",redNum,greenNum,blueNum);

    UIColor *color = [UIColor colorWithRed:redNum/255.0 green:greenNum/255.0 blue:blueNum/255.0 alpha:1.0];

    waveLayer.borderColor = color.CGColor;

    waveLayer.borderWidth =0.5;
    waveLayer.cornerRadius =5.0;

    [self.view.layer addSublayer:waveLayer];
    [self scaleBegin:waveLayer];

}

-(void)scaleBegin:(CALayer *)aLayer
{
    float maxScale;

    if (timeNum < 50) {
        maxScale = 50.0;
    }else {
        maxScale = 20.0;
    }

    if (aLayer.transform.m11<maxScale) {
        if (aLayer.transform.m11==1.0) {
            [aLayer setTransform:CATransform3DMakeScale( 1.1, 1.1, 1.0)];

        }else{
            [aLayer setTransform:CATransform3DScale(aLayer.transform, 1.1, 1.1, 1.0)];
        }
        [self performSelector:_cmd withObject:aLayer afterDelay:0.05];
    }else [aLayer removeFromSuperlayer];
}

@end

CGColor; textFieldMaxB.layer.cornerRadius = 2.0; textFieldMaxB.textAlignment = NSTextAlignmentCenter; [viewB addSubview:textFieldMaxB]; //设置动画的快慢 UILabel *labelSudu = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(viewB.frame)+20, self.view.frame.size.width,
25)]; labelSudu.backgroundColor = [UIColor groupTableViewBackgroundColor]; labelSudu.text = @" 设置动画的速度"; [self.view addSubview:labelSudu]; UIView *viewSudu = [[UIView alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(labelSudu.frame)+10, self.view.frame.size.width-20,
70)]; viewSudu.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor; viewSudu.layer.borderWidth = 1.0; viewSudu.layer.cornerRadius = 2.0; [self.view addSubview:viewSudu]; UILabel *titleSudu = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 200,
20)]; titleSudu.text = @"请设置速度值的范围(0.1-0.5)"; titleSudu.textColor = [UIColor grayColor]; titleSudu.font = [UIFont systemFontOfSize:15.0]; [viewSudu addSubview:titleSudu]; UILabel *suduzhi = [[UILabel alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(titleSudu.frame)+10,
70, 25)]; suduzhi.text = @"速度值:"; suduzhi.textColor = [UIColor grayColor]; suduzhi.font = [UIFont systemFontOfSize:17.0]; [viewSudu addSubview:suduzhi]; textFieldSudu = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(suduzhi.frame), CGRectGetMinY(suduzhi.frame),
50, 25)]; textFieldSudu.layer.borderWidth = 1.0f; textFieldSudu.layer.borderColor = [UIColor grayColor].CGColor; textFieldSudu.layer.cornerRadius = 2.0; textFieldSudu.textAlignment = NSTextAlignmentCenter; [viewSudu addSubview:textFieldSudu]; UIButton *btn
= [[UIButton alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(viewSudu.frame)+50, self.view.frame.size.width-20, 35)]; btn.backgroundColor = [UIColor grayColor]; [btn setTitle:@"开始动画" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(btnClick:)
forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; }- (void)btnClick:(id)sender { AnimationView *next = [[AnimationView alloc] init]; next.minR = textFieldMinR.text; next.minG = textFieldMinG.text; next.minB = textFieldMinB.text; next.maxR
= textFieldMaxR.text; next.maxG = textFieldMaxG.text; next.maxB = textFieldMaxB.text; next.suduzhi = textFieldSudu.text; //判断是否有值为填写 if (textFieldMinR.text.length >0 && textFieldMinG.text.length >0 && textFieldMinB.text.length >0 && textFieldMaxR.text.length
>0 && textFieldMaxG.text.length >0 && textFieldMinR.text.length >0 && textFieldMaxB.text.length >0 && textFieldSudu.text.length >0 ) { [self.navigationController pushViewController:next animated:YES]; }else { UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"温馨提示"
message:@"\n 您有内容未填写完整" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alter show]; } }@end


二、动画实现界面,这个界面才是干货。

#import "AnimationView.h"

@interface AnimationView ()

@end

@implementation AnimationView

@synthesize minR,minG,minB;
@synthesize maxR,maxG,maxB;
@synthesize suduzhi;

- (void)viewDidLoad {
    [super viewDidLoad];

    i = 0;
    j = 0;
    timeNum = 1;

    self.view.backgroundColor = [UIColor blackColor];

   timer = [NSTimer scheduledTimerWithTimeInterval:[suduzhi floatValue] target:self selector:@selector(drawCirle) userInfo:nil repeats:YES];

}

- (void)drawCirle
{
    timeNum = timeNum +1;

    CALayer *waveLayer=[CALayer layer];

    if (timeNum < 100) {
        waveLayer.frame = CGRectMake(self.view.center.x, self.view.center.y, 10, 10);
    }
    else if (timeNum >100 && timeNum <170){
        waveLayer.frame = CGRectMake(i, i*2, 10, 10);
        i = i +5;
    }
    else if (timeNum >170 && timeNum <240){
        waveLayer.frame = CGRectMake(self.view.frame.size.width-j, j*2, 10, 10);
        j = j +5;
    }
    else if (timeNum >240 && timeNum < 400) {
        int x = (arc4random()%300)+20;
        int y = (arc4random()%500)+20;
        waveLayer.frame = CGRectMake(x, y, 10, 10);
    }
    else if (timeNum>400) {
        [timer invalidate];//停止计时器
    }

    //设置颜色

    int rMin = (int)[minR integerValue];
    int rMax = (int)[maxR integerValue];

    int gMin = (int)[minG integerValue];
    int gMax = (int)[maxG integerValue];

    int bMin = (int)[minB integerValue];
    int bMax = (int)[maxB integerValue];

    int redNum = (arc4random()%rMax) + rMin;
    int greenNum = (arc4random()%gMax) + gMin;
    int blueNum = (arc4random()%bMax) + bMin;

    NSLog(@"%d %d %d",redNum,greenNum,blueNum);

    UIColor *color = [UIColor colorWithRed:redNum/255.0 green:greenNum/255.0 blue:blueNum/255.0 alpha:1.0];

    waveLayer.borderColor = color.CGColor;

    waveLayer.borderWidth =0.5;
    waveLayer.cornerRadius =5.0;

    [self.view.layer addSublayer:waveLayer];
    [self scaleBegin:waveLayer];

}

-(void)scaleBegin:(CALayer *)aLayer
{
    float maxScale;

    if (timeNum < 50) {
        maxScale = 50.0;
    }else {
        maxScale = 20.0;
    }

    if (aLayer.transform.m11<maxScale) {
        if (aLayer.transform.m11==1.0) {
            [aLayer setTransform:CATransform3DMakeScale( 1.1, 1.1, 1.0)];

        }else{
            [aLayer setTransform:CATransform3DScale(aLayer.transform, 1.1, 1.1, 1.0)];
        }
        [self performSelector:_cmd withObject:aLayer afterDelay:0.05];
    }else [aLayer removeFromSuperlayer];
}

@end

三、来看看截图的效果吧

时间: 2024-11-18 06:19:42

IOS 屏保动画的相关文章

iOS 视图,动画渲染机制探究

腾讯Bugly特约作者:陈向文 终端的开发,首当其冲的就是视图.动画的渲染,切换等等.用户使用 App 时最直接的体验就是这个界面好不好看,动画炫不炫,滑动流不流畅.UI就是 App 的门面,它的体验伴随着用户使用 App 的整个过程.如果UI失败,用户是不会有打开第二次的欲望的. iOS 为开发者提供了丰富的 Framework(UIKit,Core Animation,Core Graphic,OpenGL 等等)来满足开发从上层到底层各种各样的需求.不得不说苹果很牛逼,很多接口你根本不需要

Centos 屏保特效

Centos 屏保特效 提示:必须要确保虚拟机能够上网 安装wget下载工具 yum install wget -y 1.生成动画:2.安装epel源CENTOS6/7根据系统二选一Centos 6:wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo Centos 7:wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/re

CentOS7 屏保 “小火车”

屏保 "小火车" 操作环境:NAT模式 操作: yum -y install wget //生成动画 安装epel源CENTOS6/7根据系统二选一 wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo 安装软件 yum insta

麻友的麻将数字时钟屏保【windows电脑屏幕保护】

软件名称:冷高轮时间麻将数字时钟屏保 软件大小:711KB 软件语言:简体中文 软件类别:桌面工具 软件授权:免费软件 适用平台:WinXP, Win7, Win8, Win10, WinAll 屏保可在官网下载:lenggaolun.com(com前面不是句号是点哦) 安卓版和iOS版APP可自行在各大应用市场搜索全称“冷高轮时间”进行下载. 原文地址:https://www.cnblogs.com/lenggaolun/p/11963692.html

扑克数字时钟屏保

软件名称:冷高轮时间扑克数字时钟屏保 软件大小:752KB 软件语言:简体中文 软件类别:桌面工具 软件授权:免费软件 适用平台:WinXP, Win7, Win8, Win10, WinAll 屏保可在官网下载:lenggaolun.com(com前面不是句号是点哦) 安卓版和iOS版APP可自行在各大应用市场搜索全称“冷高轮时间”进行下载. 原文地址:https://www.cnblogs.com/lenggaolun/p/11963815.html

梵高数字时钟电脑屏保「windows电脑屏幕保护」

软件名称:冷高轮时间梵高数字时钟屏保? 软件大小:585KB软件语言:简体中文软件类别:桌面工具软件授权:免费软件适用平台:WinXP, Win7, Win8, Win10, WinAll屏保可在官网下载:lenggaolun.com(com前面不是句号是点哦)? 安卓版和iOS版APP可自行在各大应用市场搜索全称“冷高轮时间”进行下载. 原文地址:https://www.cnblogs.com/lenggaolun/p/11963586.html

手机屏保临摹制作中遇到的问题

手机屏保临摹,在制作工程中本来使用AI制作了一个图标就是中间圆形的那个种表的样式,可是用AI 制作完成以后保存为EPS格式以后,再一次通过PS打开发现制作的内容不在了变为了一个圆形的白圈,后来有试了几次,还是出现了这个问题,但是在AI中我尝试加了个方形的底色进行保存EPS格式在用PS 打开制作的白图标在的但是无法分离,经过这一个事情我总结出来,全白的AI图标保存为EPS格式再一次被PS打开会变为一片白色,如何解决这个问题呢请指教,谢谢.

iOS关于CoreAnimation动画知识总结

一:UIKit动画 在介绍CoreAnimation动画前先简单介绍一下UIKit动画,大部分简单的动画都可以使用UIKit动画实现,如果想实现更复杂的效果,则需要使用Core Animation了:UIKit动画有两种写法:它不仅可以针对视图还可以针对其它控件: 1:第一种写法是利用属性,结合beginAnimations.commitAnimations -(void)animationOfUIKit { UIView *redView=[[UIView alloc]initWithFram

屏保源码

黑客帝国屏保源码 黑客帝国屏幕保护设置 1. 打开VS2010或以上版本,新建—>项目,Visual C++,win32,选择win32控制台应用程序,名称填写为"hacker". 2. 将一下代码复制到项目源文件中 黑客帝国屏保源码 //数字流星雨 作者:Wicrecend #include "stdafx.h" #include <windows.h> #define ID_TIMER 1 #define STRMAXLEN 25 //一个显示