0
点赞
收藏
分享

微信扫一扫

iOS 动画 —— 飘落的雪花

- (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);
    // 大小比例和速度 的微调
    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开发范例实战宝典.进阶篇】。

举报

相关推荐

0 条评论