小球弹跳:UI2-2

//

//  AppDelegate.m

//  Demo-UI2-2

//

//  Created by dllo on 15/3/5.

//  Copyright (c) 2015 luojin. All rights reserved.

//


#import "AppDelegate.h"


@interface AppDelegate ()


@end


@implementation AppDelegate


- (void)dealloc

{

    [_block release];

    

    [super dealloc];

}



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

    

    _yVelocity = 1.0;

    _xVelocity = 1.0;

    

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    [self.window makeKeyAndVisible];

    

    /*

     CGRect rect2 = CGRectMake(200, 80, 50, 50);

     UIView *bView = [[[UIView alloc] initWithFrame:rect2] autorelease];

     [bView setBackgroundColor:[UIColor greenColor]];

     [self.window addSubview:bView];

     [bView setTag:200];

     */

    CGRect rect = CGRectMake(40, 40, 50, 50);

    UIView *aView = [[[UIView alloc] initWithFrame:rect] autorelease];

    [self.window addSubview:aView];

    [aView setBackgroundColor:[UIColor redColor]];

    [aView setTag:100];

    //成圆

    [aView.layer setCornerRadius:25];

    

    //用定时器定时调用改变AView位置的方法

    [NSTimer scheduledTimerWithTimeInterval:0.003 target:self selector:@selector(changePosition) userInfo:nil repeats:YES];

    

    //设置一个固定不动的视图, 作为障碍物,若碰撞则aView停止运动

    //创建block

    _block = [[UIView alloc] initWithFrame:CGRectMake(160, 250, 50, 50)];

    [_block setBackgroundColor:[UIColor cyanColor]];

    [self.window addSubview:_block];


   

    return YES;

}




//改变AView位置的方法

- (void)changePosition

{

    UIView *aView = [self.window viewWithTag:100];

    

   //视图向下移动一个像素

    CGRect rect = [aView frame];

    rect.origin.y += _yVelocity;

    rect.origin.x += _xVelocity;

    [aView setFrame:rect];


    

    //碰撞上边界

    if (rect.origin.y + rect.size.height == self.window.bounds.size.height) {

        _yVelocity = -1.0;

    }

    //碰撞下边界

    if (rect.origin.y == 0) {

        _yVelocity = 1.0;

    }

    //碰撞左边界

    if (rect.origin.x + rect.size.width == self.window.bounds.size.width) {

        _xVelocity = -1.0;

    }

    //碰撞右边界

    if (rect.origin.x == 0) {

        _xVelocity = 1.0;

    }

    

    //检测是否碰撞

    if (CGRectIntersectsRect(aView.frame, _block.frame)) {

        //停止运动

//        _xVelocity = 0.0;

        _yVelocity = 0.0;

        //将障碍物打没

//        [_block setHidden:YES];

        

        //碰撞之后反弹

        //判断碰撞的是哪个边界 (碰撞产生的矩形)

        

    }

    

    /*

    UIView *bView = [self.window viewWithTag:200];

    CGRect rect2 = [bView frame];

    rect2.origin.y += _yVelocity;

    rect2.origin.x += _xVelocity;

    [bView setFrame:rect2];

    

    //碰撞下边界

    if (rect2.origin.y + rect.size.height == self.window.bounds.size.height) {

        _yVelocity = -1.0;

    }

    //碰撞下边界

    if (rect2.origin.y == 0) {

        _yVelocity = 1.0;

    }

    //碰撞左边界

    if (rect2.origin.x + rect.size.width == self.window.bounds.size.width) {

        _xVelocity = -1.0;

    }

    //碰撞右边界

    if (rect2.origin.x == 0) {

        _xVelocity = 1.0;

    }

     */

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值