0

现在我正在开发一个 iPad 应用程序,它使用 UITableviewCell 的子类,它使用 UIPanGestureRecognizer 左右滑动,但是它只向右滑动,向左滑动......这是我正在使用的代码:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString           *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) 
    {
    // Initialization code
                  UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self        action:@selector(handlePan:)];
    pan.delegate = self;

    [self addGestureRecognizer:pan];

}
return self;
}

这是我的 handlePan: 方法,

 CGPoint translation = [gesture locationInView:self];
    self.center = CGPointMake(self.center.x + translation.x,
                              self.center.y);
    if (self.center.x > 700) {
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:self.tag],@"number",nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"right" object:nil userInfo:dic];
        dic = nil;
    }
    if (self.center.x < 0){
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:self.tag],@"number",nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"left" object:nil userInfo:dic];
        dic = nil;
        NSLog(@"Left Called");


    }
    [gesture setTranslation:CGPointZero inView:self];

无论我尝试什么,我似乎都无法让控制台说“Left Called”,即单元格向左滑动。我真的在这个问题上苦苦挣扎,并希望得到任何帮助。

4

2 回答 2

2

诀窍是对锅的变化状态做出反应,中心计算可以简单得多......

- (void)handlePan:(UIPanGestureRecognizer *)gesture {

    if ((gesture.state == UIGestureRecognizerStateChanged) ||
        (gesture.state == UIGestureRecognizerStateEnded)) {

        CGPoint newCenter = CGPointMake([gesture locationInView:self.superview].x, self.center.y);
        self.center = newCenter;

        // To determine the direction of the pan, check the sign of translation in view.
        // This supplies the cumulative translation...
        if ([gesture translationInView:self.superview].x > 0) {
            NSLog(@">>>");
        } else {
            NSLog(@"<<<");
        }
    }
}
于 2013-03-03T06:36:55.490 回答
1

我认为您确定左/右的方式可能存在缺陷。

CGPoint translation = [gesture locationInView:self];
self.center = CGPointMake(self.center.x + translation.x,
                              self.center.y);

self.center.x 总是积极的,因为“翻译”是视图中积极的位置。您可能想要跟踪原始触摸位置,然后将其与滑动/移动时的位置进行比较。尝试这样的事情:

- (void)handlePan:(UIPanGestureRecognizer *)panRecognizer {

    if (panRecognizer.state == UIGestureRecognizerStateBegan)
    {
        // create a local property for the original point and assign the position on first touch
        self.originalPanTouchPoint = [panRecognizer locationInView:self.view];
        // create a local property for cell's center prior to pan
        self.initialCellCenter = self.center;
    } else {
        CGPoint currentTouchPoint = [panRecognizer locationInView:self.view];
        CGFloat xTranslation = self.originalPanTouchPoint.x - currentTouchPoint.x;

        self.center = CGPointMake(self.initialCellCenter.x + xTranslation, self.initialCellCenter.y);
    }
}
于 2013-03-03T03:26:10.523 回答