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

@点击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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值