UIViewTransitionfromView:永远不会工作,只显示第二个图像

时间:2012-08-26 22:57:37

标签: objective-c ios

我疯狂地不理解UIViewTransitionfromView的奇怪行为:第一个视图首先显示没有动画。这是代码:

UIImageView *backstar=[[UIImageView alloc]initWithFrame:CGRectMake(50, 50, 205, 205)];
    backstar.image=[UIImage imageNamed:@"backstar.png"];

UIImageView *star=[[UIImageView alloc]initWithFrame:CGRectMake(50, 50, 205, 205)];
star.image=[UIImage imageNamed:@"star.png"];

UIView *containerView=[[UIView alloc]initWithFrame:CGRectMake(50, 50, 205, 205)];
containerView.backgroundColor=[UIColor blackColor];
[self.view addSubview:containerView];
[containerView addSubview:backstar]; // if I don't do this nothing happens, container view stays f*** deep black
[containerView addSubview:star];

//if i remove the animation line, star is displayed of course, on top of backstar

[UIView transitionFromView:star toView:backstar  duration:2.0 options:UIViewAnimationOptionShowHideTransitionViews | UIViewAnimationOptionTransitionFlipFromLeft completion:^(BOOL finished){} ];

到底哪里是我的错?应该可以不删除imageViews,而是显示/隐藏它们,如Apple doc中所述!

非常感谢

1 个答案:

答案 0 :(得分:0)

从你的帖子中你不清楚你有这个代码。如果你在viewDiLoad或viewWillAppear中拥有它,我发现它不起作用。您需要获取第一个视图并且可见(在viewDidLoad或viewWillAppear中),然后添加第二个视图并在另一个方法中进行转换(可以使用按钮或performSelector调用:withObject:afterDelay :)。此外,如果您想从星号转到后星号,则应首先将星号添加到视图中,然后仅在您想要进行过渡时添加后星号。这对我有用:

@implementation TransitionTestViewController
@synthesize star,backstar;

-(void)viewDidLoad {
    self.backstar=[[UIImageView alloc]initWithFrame:CGRectMake(50, 50, 205, 205)];
    backstar.image=[UIImage imageNamed:@"backstar.png"];
    self.star=[[UIImageView alloc]initWithFrame:CGRectMake(50, 50, 205, 205)];
    star.image=[UIImage imageNamed:@"star.png"];
    [self.view addSubview:star];
    //[self performSelector:@selector(flipViews:) withObject:nil afterDelay:.01];
}

-(IBAction)flipViews:(UIButton *) sender {
    [self.view addSubview:backstar];
    [UIView transitionFromView:self.star toView:self.backstar  duration:2.0 options: UIViewAnimationOptionShowHideTransitionViews |UIViewAnimationOptionTransitionFlipFromLeft completion:^(BOOL finished){}];
}