簡體   English   中英

如何使用UIView動畫將一個UIView xib移至iOS中的另一個UIView xib?

[英]How to move one UIView xib to another UIView xib in iOS using UIView animations?

我是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)
}
-(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
}];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM