您的位置:首页 > 移动开发 > IOS开发

ios霓虹灯效果(没基础也可以做出好看的霓虹灯)

2013-12-14 20:31 274 查看
创建一个TheNeonLightsView类
TheNeonLightsView.h
定义4个实例变量@interface TheNeonLightsView : UIView{NSTimer *time;UIView *aView;UIView *bView;UIButton *button;}@end
TheNeonLightsView.m中写实现
@implementation TheNeonLightsView- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { //基层容器视图 self.backgroundColor = [UIColor blackColor]; aView = [[UIView alloc]initWithFrame:CGRectMake(10, 40, 300, 300)]; aView.layer.cornerRadius= 10; [self addSubview:aView]; //scrollView //button视图 for (int i=0; i<5; i++) { //外循环 内循环循环5次,外循环循环一次 for (int j=0; j<5; j++) { //内循环 bView = [[UIView alloc]initWithFrame:CGRectMake((0+j)*60, (0+i)*60, 60, 60)]; //x坐标是每次增加60 因为我要一行5个视图, bView.backgroundColor= [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:0.7]; //背景颜色随机 bView.layer.cornerRadius = 10; [aView addSubview:bView]; } } //创建需要用的Button button = [UIButton buttonWithType:UIButtonTypeRoundedRect];//初始化 button.frame = CGRectMake(60, 380, 70, 40); //设置坐标 [button setBackgroundColor:[UIColor yellowColor]]; //背景颜色 [button setTitle:@"开始" forState:UIControlStateNormal]; //设置标题 button.titleLabel.textAlignment = NSTextAlignmentCenter; //标题对齐方式 [self addSubview:button];[button addTarget:self action:@selector(button) forControlEvents:UIControlEventTouchUpInside];//target是目标,action是行为,就是指 谁去做什么事. //返回button UIButton *getBackButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; getBackButton.frame = CGRectMake(210, 380,70, 40); [getBackButton setBackgroundColor:[UIColor whiteColor]]; [getBackButton setTitle:@"返回" forState:UIControlStateNormal]; getBackButton.titleLabel.textAlignment = NSTextAlignmentCenter; [self addSubview:getBackButton]; [getBackButton addTarget:self action:@selector(goBack)forControlEvents:UIControlEventTouchUpInside]; } return self;}//button需要做的事- (void)button{ time = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(changeColor) userInfo:nil repeats:YES];}- (void)goBack{ NSLog(@"@@@"); UIView *view= [[self superview] viewWithTag:2000]; view.hidden = NO; self.hidden = YES; }//changeColor 改变颜色的行为 方法- (void)changeColor{ NSArray *arr = aView.subviews; //用数组获取所有子视图 int count = [arr count]-1; //后面要用到颜色调换 -1 是留着最后一个和第一个调换 UIView *firstView = [arr objectAtIndex:0];//取数组中第一个下标 也就是0的视图 UIColor *firstColor = firstView.backgroundColor; //数组中取出颜色赋值 for (int i=0; i<count; i++) { UIView *view = [arr objectAtIndex:i]; //数组中的每次元素都赋值给视图view UIView *nextView = [arr objectAtIndex:(i+1)]; //数组中下一个的元素赋值给nextView view.backgroundColor= nextView.backgroundColor; //把下一个的元素视图的颜色赋值给当前视图的颜色 } ((UIView*)[arr objectAtIndex:count]).backgroundColor = firstColor; //出了for循环 count为数组中最后一个 把第一个颜色赋值给最后一个视图 bView.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0blue:arc4random()%256/255.0 alpha:0.7]; //颜色随机}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: