您的位置:首页 > 移动开发 > IOS开发

iOS7自定义back按钮和pop交互手势

2013-10-10 21:58 113 查看
Clambake for iPhone有一个回退按钮在所有的导航条上.这是一个简单的没有文字箭头.

实现一个自定义按钮是简单的.类似这个设置controller 的navigationItem一个leftBarButtonItem.

- (void)viewDidLoad
{
self.navigationItem.leftBarButtonItem = [self backButton];
}

- (UIBarButtonItem *)backButton
{
UIImage *image = [UIImage imageNamed:@"back_button"];
CGRect buttonFrame = CGRectMake(0, 0, image.size.width, image.size.height);

UIButton *button = [[UIButton alloc] initWithFrame:buttonFrame];
[button addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[button setImage:[UIImage imageNamed:normalImage] forState:UIControlStateNormal];

UIBarButtonItem *item; = [[UIBarButtonItem alloc] initWithCustomView:button];

return item;
}


但是这样在iOS7上 pop手势交互就不好使了.我发现了一个轻松解决的办法.

通过我的beta测试者,我收到了很多关于pop手势的崩溃日志.

我发现在栈中推入一个controller后,快速向左平滑,将会引起崩溃.

换句话说,如果用户在推入还在进行的时候立即去点击返回.那么导航控制器就秀逗了.

我在调试日志里面发现这些:

nested pop animation can result in corrupted navigation bar


经过几个小时的奋斗和尝试,我发现可以缓解这个错误:

设置手势的delegate为这个导航控制器

就像Stuart Hall在他的帖子说的那样,分配了一个手势交互行为的委托在自定义按钮显示的时候.然后,当用户快速点击退出的时候,控制器因为手势发送了一个消息在本身已经被销毁的时候.

我的解决方案是简单的让NavigationController自己成为响应的接受者.最好用一个UINavigationController的子类.

@interface CBNavigationController : UINavigationController <UIGestureRecognizerDelegate>
@end

@implementation CBNavigationController

- (void)viewDidLoad
{
__weak CBNavigationController *weakSelf = self;

if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
{
self.interactivePopGestureRecognizer.delegate = weakSelf;
}
}

@end


在转场/过渡的时候禁用
interactivePopGestureRecognizer


当用户在转场的时候触发一个后退手势,则各种事件又凑一块了.导航栈内又成了混乱的.我的解决办法是,转场效果的过程中禁用手势识别,当新的视图控制器加载完成后再启用.再次建议使用UINavigationController的子类操作.

@interface CBNavigationController : UINavigationController <UINavigationControllerDelegate, UIGestureRecognizerDelegate>
@end

@implementation CBNavigationController

- (void)viewDidLoad
{
__weak CBNavigationController *weakSelf = self;

if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
{
self.interactivePopGestureRecognizer.delegate = weakSelf;
self.delegate = weakSelf;
}
}

// Hijack the push method to disable the gesture

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
self.interactivePopGestureRecognizer.enabled = NO;

[super pushViewController:viewController animated:animated];
}

#pragma mark UINavigationControllerDelegate

- (void)navigationController:(UINavigationController *)navigationController
didShowViewController:(UIViewController *)viewController
animated:(BOOL)animate
{
// Enable the gesture again once the new controller is shown

if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
self.interactivePopGestureRecognizer.enabled = YES;
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐