IOS 屏保動畫

來源:互聯網
上載者:User

標籤:

前言


     最近公司沒項目,我們碼農處於“農閑”的時期,主管說要我找個動畫渲染的引擎,找了許久都沒找合適的,於是就自己動手寫了個自我感覺比較酷炫的屏保效果。
一、控制介面,這個介面沒什麼技術含量,主要作用是用來控制動畫到底有多炫的,比如動畫色彩的豐富度和動畫速度的。溫馨提示,按照提示填入相應的數字,我沒有做越界的處理,你要是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

三、來看看的效果吧


IOS 屏保動畫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.