ios霓虹燈效果(沒基礎也可以做出好看的霓虹燈)


創建一個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.0
blue:arc4random()%256/255.0 alpha:0.7]; //顏色隨機}



發佈了43 篇原創文章 · 獲贊 11 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章