Ios 在两点之间来回滑动按钮

Ios 在两点之间来回滑动按钮,ios,objective-c,uipangesturerecognizer,Ios,Objective C,Uipangesturerecognizer,我尝试使用UIPangestureRecognitor在两点之间滑动按钮,就像音量滑块一样。下面的代码允许我来回滑动按钮,但按钮并不是每次都停在同一点上,也就是说,当我向右滑动按钮时,它有时停在应该停的地方,有时停在应该停的地方+/-10像素。我做错了什么 - (void)handlePan:(UIPanGestureRecognizer *)recognizer { CGPoint location = [recognizer locationInView:self];

我尝试使用UIPangestureRecognitor在两点之间滑动按钮,就像音量滑块一样。下面的代码允许我来回滑动按钮,但按钮并不是每次都停在同一点上,也就是说,当我向右滑动按钮时,它有时停在应该停的地方,有时停在应该停的地方+/-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];
        }

    }
}

当位置在点之外时,将其放置在最近的点上如何?因此,如果汤姆·欧文建议的方法是这样做的话!我看不到在手势完成后修复视图位置的代码部分。它是buttonDraggedToEnd的一部分吗?如果是这样,请发布相关代码。汤姆,谢谢!将其添加为答案,我将接受: