简体   繁体   中英

how to load uiview from nib

I am trying to change views when I swipe. I have the swipe working find as I have tested this by changing the background color.

Since then however I have added a new view.nib and set the class to be the same as the current view. Inside this classes .h file I have done this

@interface MasterViewController : UIViewController <UIGestureRecognizerDelegate>{

    UIView *myView;
    UIView *secondView;
}

@property (strong, nonatomic) IBOutlet UIView *myView; // attached to Viewcontroller nib
@property (strong, nonatomic) IBOutlet UIView *secondView; // attached to the secondView


- (void)swipedScreen;

MyView is the main view that appears first, secondView is the view of the nib that I have created and changed its class to relate to this view.

From there I have done this in the .m file

- (void) setupSwipeGestureRecognizer {
    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedScreen)];
    swipeGesture.direction = (UISwipeGestureRecognizerDirectionRight|UISwipeGestureRecognizerDirectionLeft);
    [myView addGestureRecognizer:swipeGesture];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"Prototype";
    [self setupSwipeGestureRecognizer];
}

- (void)swipedScreen
{
    // Not sure what to do here.
    [[NSBundle mainBundle] loadNibNamed:@"SecondView" owner:self options:nil];

}

I just dont know what to do in swipedScreen method to get the secondView to appear.. I would like to animate this view to slid in from the right.. but at the moment that comes secondary to actually just getting the view to appear... not sure what I am doing wrong here but obviously its something quite fundamental.

any help would be greatly appreciated.

As I commented:

- (void)swipedScreen
{
    [UIView animateWithDuration:3.0f delay:0 options:UIViewAnimationOptionCurveLinear
                 animations:^{ 
                         secondView.frame = CGRectMake(180, secondView.frame.origin.y, secondView.frame.size.width, secondView.frame.size.height);
                     }
                 completion:^(BOOL finished){
                         myView.hidden = YES;
                 }];
}

Actually, you'll have to change the X, Y, Width and height values as you want to animate, and when it's completed, I set the main view - myView - hidden.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM