2012-02-05 7 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]; 

、このコードは言っている:最初の2行を意味

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中に行うべき変なことです。最終結果は、ロード後にビューがnilになることです。

+0

私はそれを手に入れます。 これは、addSubviewが使用されているときに表示するために適用できる唯一のアニメーションです。 – objlv

+0

申し訳ありませんが、私はあなたのアニメーションコードを誤読しました - あなたは別の種類のアニメーションをやっていると思いました。私は私の答えを更新しました。 –

+0

TViewController.mの更新されたコードを参照してください。私はアニメーションの変化に気付かなかった。 – objlv

関連する問題