- (void)viewDidLoad {
[super viewDidLoad];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 20, 30,30)];
view.backgroundColor= [UIColor orangeColor];
[self.view addSubview:view];
//__bridge橋接因?yàn)閏ontext參數(shù)是void*類型,而view是UIView類型,不匹配,所以使用__bridge橋接進(jìn)行類型匹配
[UIView beginAnimations:nil context:(__bridgevoid*)(view)];
[UIView setAnimationDuration:2];
//設(shè)置代理
[UIView setAnimationDelegate:self];
//綁定方法
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
view.frame=CGRectMake(200, 200, 50, 50);
[UIView commitAnimations];
}
//自己寫的方法不能進(jìn)行傳參
//-(void)change:(UIView *)view
//系統(tǒng)提示的方法沒有context這個(gè)參數(shù),所以也不能傳參
//-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
//這個(gè)方法是在setAnimationDidStopSelector這個(gè)方法定義的時(shí)候給的,系統(tǒng)把它注釋了,需要傳參數(shù)時(shí)把它復(fù)制出來用就可以了
- (void)animationDidStop:(NSString*)animationID finished:(NSNumber*)finished context:(void*)context {
//把參數(shù)context轉(zhuǎn)換為UIView類型的對(duì)象
UIView *view = (__bridgeUIView*)context;
// NSLog(@"view");
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2];
view.backgroundColor= [UIColor blueColor];
[UIView commitAnimations];
}