前言
最近公司没项目,我们码农处于“农闲”的时期,主管说要我找个动画渲染的引擎,找了许久都没找合适的,于是就自己动手写了个自我感觉比较酷炫的屏保效果。
一、控制界面,这个界面没什么技术含量,主要作用是用来控制动画到底有多炫的,比如动画色彩的丰富度和动画速度的。温馨提示,按照提示填入相应的数字,我没有做越界的处理,你要是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
三、来看看截图的效果吧