定时器和视图移动

1. 定时器

首先在视图控制器ViewController.h中创建定时器:

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12f1b}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Heiti SC Light'; color: #008400}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008400}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #bb2ca2}span.s1 {font-variant-ligatures: no-common-ligatures; color: #78492a}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s4 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s5 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #000000}span.s6 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s7 {font: 11.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}span.s8 {font-variant-ligatures: no-common-ligatures; color: #000000}

#import 

@interface ViewController : UIViewController
{
    //定义一个定时器对象,可以在每隔固定时间发送一个消息,通过此消息来调用相应的时间函数
    //通过此函数可在固定时间段来完成一个根据时间间隔的任务
    NSTimer* _timerView;
    
}
//属性
@property(retain,nonatomic)NSTimer* timerView;

@end

然后在ViewController.m中实现
完整代码如下:

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12f1b}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #4f8187}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #bb2ca2}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Heiti SC Light'; color: #008400}p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3d1d81}p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008400}p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #703daa}span.s1 {font-variant-ligatures: no-common-ligatures; color: #78492a}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s4 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s5 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s6 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s7 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s8 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s9 {font-variant-ligatures: no-common-ligatures; color: #272ad8}span.s10 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s11 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #000000}span.s12 {font: 11.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

//属性和成员变量的同步
//内部使用成员变量,外部使用属性
@synthesize timerView = _timerView;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UIButton* btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame=CGRectMake(100, 100, 80, 40);
    [btn setTitle:@"start time" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(pressStart) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:btn];
    
    UIButton* btnStop = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btnStop.frame=CGRectMake(100, 200, 80, 40);
    [btnStop setTitle:@"stop time" forState:UIControlStateNormal];
    [btnStop addTarget:self action:@selector(pressStop) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btnStop];
    
    //再添加一个视图,可以随着定时器移动
    UIView* view=[[UIView alloc]init];
    view.frame=CGRectMake(0, 0, 80, 80);
    view.backgroundColor=[UIColor orangeColor];
    [self.view addSubview:view];
    //设置view的标签值
    //通过父亲视图对象以及view的标签值可以获得相应的视图对象
    view.tag=101;

}

//实现按下按钮后的功能。使用定时器
-(void)pressStart
{
    //通过NSTimer的类方法创建一个定时器,并且启动这个定时器
    //参数1:每隔多长时间调用定时器函数,s为单位
    //参数2:表示实现定时器函数的对象(指针)
    //参数3:表示定时器函数对象
    //参数4:可以传入定时器函数中一个参数,nil代表无参数。注意要是有参数,函数名后要加个冒号
    //参数5:定时器是否重复调用函数操作 比如闹钟 只响一次和每周响一次
    //这个方法的返回值为一个新建好的定时器对象
    _timerView = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(updateTimer:) userInfo:@"mary" repeats:YES];
    
}

//定时器函数
-(void)updateTimer:(NSTimer*) timer
{
    NSLog(@"test");
    NSLog(@"%@",timer.userInfo);
    //最好tag从100开始,防止冲突
    UIView* view=[self.view viewWithTag:101];
    view.frame=CGRectMake(view.frame.origin.x+5, view.frame.origin.y+5, 80, 80);
}

-(void)pressStop
{
    //通过返回值停止定时器
    if(_timerView != nil)
    {
        //使该定时器失效
        [_timerView invalidate];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

2. 定时器的创建

其中需要注意的是创建定时器,是用的NSTimer的类方法scheduledTimerWithTimeInterval来创建的。
这个方法有五个参数:

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}span.s1 {font-variant-ligatures: no-common-ligatures}

NSTimer scheduledTimerWithTimeInterval:<#(NSTimeInterval)#> target:<#(id)#> selector:<#(SEL)#> userInfo:<#(id)#> repeats:<#(BOOL)#>
  • 参数1:每隔多长时间调用定时器函数,s为单位
  • 参数2:表示实现定时器函数的对象(指针)
  • 参数3:表示定时器要调用的函数对象
  • 参数4:可以传入定时器要调用的函数中一个参数,nil代表无参数。注意要是有参数,参数3的函数名后要加一个冒号
  • 参数5:定时器是否重复
    注意:这个方法的返回值是一个新建好的定时器对象。

3. 定时器的停止

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Heiti SC Light'; color: #008400}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008400}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s3 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #000000}span.s4 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s5 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s6 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s7 {font: 11.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}span.s8 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}

-(void)pressStop
{
    //通过返回值停止定时器
    if(_timerView != nil)
    {
        //使该定时器失效
        [_timerView invalidate];
    }
}

4. 视图移动

新建一个视图,然后在定时器重复调用函数中逐步改变视图原点,即可实现视图移动。

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Heiti SC Light'; color: #008400}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008400}span.s1 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s4 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s5 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s6 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s7 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s8 {font: 11.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}span.s9 {font-variant-ligatures: no-common-ligatures; color: #272ad8}

//定时器函数


-(void)updateTimer:(NSTimer*) timer
{
    NSLog(@"test");
    NSLog(@"%@",timer.userInfo);
    //最好tag从100开始,防止冲突
    UIView* view=[self.view viewWithTag:101];
    view.frame=CGRectMake(view.frame.origin.x+5, view.frame.origin.y+5, 80, 80);
}

你可能感兴趣的:(定时器和视图移动)