如何从左侧打开视图并且应该适用于所有视图控制器

Posted

技术标签:

【中文标题】如何从左侧打开视图并且应该适用于所有视图控制器【英文标题】:How to open view from left hand side and should work on all viewcontrollers 【发布时间】:2012-10-04 06:02:52 【问题描述】:

我想实现如上图所示的功能。 当按下我管理的按钮时,侧面菜单(蓝色面板)应该从左侧打开,但我的问题是 我必须创建这个侧面菜单,以便它可以在其他视图上重复使用

但我不明白我应该如何实现它以便可以在其他视图上重用它?

如果有人有想法请分享一下?

【问题讨论】:

您可以创建一个主视图控制器,例如 parentViewController,然后将其他视图控制器添加为 parentViewController 的子视图。当您打开和关闭父视图控制器时,您的黑色视图控制器框架会发生变化。 在您的窗口中添加视图.... :) 使用侧面板制作视图...并在按钮单击时在右侧添加其他视图 【参考方案1】:

如果您想为所有 UIViewControllers 拥有自己的转换,您可以为 UIViewController 创建一个类别。

这就是你可以做界面的方式:

*.h 文件:

#import <UIKit/UIKit.h>

@interface UIViewController(Transitions)

- (void) presentViewController:(UIViewController *)viewController withPushDirection: (NSString *) direction;
- (void) dismissViewControllerWithPushDirection:(NSString *) direction;

@end

*.m 文件

#import "UIViewControllerWithTransitions.h"
#import <QuartzCore/QuartzCore.h>

@implementation UIViewController(Transitions)

- (void) presentViewController:(UIViewController *)viewController withPushDirection: (NSString *) direction 

    [CATransaction begin];

    CATransition *transition = [CATransition animation];
    transition.type = kCATransitionPush;
    transition.subtype = direction;
    transition.duration = 0.25f;
    transition.fillMode = kCAFillModeForwards;
    transition.removedOnCompletion = YES;

    [[UIApplication sharedApplication].keyWindow.layer addAnimation:transition forKey:@"transition"];        
    [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
    [CATransaction setCompletionBlock: ^ 
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(transition.duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^ 
            [[UIApplication sharedApplication] endIgnoringInteractionEvents];        
        );
    ];

    [self presentViewController:viewController animated:NO completion:NULL];

    [CATransaction commit];



- (void) dismissViewControllerWithPushDirection:(NSString *) direction 

    [CATransaction begin];

    CATransition *transition = [CATransition animation];
    transition.type = kCATransitionPush;
    transition.subtype = direction;
    transition.duration = 0.25f;
    transition.fillMode = kCAFillModeForwards;
    transition.removedOnCompletion = YES;

    [[UIApplication sharedApplication].keyWindow.layer addAnimation:transition forKey:@"transition"];        
    [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
    [CATransaction setCompletionBlock: ^ 
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(transition.duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^ 
            [[UIApplication sharedApplication] endIgnoringInteractionEvents];        
        );
    ];

    [self dismissViewControllerAnimated:NO completion:NULL];

    [CATransaction commit];



@end

这是一个示例调用:

[self presentViewController: myVC withPushDirection:@"fromRight"]; 

【讨论】:

【参考方案2】:

您可以将视图添加到您的窗口...有关详细信息,请参阅下面的链接:)

Programmatically creating Views in ios (how does it work)?

iOS - adding/removing a subview programmatically

【讨论】:

【参考方案3】:

使用这样的东西:

[UIView transitionWithView:self.view duration:0.5 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^["Set your view's new frame here"]; completion:nil];

或者您可以将视图的框架设置为可见区域之外的某个默认位置,然后在动画块中将框架设置为滑出后最终的位置。

[UIView beginAnimation];
//Your new frame here
[UIView commitAnimation];

类似这样的东西......相应地用谷歌搜索......

【讨论】:

【参考方案4】:

首先看看我在我的项目中实现的这种类型的逻辑..

如果需要,这里给出打开和关闭视图的布尔值

-(IBAction)yourButtonOpen_Clicked:(id)sender
    

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.5];
        [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:viewObjectSlider cache:YES];
        viewObjectSlider.frame=CGRectMake(828, 0, 196, 694);// set your frame here
        [UIView commitAnimations];
    
-(IBAction)yourButtonClosed_Click:(id)sender

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:viewObjectSlider cache:YES];
    viewObjectSlider.frame=CGRectMake(984, 0, 196, 694);//set yourFrame
    [UIView commitAnimations];

我在 iPad 应用程序中使用此登录名,所以给这种类型的框架你可以给 iPhone 的框架:)

还可以查看下面的链接,您可以在其中看到一个与您的要求相同的演示..

http://www.highwaystech.com/index.php/source-code/ios/360-iiviewdeckcontroller-for-ios.html

【讨论】:

以上是关于如何从左侧打开视图并且应该适用于所有视图控制器的主要内容,如果未能解决你的问题,请参考以下文章

从视图获取值到控制器,然后到另一个视图

从容器视图呈现视图控制器

添加应在所有视图控制器中可见的按钮

将旁白限制为活动视图控制器

IOS:如何打开视图控制器设置?

XCODE 如何跨不同视图使用 IBAction