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

本文介绍了一种使用UIKit进行连续动画效果的方法,通过直接调用UIView动画API实现了流畅的过渡效果,并利用NSThread.sleepUntilDate暂停动画播放。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前受某人影响以为一连串的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
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值