`
xhanxhanxhan
  • 浏览: 203303 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

原来UIView Animation 可以这么写(看错咯)

 
阅读更多

之前受某人影响以为一连串的UIView Animation 只能这么写:

在某个animation 设置delegate ,然后在 delegate 函数中再调用另一个函数。

 

今天偷闲决定看 iPhone cookbook 代码查漏补缺下,结果发现这代码:

 

	// 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;

 

tnnd 原来可以这么写。

同时学到个新玩意。

 

[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0f]];

 

PS. 原来这个例子就叫做 Modal View Animation 罪过罪过,搞了这么久iPhone还不知道这东西。

 

####################################################

####        抱歉,看错了,原来是作者自己实现的方法                              ########

####################################################

 

仔细一看原来

 

commitModalAnimations

具体代码实现是这样的。

@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
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics