UIView动画不适用于UIViewController子类

时间:2012-02-04 17:36:20

标签: ios xcode uiview ios5 uiviewcontroller

我尝试将子视图(它是一个子类UIViewController的视图,由几个UIButtons和UILabels组成)添加到当前视图,但动画不起作用,子视图刚出现时没有动画。

代码是:

[UIView beginAnimations:nil context:nil];
[UIView setAnimaitonCurve:UIViewAnimationCurveLinear]; 
[UIView setAnimationDuration:0.5f];
[UIView setAnimationTransition:transition forView:thisPersonViewController.view cache:YES];
[self.view addSubView:thisPersonViewController.view];
[UIView commitAnimations];

它只是不起作用,但如果我将 forView 参数从子视图(thisPersonViewController.view)更改为 self.view , self.view做了动画,这是古玩。

我的xcode版本是4.2,SDK版本是5.0,对于任何提供解决方案的人来说都是thx!

===========以下代码工作精细===============

CATransition *trans = [CATransition animation];
[trans setDuration:0.4f];
[trans setType:kCATransitionReveal];
[trans setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[thisPersonViewController.view layer]addAnimation:trans forKey:kCATransitionReveal];
[self.view addSubview:thisPersonViewController.view];

另一个令人讨厌的问题是,如果我将CATransition类型更改为 kCATransitionFromBottom 或其他动画再次无法运行的话!

1 个答案:

答案 0 :(得分:1)

  1. 将thisPersonViewController.view添加为子视图,其位置在屏幕的某个位置。 (即框架x / y坐标位于屏幕底部。)

  2. 执行此操作:

  3. 
    [UIView animateWithDuration:0.5f delay:0.0f options:UIViewAnimationCurveLinear animations:^(void)
    {
        // Move it to wherever you want to have it.
        thisPersonViewController.view.frame = CGRectMake(0.0f, 0.0f, thisPersonViewController.view.frame.size.width, thisPersonViewController.view.frame.size.height);
    }
    completion:^(BOOL finished)
    {
        // Do something when it completes the animation if you so desire.
    }];