iOS学习笔记(五)————雪花下落

/ 0评 / 0

来总结一下前两天学的东西,做一个雪花下落的效果

xuehuaxialuo

这个就是效果图,雪花灰不断的从上飘落下来,并且落到底之后就会消失,先来看一下第一种方法:

- (void)snowDown{
    //    1.定时器
    NSTimer *test = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(snowDownTimer) userInfo:nil repeats:YES] ;
    //不等待第一次时间间隔
    [test fire];
    self.view.backgroundColor = [UIColor blackColor];
}

- (void)snowDownTimer{ 
    //    2.创建雪花视图的对象,位置随机,大小50以内随机
    int i = arc4random()%321;
    int j = arc4random()%321;
    
    int x = arc4random()%50;

    UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"flake"]];
    image.frame = CGRectMake(i, -80, x, x);
    [self.view addSubview:image];
    //    3.UIView动画
    [UIView beginAnimations:nil context:(__bridge void *)(image)];
    //    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:8];
    
    //
    image.frame = CGRectMake(j, kDeviceSize.height, x, x);
    
    //    image.alpha = 0;
    
    //    4.移除雪花对象
    
    //    设置代理
    [UIView setAnimationDelegate:self];
    //    动画结束后调用的方法
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    [UIView commitAnimations];
}

//动画结束后调用
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context{
    UIImageView *view = (__bridge UIImageView *)context;
    [view removeFromSuperview];
}

此方法没有复用图片对象,比较消耗内存,所以不推荐,下面我们说一下另一种方法,用到了重用的机制,在每个雪花上添加一个BOOL类型的属性,判断是否使用过,没有使用的可以拿出来放在界面上,使用过的就回收,重用机制在以后用的很多,这里我们先接触一下,话不多说,上代码!

首先自定义一个DCImageView,继承于UIImageView,添加一个波尔类型的属性isUse。

@interface DCImageView : UIImageView
@property BOOL isUse;
@end


@interface ViewController (){
    NSMutableArray *imageArr;
}
@end


- (void)newSnowDown{
    //   初始化雪花数组
    imageArr= [[NSMutableArray alloc] initWithCapacity:0];
    for (int j = 0; j < 40; j ++) {
        int i = arc4random()%321;
        int x = arc4random()%50;
        DCImageView *view = [[DCImageView alloc] initWithImage:[UIImage imageNamed:@"flake"]];
        view.frame = CGRectMake(i, -50, x, x);
        [self.view addSubview:view];
        [imageArr addObject:view];
    }
    
    
    // 定时器
    NSTimer *time = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(newSnowDownTimer) userInfo:nil repeats:YES];
    [time fire];
    self.view.backgroundColor = [UIColor blackColor];
}

static int count = 0;

- (void)newSnowDownTimer{
    count ++;
    //    if (count%20 == 0) {
    if (count == 40) {
        //       找雪花
        [self findSnow];
        //        让count值++,当等于20的时候找一次雪花
        //        定时器不断的执行count值会大于20,就再也找不到雪花了,所以要把count值重新归0
        count = 0;
    }
    [self downSnow];
}


- (void)findSnow{
    for(int i = 0; i= kDeviceSize.height) {
                view.isUse = NO;
            }
        }
    }
}

//动画结束后调用
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context{
    DCImageView *view = (__bridge DCImageView *)context;
    [view removeFromSuperview];
}

就是这么简单~

代码请查看 http://git.oschina.net/zcb1603999/LearningiOS

评论已关闭。