如何使用ios中的动画将一个UIView xib文件推送到另一个UIView xib文件?

时间:2015-11-21 07:01:55

标签: ios objective-c iphone xib

您好我是ios的初学者,而我ViewController我已经以编程方式加载了两个UIView xib个文件,它们是Test1和Test2 我在这个Test1 xib文件中添加了一个按钮。

当我点击此按钮时,我想使用动画移动Test2 xib文件。

为此,我编写了下面的代码,但是当我点击“下一步”按钮时,没有动画发生并且没有正确添加Test2 xib文件(如下图所示)。

mycode的: -

我的MainviwController: -

#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];
}

Test1 xib文件类:

#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];

}

enter image description here

0 个答案:

没有答案