美文网首页
ios NSTimer定时器

ios NSTimer定时器

作者: 顶儿响叮当 | 来源:发表于2016-09-07 23:32 被阅读14次

    做了一个非常简单的关于定时器的demo,效果如图,点击start timer,两个矩形开始移动,添加了一些碰撞检测,即两个矩形相碰或碰到边界会弹回,点击stop timer定时器停止。

    效果图

    在ViewController.h中,定义定时器变量

    //ViewController.h
    #import <UIKit/UIKit.h>
    //所有的控制器都需要自定义来完成
    //继承于官方的UIViewController
    @interface ViewController : UIViewController{
        //定义一个定时器对象
        //可以在每隔固定的时间发送一个消息,通过此消息调用相应的函数
        NSTimer* _timerView;
    
        int direction1_x;  //标记view1 xy方向
        int direction1_y;
        int direction2_x;  //标记view2 xy方向
        int direction2_y;
        int speed_x;      //标记xy方向上的移动速度
        int speed_y;
    }
    @property (retain,nonatomic) NSTimer* timerView;
    @end
    

    在ViewController.m中实现

    #import "ViewController.h"
    @interface ViewController ()
    @end
    @implementation ViewController
    //属性和成员函数的同步
    @synthesize timerView = _timerView;
    
    -(void) creatTimer{
       //创建startTimer按钮 ,并设置按下的触发事件,启动定时器
        UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btn.frame = CGRectMake(100, 100, 80, 40);
        [btn setTitle:@"start timer" forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(pressStart) forControlEvents:UIControlEventTouchUpInside];
        //创建stopTimer按钮
        UIButton* btnStop = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btnStop.frame = CGRectMake(100, 200, 80, 40);
        [btnStop setTitle:@"stop Timer" forState:UIControlStateNormal];
        [btnStop addTarget:self action:@selector(pressStop)  forControlEvents:UIControlEventTouchUpInside];
        
        [self.view addSubview:btn];
        [self.view addSubview:btnStop];
        
        //创建两个view,矩形
        UIView* view1 = [[UIView alloc] init];
        view1.frame = CGRectMake(30, 30, 80, 80);
        view1.backgroundColor = [UIColor orangeColor];
        [self.view addSubview:view1];
        
        UIView* view2 = [[UIView alloc] init];
        view2.frame = CGRectMake(80, 200, 40, 40);
        view2.backgroundColor = [UIColor blueColor];
        [self.view addSubview:view2];
        
        //设置view的标签值,通过父亲视图对象以及view的标签值可以获得相应的视图对象
        view1.tag=101;
        view2.tag=102;
    
        direction1_x=1;  //标记view1 xy方向
        direction1_y=1;
        direction2_x=1;  //标记view2 xy方向
        direction2_y=1;
        speed_x=5;      //标记xy方向上的移动速度
        speed_y=5;
    }
    
    -(void) pressStart{
        //nstimer的类方法创建一个定时器并且启动这个定时器
        //P1 每隔多长时间调用一次定时器函数,以秒为单位
        //P2 表示实现定时器函数的对象
        //P3 对象实现的函数
        //P4 传参
        //P5 定时器是否重复操作
        //返回值为一个新创建好的定时器
        _timerView = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(updateTimer:) userInfo:@"Johnny" repeats:YES];
    }
    
    -(void) pressStop{
        //停止定时器
        if (_timerView != nil){
            [_timerView invalidate];
        }
    }
    
    //定时器的触发函数,定时器本身作为参数传递过来
    -(void) updateTimer:(NSTimer*) timer{
        //NSLog(@"timerStart test name = %@",timer.userInfo);
        UIView* view1 = [self.view viewWithTag:101];
        UIView* view2 = [self.view viewWithTag:102];
       //检测两个矩形是否相交,若相交则方向取反
        [self checkCollopse:view1 view2:view2];
       //移动两个矩形,并判断是否与边界相交,若相交方向取反
       //两个矩形调用同一个方法,两个矩形使用不同的direction值,将各自的direction以指针传递过去
        [self checkBorder:view1 direction_x:&direction1_x direction_y:&direction1_y];
        [self checkBorder:view2 direction_x:&direction2_x direction_y:&direction2_y];
    }
    
    //检测两个矩形view是否相交,若不相交返回false,相交则将direction取反,返回true
    -(bool) checkCollopse:(UIView*) view1 view2:(UIView*) view2{
        int x1 = view1.frame.origin.x;
        int y1 = view1.frame.origin.y;
        int x2 = view2.frame.origin.x;
        int y2 = view2.frame.origin.y;
        int w1 = view1.frame.size.width;
        int h1 = view1.frame.size.height;
        int w2 = view2.frame.size.width;
        int h2 = view2.frame.size.height;
        //检测一定不相交的情况
        if ((x1 + w1) < x2 || (x2 + w2) < x1 || (y1 + h1) < y2 || (y2 + h2) < y1){
            //不相交
            return false;
        }else{
            if ((x1 + w1) <= x2 || (x2 + w2) <= x1){
                //x相交
                direction1_x = -direction1_x;
                direction2_x = -direction2_x;
            }else{
                //y相交
                direction1_y = -direction1_y;
                direction2_y = -direction2_y;
            }
            return true;
        }
    }
    
    //检测view的范围,碰到边界返回
    -(void) checkBorder:(UIView*) view direction_x:(int *)direction_x  direction_y:(int *)direction_y{
        if (view.frame.origin.x+view.frame.size.width >= self.view.frame.size.width || view.frame.origin.x <= 0){
            *direction_x = -*direction_x;
        }
        if (view.frame.origin.y+view.frame.size.height >= self.view.frame.size.height || view.frame.origin.y <= 0){
            *direction_y = -*direction_y;
        }
        int x_move = *direction_x*speed_x, y_move = *direction_y*speed_y;
        view.frame = CGRectMake(view.frame.origin.x+x_move, view.frame.origin.y+y_move, view.frame.size.width, view.frame.size.height);    
    }
    
    //当视图控制器第一次被加载显示视图时,调用此函数
    //布局初始化视图来使用,初始化资源使用
    - (void)viewDidLoad {
        //定时器函数
        [self creatTimer];
    }
    

    相关文章

      网友评论

          本文标题:ios NSTimer定时器

          本文链接:https://www.haomeiwen.com/subject/pycsettx.html