I create the views, UI elements etc programmatically.
I’m trying to animate views when they are added or removed. The problem is that only UIButton *button is being animated and it is animated wrong. I mean the button title comes from the top and the button itself comes from the right of the screen.
Please see the code below
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];
Transitions have to be applied to a view that doesn’t get removed or added during the transition. You are adding the transition to self.view, but then you are removing self.view during the transition.
Try adding the transition to self.view.superview instead, like this:
If that still doesn’t work, are you sure this code is right:
Literally, this code is saying:
That means the first two lines are pointless because everything you do to the view is wiped out when you remove it from the screen. And removing the view is a weird thing to do during loadView – the end result would be the view would be nil after loading.