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
- NSTimer-------霓虹灯demo
- 霓虹灯Demo
- 霓虹灯 随机颜色 定时器nstimer
- android demo(二):霓虹灯
- NSTimer的使用Demo
- 霓虹灯
- 霓虹灯
- 霓虹灯
- 霓虹灯
- 【iOS学习】----NSTimer(附Demo)
- IOS开发入门基本知识——UIView和NSTimer形成的霓虹灯效果
- ios 时间定时器 NSTimer应用demo
- ios 时间定时器 NSTimer应用demo
- NSTimer
- NSTimer
- NSTimer
- NSTimer
- NSTimer
- pdm table不显示namem,而显示code
- 数据库连接参数从配置文件中读取
- Gallery-Dome图片滑动效果
- 更新java后的错误
- js怎么实现文本框中的颜色变化
- NSTimer-------霓虹灯demo
- 主流WIFI加密方式
- 让TLD 运行起来
- Android组件学习之TextView的直接子类和间接子类
- OpenGL(1) GLUT窗口图层初始化函数API
- 如何查看占用端口
- 电子信息工程就业方向及薪资标准
- QT 窗口取消标题栏
- 英语美句 4_1