Frage

EDITED:

I had created a slide in menu for my view and it works as i wanted, this was the original guide that I used.

http://www.youtube.com/watch?v=79ZQDzzOHLk

My goal was to get this programmed once in a class and get it to work on any view controller that i decided to call it in.

Thanks to @harsh.prasad and some additional research I have managed to get this to work to a point where it works as I want apart apart from linking buttons on to it.

So to update this question in the hope it may help someone else.

This is what I did:

I created a UIView class and called it MenuOne.

MenuOne.h

#import <UIKit/UIKit.h>

@interface TFMenuOne : UIView {
    // Pop Up Menu

    IBOutlet UIScrollView *scrollView;
    IBOutlet UIButton *openMenu;
    int draw1;
    IBOutlet UIButton *backButton;
}

// Pop Up Menu
- (IBAction)openMenu_clicked:(id)sender;

// Reset draw1 to 0
- (void) resetView: (id) sender;

@property (retain, nonatomic) IBOutlet UIScrollView *scrollView;

@end

MenuOne.m

#import "TFMenuOne.h"

@implementation TFMenuOne
@synthesize scrollView;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        draw1 = 0;

        scrollView = [[UIScrollView alloc] init];
        [scrollView setBackgroundColor:[UIColor whiteColor]];
        [scrollView setFrame:CGRectMake(0, 315, 568, 5)];
        [scrollView setContentSize:CGSizeMake(568, 5)];


        backButton = [[UIButton alloc] init];
        [backButton setBackgroundColor:[UIColor greenColor]];
        backButton.frame = CGRectMake(224, 350, 120, 30);

        openMenu = [[UIButton alloc] init];
        [openMenu setBackgroundImage:[UIImage imageNamed:@"menu-button-@2.png"]
                            forState:UIControlStateNormal];
        openMenu.adjustsImageWhenHighlighted = NO;
        [openMenu addTarget:self
                     action:@selector(openMenu_clicked:)
           forControlEvents:UIControlEventTouchUpInside];
        openMenu.frame = CGRectMake(256, 269, 64, 46);


        [self addSubview:scrollView];
        [self addSubview:backButton];
        [self addSubview:openMenu];

    } 
    return self; 
}

// Allow for touch even through transparent View class
-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    for (UIView *view in self.subviews) {
        if (!view.hidden && view.userInteractionEnabled && [view pointInside:[self convertPoint:point toView:view] withEvent:event])
            return YES;
    }
    return NO;
}

- (void) resetView: (id) sender {
    draw1 = 1;
    [self openMenu_clicked:sender];
}

- (IBAction)openMenu_clicked:(id)sender {
    if (draw1 == 0) {

        draw1 = 1;

        [UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
            scrollView.frame = CGRectMake(0, 260, 568, 60);
        } completion:^(BOOL finished) {

        }];

        [UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
            openMenu.frame = CGRectMake(256, 214, 64, 46);
        } completion:^(BOOL finished) {

        }];

        [UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
            backButton.frame = CGRectMake(224, 275, 120, 30);
        } completion:^(BOOL finished) {

        }];


    } else {

        draw1 = 0;

        [UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
            scrollView.frame = CGRectMake(0, 315, 568, 5);
        } completion:^(BOOL finished) {

        }];

        [UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
            openMenu.frame = CGRectMake(256, 269, 64, 46);
        } completion:^(BOOL finished) {

        }];

        [UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
            backButton.frame = CGRectMake(224, 350, 120, 30);
        } completion:^(BOOL finished) {

        }];

    }
}


/* 
 // Only override drawRect: if you perform custom drawing. 
 // An empty implementation adversely affects performance during animation. 
 - (void)drawRect:(CGRect)rect 
 { 
 // Drawing code 
 } 
 */ 

@end

After a lot of trial and error, in order to get this UIView class to appear on multiple ViewControllers I call the view like this in the m file of the view controller. The obstacles I ran in to was that the menu would open but when I left the view controller to go to another view controller the menu would be in the state it was in when i left, it wouldn't reset back to closed. The code below covered that thanks again to @harsh.prasad. I also managed to get the menu to animate in.

@interface TFMapViewController ()
{
    TFMenuOne *menuView;
}

@end

@implementation TFMapViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [menuView resetView:nil];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    menuView = [[TFMenuOne alloc] initWithFrame:CGRectMake(0, 51, 568, 320)];
    [self.view addSubview:menuView];

}

- (void) viewDidAppear:(BOOL)animated
{

    [UIView animateWithDuration:0.5 delay:0.5 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        menuView.frame = CGRectMake(0, 0, 568, 320);
    } completion:^(BOOL finished) {

    }];

}


- (void) viewDidDisappear:(BOOL)animated
{
    [menuView resetView:nil];
    [UIView animateWithDuration:0.0 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        menuView.frame = CGRectMake(0, 51, 568, 320);
    } completion:^(BOOL finished) {

    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// Allows for Exit button to work
- (IBAction)returned:(UIStoryboardSegue *)segue {

}


@end
War es hilfreich?

Lösung

I you want you can use tabsController if that kind of feature you want, otherwise create a class of your slide in menu view and then you can use it in all of your views as you like. No same piece of code is to be rewritten.

EDIT: Suppose you have created a class CustomMenuView which basically creates your menu view. So now you can use this in every view controller you want to use it in in the following way:

CustomMenuView *menuView = [CustomMenuView alloc] initWithFrame:CGRectMake(0, 200, 320, 100);
menuView.<properties you want to pass> = <set properties here>;
[self.view addSubView:menuView];

This will set the view with the custom menu and then you can handle the actions in this menu.

Andere Tipps

1)Here is an old tutorial for creating menu - http://www.raywenderlich.com/32054/how-to-create-a-slide-out-navigation-like-facebook-and-path/projectlayout

2)A better way is to create Drawer menu With container views. You can read more about container views from WWDC videos.

3)Or if you are lazy to d it yourself, try this library http://cocoacontrols.com/platforms/ios/controls/jtrevealsidebar

P.S. No , you do not havev to repeat the code.

You can try this,

First make a view on the origin point x = 0 y = 480 (for iphone4) and then run this code.

CGRect theFrame = self.viewMenuShare.frame;
theFrame.origin = CGPointMake(0, 480);
self.viewMenuShare.frame = theFrame;
theFrame.origin = CGPointMake(0,480 - 187);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3f];
self.viewMenuShare.frame = theFrame;
[UIView commitAnimations];
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top