尝试实现UILongPressGesture时应用程序崩溃

时间:2013-05-03 10:26:18

标签: iphone ios objective-c sdk

我正在尝试在我的一个表上实现UILongPressGesture,以便能够重新排序其单元格。但是,每次我长按我的应用程序崩溃。

这是实现UILongPressGesture的文件:

#import "LstrTableViewDragToMove.h"
#import "LstrTableViewCell.h"

@implementation LstrTableViewDragToMove
{
    LstrTableView *_tableView;
    BOOL _dragInProgress;
    CGPoint _initialTouchPoint;
    int _cellIndex;
    LstrTableViewCell *_currentCell;

}

-(id)initWithTableView:(LstrTableView *)tableView
{
    self = [super init];
    if(self)
    {
        _tableView = tableView;
        UIGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
        [_tableView addGestureRecognizer:recognizer];
    }

    return self;

}

-(BOOL) viewContainsPoint:(UIView*)view withPoint:(CGPoint)point {
    CGRect frame = view.frame;
    return (frame.origin.y < point.y) && (frame.origin.y + frame.size.height) > point.y;
}

-(void)handleLongPress:(UILongPressGestureRecognizer *)recognizer
{
    if (recognizer.state == UIGestureRecognizerStateBegan) {
        [self longPressStarted:recognizer];
    }

    if (recognizer.state == UIGestureRecognizerStateEnded) {
        [self longPressEnded:recognizer];
    }
}

-(void)longPressStarted:(UILongPressGestureRecognizer *)recognizer
{
    NSLog(@"Started");
    /*
    _initialTouchPoint = [recognizer locationOfTouch:0 inView:_tableView.scrollView];
    NSArray* visibleCells = _tableView.visibleCells;
    for (int i=0; i < visibleCells.count; i++) {
        UIView* cell = (UIView*)visibleCells[i];
        if ([self viewContainsPoint:cell withPoint:_initialTouchPoint]) {
            _cellIndex = i;
        }
    }

    _currentCell = _tableView.visibleCells[_cellIndex];
    _currentCell.label.allowsEditingTextAttributes = NO;
    _currentCell.frame = CGRectMake(_currentCell.frame.origin.x, _currentCell.frame.origin.y, _currentCell.frame.size.width + 10.0f, _currentCell.frame.size.height + 10.0f);
    [_currentCell addDropShadow];
     */
}


-(void)longPressEnded:(UILongPressGestureRecognizer *)recognizer
{
    NSLog(@"Ended");
}

@end

这是错误日志:

2013-05-03 13:17:53.750 Lstr[19207:907] -[UILongPressGestureRecognizer translationInView:]: unrecognized selector sent to instance 0x20856240
2013-05-03 13:17:53.754 Lstr[19207:907] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UILongPressGestureRecognizer translationInView:]: unrecognized selector sent to instance 0x20856240'
*** First throw call stack:
(0x3302a2a3 0x3ad5297f 0x3302de07 0x3302c531 0x3302d938 0x6db43 0x34f4f1ab 0x34f1863f 0x34f482a5 0x33941f53 0x32fff5df 0x32fff291 0x32ffdf01 0x32f70ebd 0x32f70d49 0x36b492eb 0x34e86301 0x66775 0x3b189b20)
libc++abi.dylib: terminate called throwing an exception
(lldb)

提前致谢。

更新

这是具有translationInView的方法:

#pragma mark - horizontal pan gesture methods
-(BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer {
    CGPoint translation = [gestureRecognizer translationInView:[self superview]];
    // Check for horizontal gesture
    if (fabsf(translation.x) > fabsf(translation.y)) {
        return YES;
    }
    return NO;
}

2 个答案:

答案 0 :(得分:2)

您是否在代码中的某处使用translationInView

您应该将其更改为locationInView

答案 1 :(得分:-1)

尝试将您的tableview声明为头文件中的属性

@property(nonatomic,retain)LstrTableView *_tableView;
相关问题