iOS雪花飄落動效

#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;
    }];
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容