CGContextRef context = UIGraphicsGetCurrentContext(); [UIView beginAnimations:nil context:context]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [UIView setAnimationDuration:1.0]; //使用當前正在運行的動畫狀態開始下一段動畫,避免了因動畫狀態不一樣而產生的跳躍 [UIView setAnimationBeginsFromCurrentState:YES]; [[self.view viewWithTag:999] setAlpha:1.0f]; [UIView commitAnimations];
一,試圖的交換:
UIView *frontObject = [[self.view subviews] objectAtIndex:2]; UIView *backObject = [[self.view subviews] objectAtIndex:1]; CGContextRef context = UIGraphicsGetCurrentContext(); [UIView beginAnimations:nil context:context]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [UIView setAnimationDuration:1.0]; frontObject.alpha = 0.0f; backObject.alpha = 1.0f; frontObject.transform = CGAffineTransformMakeScale(0.25f, 0.25f);//縮放的比例 backObject.transform = CGAffineTransformIdentity;//重置視圖 [self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:2];//交換兩個字視圖的索引 [UIView setAnimationDelegate:self]; [UIView setAnimationDidStopSelector:@selector(animationFinished:)];//動畫執行完以後的回調方法 [UIView commitAnimations];
二,翻轉視圖
CGContextRef context = UIGraphicsGetCurrentContext(); [UIView beginAnimations:nil context:context]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [UIView setAnimationDuration:1.0]; UIView *whiteBackdrop = [self.view viewWithTag:100]; // Choose left or right flip if ([(UISegmentedControl *)self.navigationItem.titleView selectedSegmentIndex]) [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:whiteBackdrop cache:YES]; else [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight forView:whiteBackdrop cache:YES]; //交換兩個視圖 NSInteger purple = [[whiteBackdrop subviews] indexOfObject:[whiteBackdrop viewWithTag:999]]; NSInteger maroon = [[whiteBackdrop subviews] indexOfObject:[whiteBackdrop viewWithTag:998]]; [whiteBackdrop exchangeSubviewAtIndex:purple withSubviewAtIndex:maroon]; [UIView setAnimationDelegate:self]; [UIView setAnimationDidStopSelector:@selector(animationFinished:)]; [UIView commitAnimations];
三,翻頁動畫
CGContextRef context = UIGraphicsGetCurrentContext(); [UIView beginAnimations:nil context:context]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [UIView setAnimationDuration:1.0]; UIView *whiteBackdrop = [self.view viewWithTag:100]; // Choose up or down curl if ([(UISegmentedControl *)self.navigationItem.titleView selectedSegmentIndex]) [UIView setAnimationTransition: UIViewAnimationTransitionCurlUp forView:whiteBackdrop cache:YES]; else [UIView setAnimationTransition: UIViewAnimationTransitionCurlDown forView:whiteBackdrop cache:YES]; NSInteger purple = [[whiteBackdrop subviews] indexOfObject:[whiteBackdrop viewWithTag:999]]; NSInteger maroon = [[whiteBackdrop subviews] indexOfObject:[whiteBackdrop viewWithTag:998]]; [whiteBackdrop exchangeSubviewAtIndex:purple withSubviewAtIndex:maroon]; [UIView setAnimationDelegate:self]; [UIView setAnimationDidStopSelector:@selector(animationFinished:)]; [UIView commitAnimations];
UIViewAnimationTransition的幾種效果
UIViewAnimationTransitionNone, UIViewAnimationTransitionFlipFromLeft,//UIView過分,從左向右翻轉,用新視圖代替舊視圖 UIViewAnimationTransitionFlipFromRight,//UIView過分,從右向左翻轉,隱藏舊視圖,顯示新視圖 UIViewAnimationTransitionCurlUp, //用從下向上翻頁蓋住舊視圖 UIViewAnimationTransitionCurlDown,//新視圖從上向下翻頁蓋住舊視圖