iOS雪花飘落动效

2017-11-04  本文已影响19人  Luke0407
#import "ViewController.h"

@interface ViewController ()
@property (strong,nonatomic)UIImage * myPic;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.myPic = [UIImage imageNamed:@"snowflower"];//初始化图片
    [self ontime];
    //启动定时器,实现飘雪效果
    [NSTimer scheduledTimerWithTimeInterval:(0.2) target:self selector:@selector(ontime) userInfo:nil repeats:YES];
}
-(void)ontime{
    __block UIImageView *view = [[UIImageView alloc] initWithImage:self.myPic];//声明一个UIImageView对象,用来添加图片
    view.alpha = 1;//设置该view的alpha为0.5,半透明的
    int x = round(random()%320);//随机得到该图片的x坐标
    int y = round(random()%320);//这个是该图片移动的最后坐标x轴的
    int s = round(random()%15)+10;//这个是定义雪花图片的大小
    int sp = 1/round(random()%100)+1;//这个是速度
    view.frame = CGRectMake(x, -50, s, s);//雪花开始的大小和位置
    [self.view addSubview:view];//添加该view
    [UIView animateWithDuration:10*sp animations:^{
        view.frame = CGRectMake(y, 500, s, s);//设定该雪花最后的消失坐标
    } completion:^(BOOL finished) {
        [view removeFromSuperview];
        view = nil;
    }];
}
上一篇 下一篇

猜你喜欢

热点阅读