UIView动画传参

- (void)viewDidLoad {

[super viewDidLoad];

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 20, 30,30)];

view.backgroundColor= [UIColor orangeColor];

[self.view addSubview:view];

//__bridge桥接因为context参数是void*类型,而view是UIView类型,不匹配,所以使用__bridge桥接进行类型匹配

[UIView beginAnimations:nil context:(__bridgevoid*)(view)];

[UIView setAnimationDuration:2];

//设置代理

[UIView setAnimationDelegate:self];

//绑定方法

[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];

view.frame=CGRectMake(200, 200, 50, 50);

[UIView commitAnimations];

}

//自己写的方法不能进行传参

//-(void)change:(UIView *)view

//系统提示的方法没有context这个参数,所以也不能传参

//-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag

//这个方法是在setAnimationDidStopSelector这个方法定义的时候给的,系统把它注释了,需要传参数时把它复制出来用就可以了

- (void)animationDidStop:(NSString*)animationID finished:(NSNumber*)finished context:(void*)context {

//把参数context转换为UIView类型的对象

UIView *view = (__bridgeUIView*)context;

// NSLog(@"view");

[UIView beginAnimations:nil context:nil];

[UIView setAnimationDuration:2];

view.backgroundColor= [UIColor blueColor];

[UIView commitAnimations];

}

你可能感兴趣的:(UIView动画传参)