UIView动画

来源:互联网 发布:淘宝个人店铺哪里看 编辑:程序博客网 时间:2024/05/16 04:46
UIView动画能够完美的建立起一座简介视图当前状态和未来状态的视觉桥梁,可以把所有视觉变化链接起来,产生流畅的动画效果。可以产生动画效果的变化包括:位置变化,大小变化,伸缩变化,透明度变化,隐藏和显示变化,视图层次顺序变化,仿射变化。  

UIView动画成块运行,也就是说作为完整的事物一次性运行。  

Copy code
CGContextRef context = UIGraphicsGetCurrentContext();  
[UIView beginAnimations:nil context:context];  
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];  
[UIView setAnimationDuration:0.3f];  
[myView setAlpha: 0.0f];  
[UIView commitAnimations];  
UIGraphicsGetCurrentContext() 返回当前视图堆栈顶部的图形上下文。图形上下文在抽象的绘图调用和屏幕实际像素之间建立一种虚拟的连接。  
beginAnimations:context: 标记动画块的开始。  
setAnimationCurve: 定义动画的加速和减速方式。  
setAnimationDuration: 以秒为单位指定动画的时长,尽量让动画在1~2秒内完成。另外,键盘移入和移除动画时间是0.3秒。  
commitAnimations 标志动画块的结束。  
视图动画可以通知可选委托其状态的改变。  
setAnimationDelegate: 设置委托。  
setAnimationWillStartSelector: 设置动画开始回调。  
setAnimationDidStopSelector: 设置动画结束回调。  
视图交换 
Copy code
- (void) animationFinished: (id) sender 

// show the button 
self.navigationItem.rightBarButtonItem = BARBUTTON(@"Swap", @selector(swap:)); 

- (void) swap: (id) sender 

// hide the button 
self.navigationItem.rightBarButtonItem = nil; 
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]; 
}
翻转视图 
Copy code
- (void) animationFinished: (id) sender 

self.navigationItem.rightBarButtonItem = BARBUTTON(@"Flip", @selector(flip:)); 

- (void) flip: (id) sender 

// hide the button 
self.navigationItem.rightBarButtonItem = nil; 
CGContextRef context = UIGraphicsGetCurrentContext(); 
[UIView beginAnimations:nil context:context]; 
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
[UIView setAnimationDuration:1.0]; 
UIView *whiteBackdrop = [self.view viewWithTag:100]; 
// 翻转效果 
if ([(UISegmentedControl *)self.navigationItem.titleView selectedSegmentIndex]) 
  [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:whiteBackdrop cache:YES]; 
else 
  [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight forView:whiteBackdrop cache:YES]; 
/* 翻页效果 
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]; 

反弹视图 
Copy code
@interface UIView (ModalAnimationHelper) 
  (void) commitModalAnimations; 
@end 
@interface UIViewDelegate : NSObject 

CFRunLoopRef currentLoop; 

@end 
@implementation UIViewDelegate 
-(id) initWithRunLoop: (CFRunLoopRef)runLoop  

if (self = [super init]) currentLoop = runLoop; 
return self; 

-(void) animationFinished: (id) sender 

CFRunLoopStop(currentLoop); 

@end 
@implementation UIView (ModalAnimationHelper) 
  (void) commitModalAnimations 

CFRunLoopRef currentLoop = CFRunLoopGetCurrent(); 
UIViewDelegate *uivdelegate = [[UIViewDelegate alloc] initWithRunLoop:currentLoop]; 
[UIView setAnimationDelegate:uivdelegate]; 
[UIView setAnimationDidStopSelector:@selector(animationFinished:)]; 
[UIView commitAnimations]; 
CFRunLoopRun(); 
[uivdelegate release]; 

@end 
@interface TestBedViewController : UIViewController 

int direction; 

@end 
@implementation TestBedViewController 
- (void) animate: (id) sender 

// Hide the bar button and show the view 
self.navigationItem.rightBarButtonItem = nil; 
[self.view viewWithTag:101].alpha = 1.0f; 
// Bounce to 115% of the normal size 
[UIView beginAnimations:nil context:UIGraphicsGetCurrentContext()]; 
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
[UIView setAnimationDuration:0.4f]; 
[self.view viewWithTag:101].transform = CGAffineTransformMakeScale(1.15f, 1.15f); 
[UIView commitModalAnimations]; 
// Return back to 100% 
[UIView beginAnimations:nil context:UIGraphicsGetCurrentContext()]; 
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
[UIView setAnimationDuration:0.3f]; 
[self.view viewWithTag:101].transform = CGAffineTransformMakeScale(1.0f, 1.0f); 
[UIView commitModalAnimations]; 
// Pause for a second and appreciate the presentation 
//[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0f]]; 
// Slowly zoom back down and hide the view 
[UIView beginAnimations:nil context:UIGraphicsGetCurrentContext()]; 
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
[UIView setAnimationDuration:1.0f]; 
[self.view viewWithTag:101].transform = CGAffineTransformMakeScale(0.01f, 0.01f); 
[UIView commitModalAnimations]; 
// Restore the bar button 
[self.view viewWithTag:101].alpha = 0.0f; 
self.navigationItem.rightBarButtonItem = BARBUTTON(@"Bounce", @selector(animate:)); 

- (void) viewDidLoad 

direction = 0; 
self.navigationController.navigationBar.tintColor = COOKBOOK_PURPLE_COLOR; 
self.navigationItem.rightBarButtonItem = BARBUTTON(@"Bounce", @selector(animate:)); 
[self.view viewWithTag:101].transform = CGAffineTransformMakeScale(0.01f, 0.01f); 
[self.view viewWithTag:101].alpha = 0.0f; 

@end 
@interface TestBedAppDelegate : NSObject <UIApplicationDelegate> 
@end 
@implementation TestBedAppDelegate 
- (void)applicationDidFinishLaunching:(UIApplication *)application {  
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[TestBedViewController alloc] init]]; 
[window addSubview:nav.view]; 
[window makeKeyAndVisible]; 

@end 
int main(int argc, char *argv[]) 

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 
int retVal = UIApplicationMain(argc, argv, nil, @"TestBedAppDelegate"); 
[pool release]; 
return retVal; 

UIImageView图像动画 
Copy code
NSMutableArray *bflies = [NSMutableArray array]; 
for (int i = 1; i <= 17; i  ) 
[bflies addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"bf_%d", i] ofType:@"png"]]]; 
UIImageView *butterflyView = [[UIImageView alloc] initWithFrame:CGRectMake(40.0f, 300.0f, 60.0f, 60.0f)]; 
butterflyView.animationImages = bflies; 
butterflyView.animationDuration = 0.75f; 
[self.view addSubview:butterflyView]; 
[butterflyView startAnimating]; 
[butterflyView release]; 
Core Animation Transitions 作为QuartzCore的一部分,丰富了UIView动画的内涵。它只针对图层,不针对视图。图层是Core Animation与每个UIView产生联系的工作层面。使用Core Animation需要导入QuartzCore.framework框架。animation.type类型: 
kCATransitionFade :交叉淡化 
kCATransitionMoveIn :一个视图滑动到另一个视图的上面 
kCATransitionPush :一个视图将另一个视图推出屏幕 
kCATransitionReveal :一个视图从另一个视图上面滑开 
@"pageCurl" :翻页过渡 
@"pageUnCurl" :翻页过渡的逆过程 
@"suckEffect" :吸取收缩效果 
@"rippleEffect" 滴水效果 
@"oglFlip" :翻转效果 
@"cube" : 立方体转动效果 
@"cameraIrisHollowOpen" :相机镜头打开效果 
@"cameraIrisHollowClose" :相机镜头关闭效果 
Copy code
- (void) animate: (id) sender 

// Set up the animation 
CATransition *animation = [CATransition animation]; 
animation.delegate = self; 
animation.duration = 1.0f; 
animation.timingFunction = UIViewAnimationCurveEaseInOut; 
switch ([(UISegmentedControl *)self.navigationItem.titleView selectedSegmentIndex])  

  case 0: 
  animation.type = kCATransitionFade; 
  break; 
  case 1: 
  animation.type = kCATransitionMoveIn; 
  break; 
  case 2: 
  animation.type = kCATransitionPush; 
  break; 
  case 3: 
  animation.type = kCATransitionReveal; 
  default: 
  break; 

if (isLeft) 
  animation.subtype = kCATransitionFromRight; 
else 
  animation.subtype = kCATransitionFromLeft; 
// Perform the animation 
UIView *whitebg = [self.view viewWithTag:10]; 
NSInteger purple = [[whitebg subviews] indexOfObject:[whitebg viewWithTag:99]]; 
NSInteger white = [[whitebg subviews] indexOfObject:[whitebg viewWithTag:100]]; 
[whitebg exchangeSubviewAtIndex:purple withSubviewAtIndex:white]; 
[[whitebg layer] addAnimation:animation forKey:@"animation"]; 
// Allow or disallow user interaction (otherwise you can touch "through" 
// the cover view to enable/disable the switch) 
if (purple < white)  
  [self.view viewWithTag:99].userInteractionEnabled = YES; 
else  
  [self.view viewWithTag:99].userInteractionEnabled = NO; 
isLeft = !isLeft; 
}
原创粉丝点击