@implementation ViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView * iv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
iv.image = [UIImage imageNamed:@"moji"];
[self.view addSubview:iv];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationWillStartSelector:@selector(animationDidStart:)];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:)];
[UIView setAnimationDelay:0.0f];
[UIView setAnimationDuration:1.0f];
iv.alpha = 0.0f;
[UIView commitAnimations];
//[iv release];
}
-(void)animationDidStart:(CAAnimation *)anim
{
NSLog(@"animation is start ...");
}
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
NSLog(@"animation is over ...");
}
@end
如上是规范的写法:
注意:
1、一定要写[UIView beginAnimations:nil context:nil];
2、一定要设置代理 因为后两者本质是协议方法
3、要设置选择器[UIView setAnimationWillStartSelector:@selector(animationDidStart:)];
有时候,你不设置选择器,只是设置了代理 如果你实现了 协议中的开始和停止方法,那么程序会执行stop方法,但是不会执行DidStart方法。
为了规范起见,要设置选择器。