iOS学习应用开发就业课_010:定时器和视图移动

要求:

 1.定义一个定时器对象;

 2.可以每个固定时间发送一个消息;

 3.通过次函数可以在固定时间段来完成一个根据时间完成的任务

 4.添加按钮;

 5.UIView移动



复习:

5月26日失败一次

5月27日上午成功

7月11日失败一次



源码:

#import "ViewController.h"


@interfaceViewController ()


@end


@implementation ViewController

@synthesize timer=_timer;


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    UIButton *btnStart=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    btnStart.frame=CGRectMake(100,100, 80,40);

    [btnStartsetTitle:@"点击移动"forState:UIControlStateNormal];

    [btnStartaddTarget:selfaction:@selector(pressStart:)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:btnStart];

    

    UIButton *btnStop=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    btnStop.frame=CGRectMake(100,200, 80,40);

    [btnStopsetTitle:@"点击复位"forState:UIControlStateNormal];

    [btnStopaddTarget:selfaction:@selector(pressStop)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:btnStop];

    

    UIView *uiView=[[UIViewalloc]initWithFrame:CGRectMake(0,0, 50,50)];

    uiView.backgroundColor=[UIColoryellowColor];

    uiView.tag=101;

    [self.viewaddSubview:uiView];

}


 -(void)pressStart:(NSTimer*)timerM

{

    timerM=[NSTimerscheduledTimerWithTimeInterval:1target:selfselector:@selector(move)userInfo:@"小明"repeats:YES];

    _timer=timerM;

}

-(void)move

{

    UIView *vc=[[UIViewalloc]init];

    vc=[self.viewviewWithTag:101];

    vc.frame=CGRectMake(vc.frame.origin.x+5, vc.frame.origin.y+5,50, 50);

    vc.backgroundColor=[UIColorredColor];

    NSLog(@"%@",_timer.userInfo);

    

}

-(void)pressStop

{

    if (_timer!=nil)

    {

        [_timerinvalidate];

        NSLog(@"计时器停止");

        UIView *vc=[[UIViewalloc]init];

        vc=[self.viewviewWithTag:101];

        vc.frame=CGRectMake(0,0, 50,50);

        vc.backgroundColor=[UIColoryellowColor];

    }

}


猜你喜欢

转载自blog.csdn.net/sap_support/article/details/51544934