NSTimer-------霓虹灯demo

来源:互联网 发布:php pdo exec execute 编辑:程序博客网 时间:2024/06/06 02:09

@点击Start按钮,左侧霓虹灯由内向外运行显示,右侧霓虹灯由外向内运行显示。点击exchange按钮,进行相反设置,右侧霓虹灯由内向外运行显示,左侧霓虹灯由外向内运行显示。


@interface HMTNeonLightViewController (){    NSTimer * _timeControl;    int  leftViewCount;    int  rightViewCount;    BOOL isExchange;    BOOL stop;    }@property (nonatomic,retain)UIView * neonView;@property (nonatomic,retain)UIButton * startButton;@property (nonatomic,retain)UIButton * exchangeButton;@property (nonatomic,retain)UIButton * stopButton;@end@implementation HMTNeonLightViewController- (void)dealloc{        RELEASE_SAFELY(_stopButton);    RELEASE_SAFELY(_neonView);    RELEASE_SAFELY(_startButton);    RELEASE_SAFELY(_exchangeButton);    [super dealloc];}- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization        leftViewCount = 8;        rightViewCount = 9;        isExchange = NO;        stop = YES;    }    return self;}- (void)viewDidLoad{    [super viewDidLoad];// Do any additional setup after loading the view.    [self createNemoLightView];        }- (void)createNemoLightView{    int n = 0;    for (int j = 0; j < 2; j++) {        for (int i = 0; i < 9; i++) {                        self.neonView = [[UIView alloc]initWithFrame:CGRectMake(30 + i * 5 + j * 160, 80 + i * 5, 100 - i * 10 , 100 - i * 10)];            _neonView.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];            _neonView.tag = n;            [self.view addSubview:_neonView];            [_neonView release];            n++;        }    }        self.startButton = [UIButton buttonWithType:UIButtonTypeSystem];    self.startButton.frame = CGRectMake(100, 300, 100, 40);    [self.startButton setTitle:@"start" forState:UIControlStateNormal];    [self.startButton addTarget:self action:@selector(start:) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:_startButton];        self.exchangeButton = [UIButton buttonWithType:UIButtonTypeSystem];    self.exchangeButton.frame = CGRectMake(100, 350, 100, 40);    [self.exchangeButton setTitle:@"exchange" forState:UIControlStateNormal];    [self.exchangeButton addTarget:self action:@selector(exchange:) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:_exchangeButton];        self.stopButton = [UIButton buttonWithType:UIButtonTypeSystem];    self.stopButton.frame = CGRectMake(100, 400, 100, 40);    [self.stopButton setTitle:@"stop" forState:UIControlStateNormal];    [self.stopButton addTarget:self action:@selector(stop:) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:_stopButton];        }- (void)goTime:(NSTimer *)theTimer{    NSArray * arrayViews = [self.view subviews];        if (isExchange == NO) {        if (leftViewCount == -1) {            leftViewCount = 8;        }                        ((UIView *)[arrayViews objectAtIndex:leftViewCount]).backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];                leftViewCount--;                if (rightViewCount == 18) {            rightViewCount = 9;        }                ((UIView *)[arrayViews objectAtIndex:rightViewCount]).backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];                rightViewCount++;    }else{        if (leftViewCount == 9) {            leftViewCount = 0;        }                        ((UIView *)[arrayViews objectAtIndex:leftViewCount]).backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];                leftViewCount++;                if (rightViewCount == 8) {            rightViewCount = 17;        }                ((UIView *)[arrayViews objectAtIndex:rightViewCount]).backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];                rightViewCount--;        }    }- (void)start:(id)sender{        // 创建定时器    _timeControl = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(goTime:) userInfo:nil repeats:YES];    // 启动    [_timeControl fire];    stop = NO;    NSLog(@"%s,%d",__FUNCTION__,__LINE__);    }- (void)exchange:(id)sender{        stop = NO;    if (isExchange == NO) {        isExchange = YES;    } else {        isExchange = NO;    }    NSLog(@"%s,%d",__FUNCTION__,__LINE__);}- (void)stop:(id)sender{        // 加一个判断    if (stop == NO) {        // 停止        [_timeControl invalidate];    }    stop = YES;        NSLog(@"%s,%d",__FUNCTION__,__LINE__);    }- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

//安全释放NSObject指针对象

#define RELEASE_SAFELY(__Pointer) do{[__Pointer release],__Pointer = nil;} while(0)


0 0