编辑:

我已经在菜单中创建了一张幻灯片以供查看,并且可以按我的意愿运行,这是我使用的原始指南。

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

我的目标是在一堂课中对此进行编程,并使其在我决定调用它的任何视图控制器上工作。

感谢@ harsh.prasad和其他一些研究,我设法使它能够工作到一定程度,除了链接按钮之外,我还希望这样做。

因此,更新此问题希望对您有所帮助。

这就是我所做的:

我创建了一个UIView类,并将其命名为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:@"[email protected]"]
                            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

经过大量的试验和错误后,为了使该UIView类出现在多个ViewController上,我在视图控制器的m文件中调用了这样的视图。我遇到的障碍是菜单会打开,但是当我离开视图控制器转到另一个视图控制器时,菜单将处于我离开时的状态,它不会重置为关闭状态。下面的代码再次感谢@ harsh.prasad。我还设法获得了菜单动画。
@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

最佳答案

我希望您可以根据需要使用tabsController,否则可以在菜单视图中创建幻灯片的类,然后可以根据需要在所有视图中使用它。没有相同的代码段将被重写。

编辑:
假设您已经创建了CustomMenuView类,该类基本上创建了菜单视图。因此,现在您可以通过以下方式在要使用它的每个视图控制器中使用它:

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

这将使用自定义菜单设置视图,然后您可以在此菜单中处理操作。

10-06 13:23