IOS 霓虹燈效果(oc) – iPhone手機開發技術文章 iPhone軟體開發教學課程

//初始化視圖
– (void) initViews{
//紅色視圖
UIView *redView=[[UIView alloc] initWithFrame:CGRectMake(20, 20, 280, 280)];
redView.backgroundColor=[UIColor redColor];

//視圖的標簽,標示一個視圖對象
vcD4KPHA+CiAgICByZWRWaWV3LnRhZz0xOzxicj4KICAgIFtzZWxmLnZpZXcgYWRkU3VidmlldzpyZWRWaWV3XTs8YnI+CiAgICBbcmVkVmlldyByZWxlYXNlXTs8YnI+CiAgICA8YnI+CiAgICA8YnI+CiAgICAvL7PIyavK0828PGJyPgogICAgVUlWaWV3ICpvcmlhbmdlVmlldz1bW1VJVmlldyBhbGxvY10gaW5pdFdpdGhGcmFtZTpDR1JlY3RNYWtlKDIwLCAyMCwgMjQwLCAyNDApXTs8YnI+CiAgICBvcmlhbmdlVmlldy5iYWNrZ3JvdW5kQ29sb3I9W1VJQ29sb3Igb3JhbmdlQ29sb3JdOzxicj4KICAgIG9yaWFuZ2VWaWV3LnRhZz0yOzxicj4KICAgIFtyZWRWaWV3IGFkZFN1YnZpZXc6b3JpYW5nZVZpZXddOzxicj4KICAgIFtvcmlhbmdlVmlldyByZWxlYXNlXTs8YnI+CiAgICA8YnI+CiAgICAvL7vGyavK0828PGJyPgogICAgVUlWaWV3ICp5ZWxsb3dWaWV3PVtbVUlWaWV3IGFsbG9jXSBpbml0V2l0aEZyYW1lOkNHUmVjdE1ha2UoMjAsIDIwLCAyMDAsIDIwMCldOzxicj4KICAgIHllbGxvd1ZpZXcuYmFja2dyb3VuZENvbG9yPVtVSUNvbG9yIHllbGxvd0NvbG9yXTs8YnI+CiAgICB5ZWxsb3dWaWV3LnRhZz0zOzxicj4KICAgIFtvcmlhbmdlVmlldyBhZGRTdWJ2aWV3OnllbGxvd1ZpZXddOzxicj4KICAgIFt5ZWxsb3dWaWV3IHJlbGVhc2VdOzxicj4KICAgIDxicj4KICAgIDxicj4KICAgIC8vwszJq8rTzbw8YnI+Ci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ZXc9W1tVSVZpZXcgYWxsb2NdIGluaXRXaXRoRnJhbWU6Q0dSZWN0TWFrZSgyMCwgMjAsIDQwLCA0MCldOzxicj4KICAgIHB1cnBsZVZpZXcuYmFja2dyb3VuZENvbG9yPVtVSUNvbG9yIHB1cnBsZUNvbG9yXTs8YnI+CiAgICBwdXJwbGVWaWV3LnRhZz03Ozxicj4KICAgIFtibHVlVmlldyBhZGRTdWJ2aWV3OnB1cnBsZVZpZXddOzxicj4KICAgIFtwdXJwbGVWaWV3IHJlbGVhc2VdOzxicj4KICAgIDxicj4KICAgIDxicj4KfTxicj4KPC9wPgo8cD4KLy/Ktc/Wt63Ss9Cnufs8YnI+CjwvcD4KPHA+Ci0gKHZvaWQpeHVhbnpodWFuZzooaWQpIHNlbmRlcns8YnI+CiAgICA8YnI+CiAgICAvL7vxyKG1scewu63NvLXEyeixuMnPz8LOxDxicj4KICAgIENHQ29udGV4dFJlZiBjb250ZXh0PVVJR3JhcGhpY3NHZXRDdXJyZW50Q29udGV4dCgpOzxicj4KICAgIC8vv6rKvNe8sbi2r7utPGJyPgogICAgW1VJVmlldyBiZWdpbkFuaW1hdGlvbnM6QA==”*******” context:context];

[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

//設置動畫持續時間

[UIView setAnimationDuration:0.5];

//因為沒有給該Controller添加實例變量,所以用以下方法獲得View

UIView *parentView=[self.view viewWithTag:1];

//設置動畫效果

[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:parentView cache:YES]; //從上到下
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:parentView cache:YES]; //從下往上

// [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:parentView cache:YES]; //從左到右
// [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:parentView cache:YES]; //從右到左

//設置動畫委托
[UIView setAnimationDelegate:self];

//當動畫啊結束時 調用animationFinished方法
[UIView setAnimationDidStopSelector:@selector(animationFinished:)];

//提交動畫

[ UIView commitAnimations];

}

//處理顏色的改變
– (void) showChangeView:(id) sender{
[self xuanzhuang:nil];
NSLog(@”調用showChange函數!!”);

//每次調用,當前顏色加1

currentColor++;
if (currentColor>6) {
currentColor=0;
}
colorArray=[NSArray arrayWithObjects:[UIColor redColor],[UIColor orangeColor],[UIColor yellowColor],[UIColor greenColor],[UIColor cyanColor],[UIColor blueColor],[UIColor purpleColor], nil];

//初始化視圖數組
for (int i=1; i<=7; i++) {

//存的是7個視圖的對象

[viewArray addObject:[self.view viewWithTag:i]];

}

// NSLog(@”顏色數組大小為:%d”,[ colorArray count]);
// NSLog(@”視圖大小為:%d”,[viewArray count]);

//關鍵代碼,循環設置背景色
for (int i=0; i<7-currentColor; i++) {
[[viewArray objectAtIndex:i] setBackgroundColor:[colorArray objectAtIndex:i+currentColor]];
}

for (int i=6-currentColor,j=0; i<7;j++,i++) {
[[viewArray objectAtIndex:i] setBackgroundColor:[colorArray objectAtIndex:j]];
}

}

– (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@”調用viewDidLoad方法!!!”);

self.view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460 )];
self.view.backgroundColor=[UIColor clearColor];
//初始化視圖
[self initViews];

//定義一個定時器每隔0.5秒循環調用showChangeView:方法,這個定時器的用法如果還不是很熟悉的話,可以看我的 其他文章,有對他的詳細講解

NSTimer *timer=[NSTimer timerWithTimeInterval:0.5 target:self selector:@selector(showChangeView:) userInfo:nil repeats:YES];
NSRunLoop *runloop=[NSRunLoop currentRunLoop];
[runloop addTimer:timer forMode:NSDefaultRunLoopMode];

}

發佈留言