- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
_window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
_window.backgroundColor = [UIColor whiteColor];
[_window makeKeyAndVisible];
//創建可變數組
mutArray = [[NSMutableArray alloc] initWithCapacity:7];
for (int i=0; i<7; i++) {
//創建子視圖
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(60, 200, 250, 250)];
view.backgroundColor = [UIColor colorWithRed:arc4random()%10*0.1 green:arc4random()%10*0.1 blue:arc4random()%10*0.1 alpha:1];
[_window addSubview:view];
//取出數組中的最後一個元素(也就是最上層的視圖)
UIView *lastView = [mutArray lastObject];
if (lastView != nil) {
view.transform = CGAffineTransformScale(lastView.transform, .8, .8);
}
//將視圖添加到數組中
[mutArray addObject:view];
}
_index = 0;
//開啓定時器
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeAction:) userInfo:nil repeats:YES];
return YES;
}
- (void)timeAction:(NSTimer *)time {
_index ++;
//取得所有視圖進行旋轉
for (UIView *view in mutArray) {
//開始動畫
[UIView beginAnimations:nil context:nil];
//設置動畫的時間
[UIView setAnimationDuration:1];
//設置加速方式
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
view.transform = CGAffineTransformRotate(view.transform, M_PI/1.1);
view.backgroundColor = [UIColor colorWithRed:arc4random()%10*0.1 green:arc4random()%10*0.1 blue:arc4random()%10*0.1 alpha:1];
[UIView commitAnimations];
}
//當第十秒的時候移除視圖
if (_index == 10) {
for (UIView *view in mutArray) {
[view removeFromSuperview];
}
}