如果您创建一个全新的单一视图应用程序并将此代码放在一个按钮后面:
UIView *blah = [[UIView alloc] initWithFrame:CGRectMake(0,100,100)];
blah.backgroundColor = [UIColor grayColor];
[UIView transitionWithView:blah duration:1
options:UIViewAnimationoptionTransitionFlipFromright
animations:^{
[self.view addSubview:blah];
}
completion:^(BOOL finished){
}];
子视图立即添加,没有动画.如果你先添加子视图然后尝试动画它…你会遇到同样的问题.
UIView *blah = [[UIView alloc] initWithFrame:CGRectMake(0,100)];
[self.view addSubview:blah];
[UIView transitionWithView:blah duration:1
options:UIViewAnimationoptionTransitionFlipFromright
animations:^{
blah.backgroundColor = [UIColor grayColor];
}
completion:^(BOOL finished){
}];
在地球上如何为子视图设置动画,或者在添加后立即动画?
解决方法
您通常需要将限制动画的容器放在适当的位置:
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect frame = CGRectMake(0,100);
_container = [[UIView alloc] initWithFrame:frame];
_container.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:_container];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
UIView *subview = [[UIView alloc] initWithFrame:_container.bounds];
subview.backgroundColor = [UIColor darkGrayColor];
[UIView transitionWithView:_container
duration:1.0
options:UIViewAnimationoptionTransitionFlipFromright
animations:^{
[_container addSubview:subview];
}
completion:NULL];
}