0

我不理解 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){} ];

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

非常感谢

4

1 回答 1

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){}];
}
于 2012-08-27T04:55:59.010 回答