我以编程方式创建视图,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 :(得分: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
这意味着前两行毫无意义,因为当您从屏幕上移除视图时,您对视图所做的一切都将被消除。在loadView期间删除视图是一件奇怪的事情 - 最终结果是加载后视图将为零。