程序员人生 网站导航

IOS 屏保动画

栏目:综合技术时间:2015-05-20 11:19:01

前言


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

1、控制界面,这个界面没甚么技术含量,主要作用是用来控制动画到底有多炫的,比如动画色采的丰富度和动画速度的。温馨提示,依照提示填入相应的数字,我没有做越界的处理,你要是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⑵0, 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⑵55)"; 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⑵0, 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⑵55)"; 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⑵0, 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⑵55)"; 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



2、动画实现界面,这个界面才是干货。
#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

3、来看看截图的效果吧



Demo下载地址:http://download.csdn.net/detail/u010545480/8647867

------分隔线----------------------------
------分隔线----------------------------

最新技术推荐