0

我正在编写一个 iOS 5 应用程序(在 Xcode 4.3 中,使用 Storyboards 和 ARC),它有一些需要响应水平平移的表格单元格。我有一个工作得很好的表格设置,但是我需要在另一个场景中实现相同的行为。我认为最佳实践方法是将手势识别和处理代码抽象到子类中。但是现在tableView不会滚动了,我在旧方法下针对这个问题的解决方案也没有帮助。

我有一个RestaurantViewController继承自UIViewController并拥有一个属性ULPanningTableView *tableView。一些表格的单元格是MenuItemCells 并继承自 ULPanningTableViewCell。表的委托和数据源是RestaurantViewController.

ULPanningTableViewCell继承自原始版本UITableViewCell并且非常接近原始版本,唯一的区别是它具有跟踪单元格的正面和背面视图以及自定义背景的属性。

ULPanningTableView有点复杂,因为它必须设置识别和处理。

ULPanningTableView.h

#import <UIKit/UIKit.h>

@interface ULPanningTableView : UITableView <UIGestureRecognizerDelegate>

@property (nonatomic) float openCellLastTX;
@property (nonatomic, strong) NSIndexPath *openCellIndexPath;

- (id)dequeueReusablePanningCellWithIdentifier:(NSString *)identifier;
- (void)handlePan:(UIPanGestureRecognizer *)panGestureRecognizer;
// ... some helpers for handlePan:

@end

ULPanningTableView.m

#import "ULPanningTableView.h"
#import "ULPanningTableViewCell.h"

@implementation ULPanningTableView

@synthesize openCellIndexPath=_openCellIndexPath, openCellLastTX=_openCellLastTX;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

#pragma mark - Table View Helpers

- (id)dequeueReusablePanningCellWithIdentifier:(NSString *)identifier
{
    ULPanningTableViewCell *cell = (ULPanningTableViewCell *)[self dequeueReusableCellWithIdentifier:identifier];

    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    [panGestureRecognizer setDelegate:self];
    [cell addGestureRecognizer:panGestureRecognizer];

    return cell;
}

#pragma mark - UIGestureRecognizerDelegate protocol

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    // for testing: only allow UIScrollViewPanGestureRecognizers to begin
    NSString *gr = NSStringFromClass([gestureRecognizer class]);
    if ([gr isEqualToString:@"UIScrollViewPanGestureRecognizer"]) {
        return YES;
    } else {
        return NO;
    }
}

#pragma mark - panhandling

- (void)handlePan:(UIPanGestureRecognizer *)panGestureRecognizer
{
    // ...
}
// ... some helpers for handlePan:

@end

我玩过gestureRecognizerShouldBegin:,因为当这些不是单独的类时,我就是这样解决这个问题的(ULPanningTableView东西是在里面实现的RestaurantViewControllerULPanningTableViewCell东西是在里面实现的。对于垂直多于水平MenuItemCell的手势,我基本上会返回 NO ) translationInView. 无论如何,我无法让表格滚动!gestureRecognizerShouldBegin:如果我从 返回 YES或完全删除实现,我可以识别平移手势UIGestureRecognizerDelegate

我仍然是 iOS 和 Objective-C 的初学者,所以我只有基于我读过的东西的预感,而且我对类似问题的印象是罪魁祸首正在UIScrollViewPanGestureRecognizer对响应者链进行巫术。 ..

我将非常感谢您对此有所了解!

4

1 回答 1

0

好吧,所以我觉得很傻。-handlePan:已经是方法了!我将其更改为-handleCustomPan:,它将正常处理其他平底锅。我不确定为什么它没有崩溃,但它确实存在。哦,我不得不将 UIScrollViewPanGestureRecognizer 边缘案例保留在-gestureRecognizerDidBegin:

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    NSString *gr = NSStringFromClass([gestureRecognizer class]);
    if ([gr isEqualToString:@"UIScrollViewPanGestureRecognizer"]) {
        // allow scroll to begin
        return YES;
    } else if ([gr isEqualToString:@"UIPanGestureRecognizer"]){
        UIPanGestureRecognizer *panGR = (UIPanGestureRecognizer *)gestureRecognizer;
        // allow horizontal pans to begin
        ULPanningTableViewCell *cell = (ULPanningTableViewCell *)[panGR view];
        CGPoint translation = [panGR translationInView:[cell superview]];
        BOOL should = (fabs(translation.x) / fabs(translation.y) > 1) ? YES : NO;

        if (!should) {
            [self closeOpenCellAnimated:YES];
        }

        return should;

    } else {
        NSLog(@"%@",gestureRecognizer);
        return NO;
    }
}
于 2012-04-02T17:42:01.213 回答