您好我是ios的初学者,而我ViewController
我已经以编程方式加载了两个UIView
xib
个文件,它们是Test1和Test2
我在这个Test1 xib
文件中添加了一个按钮。
当我点击此按钮时,我想使用动画移动Test2 xib
文件。
为此,我编写了下面的代码,但是当我点击“下一步”按钮时,没有动画发生并且没有正确添加Test2 xib
文件(如下图所示)。
#import "ViewController.h"
@interface ViewController ()
{
UIView * MainView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
MainView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
MainView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
MainView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:MainView];
//Loading xib files inside MainView;
Test * test1 = [[Test alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
test1.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[MainView addSubview:test1];
Test1 * test2 = [[Test1 alloc]initWithFrame:CGRectMake(test1.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)];
test2.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[MainView addSubview:test2];
}
#import "Test1.h"
@implementation Test1
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
if (self = [super initWithCoder:aDecoder]) {
[self loadingView];
}
return self;
}
-(instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
[self loadingView];
}
return self;
}
-(void)loadingView{
MainView = [[[NSBundle bundleForClass:[self class]]loadNibNamed:@"View1" owner:self
options:nil]firstObject];
[self addSubview:MainView];
MainView.frame = self.bounds;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:@selector(aMethod1:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"NEXT" forState:UIControlStateNormal];
button.backgroundColor = [UIColor blackColor];
button.frame = CGRectMake(80.0, 110.0, 100.0, 30.0);
[MainView addSubview:button];
}
-(void)aMethod1 :(id)sender{
UIView * view2 = [[[NSBundle bundleForClass:[self class]]loadNibNamed:@"Test2" owner:self
options:nil]firstObject];
[UIView transitionWithView:MainView duration:1
options:UIViewAnimationOptionTransitionCurlUp //change animation here
animations:^ {
[self addSubview:view2];
}
completion:nil];
}