如何使用UIView动画将一个UIView xib移动到iOS中的另一个UIView xib?

时间:2015-11-20 12:59:08

标签: ios objective-c

我在iOS上很新。在我的项目中,我添加了两个UIView xib文件,它们是“View1”xib文件和“View2”xib文件,在这里我在View1 xib文件中添加了一个按钮。

当我点击该按钮时,我想使用UIView动画移动下一个UIView xib文件,即View2(就像我们将一个UIViewController推送到另一个UIViewcontroller一样#import "Test.h" @implementation Test @synthesize MainArray; -(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{ UIView * 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(aMethod:) forControlEvents:UIControlEventTouchUpInside]; [button setTitle:@"Show View" forState:UIControlStateNormal]; button.backgroundColor = [UIColor blackColor]; button.frame = CGRectMake(80.0, 110.0, 100.0, 30.0); [MainView addSubview:button]; } -(void)aMethod :(id)sender{ //here when i click i want to move next UIView (i.e VIew2 xib file) }

我的代码:

$, = "\t"

1 个答案:

答案 0 :(得分:0)

-(void)aMethod :(id)sender{

    //here when i click i want to move next UIView (i.e VIew2 xib file)

    UIView * view2 = [[[NSBundle bundleForClass:[self class]]loadNibNamed:@"View2" owner:self
                                                                     options:nil]firstObject];

    [UIView transitionWithView:containerView duration:1
            options:UIViewAnimationOptionTransitionCurlUp //change animation here
            animations:^ { 
                [self addSubview:view2]; 
            }
            completion:nil];
}

   -(void)aMethod :(id)sender{

            //here when i click i want to move next UIView (i.e VIew2 xib file)

        UIView * view2 = [[[NSBundle bundleForClass:[self class]]loadNibNamed:@"View2" owner:self
                                                                             options:nil]firstObject];

        CATransition *transition = [CATransition animation];
        transition.duration = 1.0;
        transition.type = kCATransitionPush; //change animation here
        [view2.layer addAnimation:transition forKey:nil];
        [self addSubview:view2]; 
    }

[self addSubview:view2];
[view2 setFrame:CGRectMake( 0.0f, 480.0f, 320.0f, 480.0f)];
[UIView beginAnimations:@"animateView" context:nil];
[UIView setAnimationDuration:1];
[view2 setFrame:CGRectMake( 0.0f, 0.0f, 320.0f, 480.0f)]; // move in
[UIView commitAnimations];

[self.view addSubview:view2];
[view2 setFrame:CGRectMake( 0.0f, 480.0f, 320.0f, 480.0f)];
[UIView animateWithDuration:1 animations:^{
  [view2 setFrame:CGRectMake( 0.0f, 0.0f, 320.0f, 480.0f)]; // move in
}];
相关问题