2012-02-05 44 views
0

我以编程方式创建视图,UI元素等。 我试图在添加或删除时添加动画视图。问题是只有UIButton *按钮被动画,并且它的动画是错误的。我的意思是按钮标题来自顶部,按钮本身来自屏幕右侧。切换视图期间奇怪的动画

请参考下面

代码AppDelegate.h

#import <UIKit/UIKit.h> 
#import "TViewController.h" 
@interface AppDelegate : UIResponder <UIApplicationDelegate> 
{ 
    TViewController *tv; 
} 

@property (strong, nonatomic) TViewController *tv; 
@property (strong, nonatomic) UIWindow *window; 

@end 

AppDelegate.m .....

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 
    self.window.backgroundColor = [UIColor whiteColor]; 
    self.tv = [[TViewController alloc]initWithNibName:@"TViewController" bundle:nil]; 
    self.window.rootViewController = self.tv; 

    [self.window makeKeyAndVisible]; 
    return YES; 
} 

TViewController.h

#import <UIKit/UIKit.h> 
#import "Elements.h" 
@interface TViewController : UIViewController 
{ 
    Elements *el; 
} 

@property (nonatomic, retain) Elements *el; 
@end 

TViewController.m

-(void)loadView 
{ 
    self.view = [[UIView alloc]initWithFrame:[UIScreen mainScreen].applicationFrame]; 

    self.el = [[Elements alloc] 
       initWithNibName:@"Elements" 
       bundle:nil]; 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:3.0]; 
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view.superview cache:YES]; 
    [self.view addSubview:self.el.view]; 
    self.view.backgroundColor = [UIColor grayColor]; 
    [self.view removeFromSuperview]; 
    [UIView commitAnimations]; 

} 

Elements.m

self.view = [[UIView alloc]initWithFrame:[UIScreen mainScreen].applicationFrame]; 

    UILabel *howManyUsersLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, 300, 150)]; 
    howManyUsersLabel.text = @"Label ..."; 
    [self.view addSubview:howManyUsersLabel]; 


    UIPickerView *playersPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(10, 150, 250, 100)]; 
    playersPickerView.delegate = self; 
    playersPickerView.showsSelectionIndicator = YES; 
    [self.view addSubview:playersPickerView]; 


    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    button.frame = CGRectMake(20, 370, 280, 50); 
    [button setTitle:@"Button" forState:UIControlStateNormal]; 

    [self.view addSubview:button]; 

回答

0

过渡必须被施加到不会被除去或过渡过程中加入的图。您正在向self.view添加转换,但在转换过程中您将删除self.view。

尝试添加过渡到self.view.superview代替,就像这样:

[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view.superview cache:YES]; 

如果仍然不行,你确定这个代码是正确的:

[self.view addSubview:self.el.view]; 
self.view.backgroundColor = [UIColor grayColor]; 
[self.view removeFromSuperview]; 

字面上,此代码是说:

add self.el.view as a subview to self.view 
set background colour of self.view to gray 
remove self.view from the screen 

这意味着前两行是毫无意义的,因为所有y当你将它从屏幕上移除时,对视图的干扰就会消失。在loadView过程中去除视图是一件很奇怪的事情 - 最终的结果是视图在加载后是零。

+0

我明白了。 因此,这是唯一一种可用于查看addSubview何时使用的动画? – objlv 2012-02-05 16:34:30

+0

对不起,我误解了你的动画代码 - 我以为你正在做一种不同类型的动画。我已经更新了我的答案。 – 2012-02-05 16:39:02

+0

请参阅TViewController.m的更新代码。我没有注意到动画的任何变化。 – objlv 2012-02-05 16:44:37