《30天精通iPhone手机编程》-Day25-雪花

来源:互联网 发布:淘宝助理5.5官方 编辑:程序博客网 时间:2024/05/11 14:13

        CGPointMake 、CGRectMake 、CGSizeMake是iphone os中几何图形动画构建类型的一种,常用在二维控件显示:

CGPoint     CGPointMake(CGFloat x,CGFloat y);

CGRect      CGRectMake(CGFloat x,CGFloat y,CGFloat width,CGFloat height);

CGSize      CGSizeMake(CGFloat width,CGFloat height);

- (void)viewDidLoad {[super viewDidLoad];//定义视图的背景颜色为冷色self.view.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:1.0 alpha:1.0];flakeImage = [UIImage imageNamed:@"flake.png"];//定义定时器[NSTimer scheduledTimerWithTimeInterval:(0.05) target:self selector:@selector(onTimer) userInfo:nil repeats:YES];}//定时期对象选择器方法- (void)onTimer{UIImageView* flakeView = [[UIImageView alloc] initWithImage:flakeImage];//图像的开始坐标xint startX = round(random() % 320);    //图像的结束坐标xint endX = round(random() % 320);    //定义图像的大小范围随机数值double scale = 1 / round(random() % 100) + 1.0;    //定义图像的速度随机值double speed = 1 / round(random() % 100) + 1.0;//定义图像视图开始坐标和尺寸flakeView.frame = CGRectMake(startX, -100.0, 25.0 * scale, 25.0 * scale);flakeView.alpha = 0.25;[self.view addSubview:flakeView];//视图动画开始[UIView beginAnimations:nil context:flakeView];//动画持续时间[UIView setAnimationDuration:5 * speed];//定义图像视图结束时的坐标和尺寸flakeView.frame = CGRectMake(endX, 500.0, 25.0 * scale, 25.0 * scale);//提交动画[UIView commitAnimations];}