1. 程式人生 > >IOS開發中NSTimer定時器的使用

IOS開發中NSTimer定時器的使用

第一步:呼叫函式

[self getCircleBackGround:self.blcView.btChartView.mCircleView getCurrentLb:self.blcView.btChartView.mCurrent ];

第二步:方法的實現

-(void)getCircleBackGround:(UIView *)view getCurrentLb:(UILabel *)cutlb{
		if(![self.OneTTF isEqualToString:@"0"]){
		                if(self.highTimer==nil){
		                    //[self.highTimer fire];
		                    self.highTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(backgroundColorChange:) userInfo:view repeats:YES];
		                }
		            }else{
		                [self.highTimer invalidate];
		                self.highTimer=nil;
		                view.backgroundColor = [UIColor grayColor];
		                NSLog(@"aaa:%@",@"database is 0");
		            }
}

第三步:定時器觸發的方法

// 定時器觸發方法
-(void)backgroundColorChange:(NSTimer *)timer{
    UIView *views =[timer userInfo];
    views.backgroundColor = [self randomColor];
}

第四步:顏色隨著時間定時器的變化而變化。

-(UIColor *)randomColor{
    if(self.cdata==0){
        self.cdata++;
        return [UIColor redColor];
        
    }else if(self.cdata==1){
        self.cdata++;
        return [UIColor greenColor];
        
    }else{
        self.cdata=0;
        return [UIColor blueColor];
    }
}

第五步:定時器的定義

NSDictionary *alarm = [notification userInfo];

摧毀

 [self.highTimer invalidate];
 self.highTimer=nil;