13重用雪花、跑马灯、打飞机_CGRectIntersectsRect_UITouch

一、重用版雪花下落
1.1 重用1
项目:Homework_20FallingSnow_Teacher0308

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor grayColor];
    [self.window makeKeyAndVisible];
    /**
     *  重用版雪花下落
        1.创建多片雪花,通过for循环创建,存放到数组中
        2.创建定时器,遍历雪花数组,判断雪花是否可以使用,判断标准:是否正在下落。可以使用就执行UIView动画
        3.通过继承,创建一个UIImageView的子类,添加一个表示雪花状态的属性(BOOL)
        4.动画结束,恢复雪花初始状态
     */
    //1.创建多片雪花
    NSMutableArray *snowArray = [[NSMutableArray alloc]initWithCapacity:20];
    
    for (int i = 0; i < 20; i++)
    {
        //随机 雪花宽高
        _widthAndHeight = arc4random()%30 + 20;
        //随机 x坐标
        float begin_x = arc4random()%(321-(int)_widthAndHeight);
        ZYImageView *snow = [[ZYImageView alloc]initWithFrame:CGRectMake(begin_x, -_widthAndHeight, _widthAndHeight, _widthAndHeight)];
        snow.image = [UIImage imageNamed:@"flake"];
        snow.isDown = NO;
        [snowArray addObject:snow];
        [self.window addSubview:snow];
    }
    //2.创建定时器
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(findAndFallSnow:) userInfo:snowArray repeats:YES];
    return YES;
}
//找到雪花并使其下落
- (void)findAndFallSnow:(NSTimer *)timer
{
    NSMutableArray *snowArray = timer.userInfo;
    
    for (ZYImageView *snow in snowArray)
    {
        if (snow.isDown == NO)
        {
            snow.isDown = YES;
            //下落
            [UIView beginAnimations:nil context:(__bridge void *)snow];
            [UIView setAnimationDuration:3];
            //随机一个终点x坐标
            float end_x = arc4random()%(321-(int)_widthAndHeight);
            snow.frame = CGRectMake(end_x, SCREEN_HEIGHT-_widthAndHeight, _widthAndHeight, _widthAndHeight);
            //调用结束方法,改变透明度
            [UIView setAnimationDelegate:self];
            [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
            [UIView commitAnimations];
            //跳出整个for循环
            break;
        }
        
    }
}
//结束方法,改变透明度
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
    ZYImageView *snow = (__bridge ZYImageView *)context;
    [UIView beginAnimations:nil context:(__bridge void *)snow];
    
    [UIView setAnimationDuration:3];
    snow.alpha = 0;
    //调用结束方法,恢复初始状态
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop1:finished:context:)];
    
    [UIView commitAnimations];
}
//结束方法,恢复初始状态
- (void)animationDidStop1:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
    ZYImageView *snow = (__bridge ZYImageView *)context;
    //让雪花回到初始状态
    snow.frame = CGRectMake(arc4random()%320, -_widthAndHeight, _widthAndHeight, _widthAndHeight);
    snow.isDown = NO;
    snow.alpha = 1;
}

1.2 重用2 使用animationID属性
项目:Homework_20FallingSnow_UseAnimationID_Teacher0308

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor grayColor];
    [self.window makeKeyAndVisible];
    
    //1.创建雪花
    UIImageView *snow = [[UIImageView alloc]initWithFrame:CGRectMake(100, -60, 60, 60)];
    snow.image = [UIImage imageNamed:@"flake"];
    snow.tag = 1;
    [self.window addSubview:snow];
    //2.开启定时器
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(snowMove:) userInfo:snow repeats:YES];
    return YES;
}

- (void)snowMove:(NSTimer *)timer
{
    UIImageView *snow = timer.userInfo;
    if (snow.tag == 1)
    {
        snow.tag = 2;//表示正在下落
        //下落
        [UIView beginAnimations:@"雪花下落" context:(__bridge void *)snow];
        
        [UIView setAnimationDuration:3];
        snow.frame = CGRectMake(200, 420, 60, 60);
        
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
        
        [UIView commitAnimations];
    }
}
//动画结束,根据animationID来确定执行的动作
//两种情况,雪花下落、雪花融化
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
    UIImageView *snow = (__bridge UIImageView *)context;
    if ([animationID isEqualToString:@"雪花下落"])
    {
        [UIView beginAnimations:@"雪花融化" context:(__bridge void *)snow];
        [UIView setAnimationDuration:1];
        snow.alpha = 0;
        
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
        
        [UIView commitAnimations];
    }
    if ([animationID isEqualToString:@"雪花融化"])
    {
        //恢复初始状态
        snow.frame = CGRectMake(200, -60, 60, 60);
        snow.tag = 1;
        snow.alpha = 1;
    }
}

二、跑马灯,反向运动
项目:Marquee_UIView0308

    //设置反向运动的方法
    [UIView setAnimationRepeatAutoreverses:YES];
    //重复次数
    [UIView setAnimationRepeatCount:LONG_MAX];

三、飞机游戏
项目:Plane_TouchAction0308

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 60, 60)];
    _imageView.backgroundColor = [UIColor redColor];
    [self.window addSubview:_imageView];
    
    //判断两个矩形块儿是否交叉
    //CGRectIntersectsRect(CGRect rect1, CGRect rect2);
    return YES;
}
//拖动事件
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    //1.获取touch事件
    UITouch *touch = [touches anyObject];
    //2.获取触摸的点坐标
    CGPoint point = [touch locationInView:self.window];
    //3.将坐标赋给图片
    //在矩形块儿上拖拽
    if (CGRectContainsPoint(_imageView.frame, point)) 
 {
        _imageView.center = point;
    }
}

//    NSMutableArray *myPlaneArray = [[NSMutableArray alloc]initWithCapacity:2];
//    for (int i =0; i<2; i++)
//    {
//        NSString *name = [NSString stringWithFormat:@"plane%d",i];
//        UIImage *planeImg = [UIImage imageNamed:name];
//        [myPlaneArray addObject:planeImg];
//    }
//    myPlane.planeBody = [[UIImageView alloc]initWithFrame:CGRectMake(200, 200, 100, 100)];
//    myPlane.planeBody.animationImages = myPlaneArray;
//    myPlane.planeBody.animationDuration = 1;
//    myPlane.planeBody.animationRepeatCount = 0;
//    [self.window addSubview:myPlane.planeBody];

你可能感兴趣的:(13重用雪花、跑马灯、打飞机_CGRectIntersectsRect_UITouch)