1

我正在尝试使用UIPanGestureRecognizer在两点之间滑动按钮(如音量滑块)。以下代码允许我来回滑动按钮,但按钮不会每次都停在同一点(即,当我向右滑动时,有时它会停在它应该停的地方,有时它会停+/ - 距离应有的位置 10 个像素)。我究竟做错了什么?

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

    CGPoint location = [recognizer locationInView:self];


    // 1
    if (recognizer.state == UIGestureRecognizerStateBegan) {
        // if the gesture just started, record current center location
        _originalCenter = self.sliderButton.center;
    }

    // 2
    if (recognizer.state == UIGestureRecognizerStateChanged) {
        // move the checkmarks and main label based on touch
        //CGPoint translation = [recognizer translationInView:self];

        // move slider button
        if (location.x < 70 + 178 && location.x > 70) {
            self.sliderButton.center = CGPointMake(location.x,  _originalCenter.y);
        } 

        // determine whether the item has been dragged far enough to initiate a removal
        if (location.x > 170 + 70) {
            _draggedToEnd = YES;
        } else {
            _draggedToEnd = NO;
        }
    }

    // 3
    if (recognizer.state == UIGestureRecognizerStateEnded) {

        if (_draggedToEnd) {
            // notify the delegate that this item should be deleted
            [self buttonDraggedToEnd];
        }

    }
}
4

0 回答 0