ios - 使用左、右和主视图 Controller 将新 View Controller 添加到自定义滑动 View Controller

标签 ios iphone objective-c uiviewcontroller

我创建了一个具有主视图 Controller 、右 View Controller 和左 View Controller 的 SlidingViewController。现在我想添加一个新的函数/方法,只要单击 RightViewController 的 TableView 单元之一,将主视图 Controller 切换到与 indexpath.row 中的 TableView 单元链接的 View Controller 。新的 View Controller 应该仍然能够使这个左右 View Controller 可用。任何人都有创建自己的 SlidingViewController 或 MenuViewController 的经验,可以帮助我解决这个问题吗?任何帮助将不胜感激。先感谢您。

滑动 View Controller .h

@interface SlidingViewController : UIViewController <UIGestureRecognizerDelegate>

@property (retain) UIViewController *mainViewController;
@property (retain) UIViewController *leftViewController;
@property (retain) UIViewController *rightViewController;
@property (retain) NSNumber *leftSwipeEnabled;
@property (assign) BOOL leftDrawerVisible;
@property (retain) NSNumber *rightSwipeEnabled;
@property (assign) BOOL rightDrawerVisible;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil mainViewController:(UIViewController *)main leftViewController:(UIViewController *)left andRightViewController:(UIViewController *)right;
-(void)toggleLeftDrawer;
-(void)toggleRightDrawer;
@end

滑动 View Controller .m
@implementation SlidingViewController
@synthesize mainViewController, leftViewController, rightViewController;
@synthesize leftDrawerVisible, rightDrawerVisible;
@synthesize leftSwipeEnabled, rightSwipeEnabled;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil mainViewController:(UIViewController *)main leftViewController:(UIViewController *)left andRightViewController:(UIViewController *)right
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
    {
        mainViewController = main;
        leftViewController = left;
        rightViewController = right;
        leftSwipeEnabled = [NSNumber numberWithBool:NO];
        rightSwipeEnabled = [NSNumber numberWithBool:NO];
    }

    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self addChildViewController:self.mainViewController];
    [self.view addSubview:[self.mainViewController view]];
    self.mainViewController.view.frame = self.view.frame;
    [self.mainViewController didMoveToParentViewController:self];

    self.mainViewController.view.layer.shadowColor = [UIColor blackColor].CGColor;
    self.mainViewController.view.layer.shadowOpacity = 0.2f;
    self.mainViewController.view.layer.shadowRadius = 5.0f;
    CGPathRef path = [UIBezierPath bezierPathWithRect:self.mainViewController.view.bounds].CGPath;
    self.mainViewController.view.layer.shadowPath = path;

    if(self.leftViewController != nil)
    {
        UISwipeGestureRecognizer *leftSwipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
        leftSwipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
        leftSwipeRecognizer.delegate = self;
        [self.mainViewController.view addGestureRecognizer:leftSwipeRecognizer];
    }
    if(self.rightViewController != nil)
    {
        UISwipeGestureRecognizer *rightSwipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
        rightSwipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
        rightSwipeRecognizer.delegate = self;
        [self.mainViewController.view addGestureRecognizer:rightSwipeRecognizer];
    }



}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self layoutShadowWithDuration:0];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    if(self.leftDrawerVisible)
        [self toggleLeftDrawer];
    else if(self.rightDrawerVisible)
        [self toggleRightDrawer];
}
-(BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}
-(void)handleSwipe:(UISwipeGestureRecognizer *)recognizer
{

    if(recognizer.state == UIGestureRecognizerStateEnded)
    {
        if(recognizer.direction == UISwipeGestureRecognizerDirectionRight && [self.leftSwipeEnabled boolValue])
            [self toggleLeftDrawer];
        else if(recognizer.direction == UISwipeGestureRecognizerDirectionLeft && [self.rightSwipeEnabled boolValue])
            [self toggleRightDrawer];
    }
}
-(void)toggleLeftDrawer
{
    if (self.rightDrawerVisible)
    {
        return;
    }
    if(self.leftDrawerVisible)
    {
        [UIView animateWithDuration:0.2
                              delay:0.0
                            options:UIViewAnimationOptionCurveLinear
                         animations:^{
                             self.mainViewController.view.frame = CGRectMake(0.0, 0.0, self.mainViewController.view.frame.size.width, self.mainViewController.view.frame.size.height);}
         completion:^(BOOL finished)
         {
             [self.leftViewController.view removeFromSuperview];
             [self.leftViewController willMoveToParentViewController:nil];
             [self.leftViewController removeFromParentViewController];
         }];
        self.leftDrawerVisible = NO;
        self.mainViewController.view.userInteractionEnabled = YES;
    }
    else
    {
        [self addChildViewController:self.leftViewController];
        [self.view insertSubview:[self.leftViewController view] belowSubview:[self.mainViewController view]];
        [self.leftViewController didMoveToParentViewController:self];

        CGPathRef path = [UIBezierPath bezierPathWithRect:self.mainViewController.view.bounds].CGPath;
        self.mainViewController.view.layer.shadowPath = path;

        self.mainViewController.view.layer.shadowOffset = CGSizeMake(-3, 0);
        NSInteger width = 260;

        if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)
            width = 320;

        self.leftViewController.view.frame = CGRectMake(0, 0, width, self.view.bounds.size.height);

        [UIView animateWithDuration:0.2
                              delay:0.0
                            options:UIViewAnimationOptionCurveLinear
                         animations:^{ self.mainViewController.view.frame = CGRectMake(width, 0, self.mainViewController.view.frame.size.width, self.mainViewController.view.frame.size.height); }
                         completion:^(BOOL finished) { self.leftViewController.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin; }];
        self.leftDrawerVisible = YES;
        self.mainViewController.view.userInteractionEnabled = NO;
    }
}
-(void)toggleRightDrawer
{
    if(self.leftDrawerVisible)
        return;

    if(self.rightDrawerVisible)
    {
        [UIView animateWithDuration:0.2
                              delay:0.0
                            options:UIViewAnimationOptionCurveLinear
                         animations:^{
                             self.mainViewController.view.frame = CGRectMake(0.0, 0.0, self.mainViewController.view.frame.size.width, self.mainViewController.view.frame.size.height);
                         }
                         completion:^(BOOL finished){
                             [self.rightViewController.view removeFromSuperview];
                             [self.rightViewController willMoveToParentViewController:nil];
                             [self.rightViewController removeFromParentViewController];
                         }];
        self.rightDrawerVisible = NO;
        self.mainViewController.view.userInteractionEnabled = YES;
    }
    else
    {
        [self addChildViewController:self.rightViewController];
        [self.view insertSubview:[self.rightViewController view] belowSubview:[self.mainViewController view]];
        [self.rightViewController didMoveToParentViewController:self];

        CGPathRef path = [UIBezierPath bezierPathWithRect:self.mainViewController.view.bounds].CGPath;
        self.mainViewController.view.layer.shadowPath = path;

        self.mainViewController.view.layer.shadowOffset = CGSizeMake(3, 0);

        NSInteger width = 260;
        if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)
            width = 320;
        self.rightViewController.view.frame = CGRectMake(self.view.bounds.size.width- width, 0, width, self.view.bounds.size.height);
        [UIView animateWithDuration:0.2 delay:0.0
                            options:UIViewAnimationOptionCurveLinear
                         animations:^{
                             self.mainViewController.view.frame = CGRectMake(-width, 0, self.mainViewController.view.frame.size.width, self.mainViewController.view.frame.size.height);
                         }
                         completion:^(BOOL finished){
                             self.rightViewController.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin;
                         }];
        self.rightDrawerVisible = YES;
        self.mainViewController.view.userInteractionEnabled = NO;
    }
}

-(void) layoutShadowWithDuration:(NSTimeInterval)duration
{
    CGPathRef oldShadowPath = self.mainViewController.view.layer.shadowPath;

    if (oldShadowPath)
    {
        CFRetain(oldShadowPath);
    }

    // Update shadow path for the view
    CGPathRef path = [UIBezierPath bezierPathWithRect:self.mainViewController.view.bounds].CGPath;
    self.mainViewController.view.layer.shadowPath = path;

    // You would think setting duration to 0 would cause the animation added below to not animate. You would be wrong.
    if (duration != 0)
    {
        if (oldShadowPath)
        {
            [self.mainViewController.view.layer addAnimation:((^ {
                CABasicAnimation *transition = [CABasicAnimation animationWithKeyPath:@"shadowPath"];
                transition.fromValue = (__bridge id)oldShadowPath;
                transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
                transition.duration = duration;
                return transition;
            })()) forKey:@"transition"];
            CFRelease(oldShadowPath);
        }
        else
            if (oldShadowPath)
                CFRelease(oldShadowPath);
    }
    else
        if (oldShadowPath)
            CFRelease(oldShadowPath);
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [self layoutShadowWithDuration:duration];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
@end

最佳答案

您可以通过“委托(delegate)设计模式”简单地做到这一点

在 RightViewController.h 中添加以下行。

@protocol RightViewControllerDelegate <NSObject>

@required
   - (void)somethingClicked : (id)theObject;

@end

@interface RightViewController : UIViewController

@property (nonatomic, assign) id<RightViewControllerDelegate> delegate;

@end

在 RightViewController.m 中添加以下行。
- (void)somethingClicked : (id)theObject
{
   [_delegate somethingClicked:theObject];
}

使用以下代码更新 MainViewController.h。
@interface MainViewController : UIViewController <RightViewControllerDelegate>

最后在 MainViewController.m 中添加以下代码
- (void)somethingClicked : (id)theObject
{
   // Perform Your Task
}

关于ios - 使用左、右和主视图 Controller 将新 View Controller 添加到自定义滑动 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19869883/

相关文章:

iphone - Iphone上的TCP套接字

iphone - 获取 UIView 中的 subview

C 函数总是向 Objective C 返回零

ios - 如何使用 Siri 快捷方式在我的应用程序中显示特定页面?

javascript - 排毒 _ 无法键入字符串,因为键盘未显示在屏幕上

iphone - 从长度为 4000+ 的 NSData 转换时,NSMutableString 变为 (null)

iphone - IOS:将语言添加为 Localizable.strings

objective-c - 在数组中搜索最接近的浮点值

ios - 如何将iTunesAccount帐户添加或链接到其他人的Apple Developer帐户?

ios - Ad-Hoc 配置文件显示 "Certificate not installed",即使 App Store 配置文件正常?