iOS 動畫這塊還是很有意思的骗村,本人決定以小 Demo 形式開始學(xué)習(xí)動畫的路程。
GIF 為1秒的反復(fù)進行
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.backImageView];
[NSTimer scheduledTimerWithTimeInterval:(0.05f) target:self selector:@selector(fallingSnows) userInfo:nil repeats:YES];
}
- (void)fallingSnows {
// 圖片
UIImageView* fallingSnowView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"snow"]];
// 位置(橫向距離的改變)
CGFloat startX = roundf(random() % (int)SCREEN_WIDTH);
CGFloat endX = roundf(random() % (int)SCREEN_WIDTH);
// 大小比例和速度 的微調(diào)
CGFloat scale = 1 / round(random() % 100) + 1.0;
CGFloat speed = 1 / round(random() % 100) + 1.0;
// 大小和透明
fallingSnowView.frame = CGRectMake(startX, -100.0, 25.0 * scale, 25.0 * scale);
fallingSnowView.alpha = 0.5;
[self.view addSubview:fallingSnowView];
// 動畫效果
[UIView animateWithDuration:5*speed animations:^{
fallingSnowView.frame = CGRectMake(endX, SCREEN_HEIGHT, 25.0 * scale, 25.0 * scale);
}];
}
飄落的雪花核心代碼如上枣耀,相對來說,比較簡單庭再,小小記錄下捞奕。
PS:場景圖片來自【iOS開發(fā)范例實戰(zhàn)寶典.進階篇】牺堰。