我正在编写一个 iOS 5 应用程序(在 Xcode 4.3 中,使用 Storyboards 和 ARC),它有一些需要响应水平平移的表格单元格。我有一个工作得很好的表格设置,但是我需要在另一个场景中实现相同的行为。我认为最佳实践方法是将手势识别和处理代码抽象到子类中。但是现在tableView不会滚动了,我在旧方法下针对这个问题的解决方案也没有帮助。
我有一个RestaurantViewController
继承自UIViewController
并拥有一个属性ULPanningTableView *tableView
。一些表格的单元格是MenuItemCell
s 并继承自 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
东西是在里面实现的RestaurantViewController
,ULPanningTableViewCell
东西是在里面实现的。对于垂直多于水平MenuItemCell
的手势,我基本上会返回 NO ) translationInView
. 无论如何,我无法让表格滚动!gestureRecognizerShouldBegin:
如果我从 返回 YES或完全删除实现,我可以识别平移手势UIGestureRecognizerDelegate
。
我仍然是 iOS 和 Objective-C 的初学者,所以我只有基于我读过的东西的预感,而且我对类似问题的印象是罪魁祸首正在UIScrollViewPanGestureRecognizer
对响应者链进行巫术。 ..
我将非常感谢您对此有所了解!