1

我试图找出拖动视图的逻辑,然后在拖动到某个点时以指数方式减慢速度。我有点让它工作了,虽然有车而且有点hacky。我想知道是否有更好的公式可以代替newPosition = (-kDeleteViewWidth + (point.x * 0.2));

完整的手势代码如下。

// kDeleteViewWidth is defined as 80.0f
UIPanGestureRecognizer *gesture = (UIPanGestureRecognizer *)sender;
CGPoint point = [gesture translationInView:self];

if ([gesture state] == UIGestureRecognizerStateBegan)
    _initialPosition = _topView.frame.origin;

if ([gesture state] == UIGestureRecognizerStateChanged)
{

    // Hacky Elastic method
    float newPosition = _initialPosition.x + point.x;
    if (point.x < -kDeleteViewWidth)
        newPosition = (-kDeleteViewWidth + (point.x * 0.2));
    [_topView setFrame:CGRectMake(newPosition, 0, self.contentView.frame.size.width, self.contentView.frame.size.height)];
}
4

2 回答 2

4

为了完整起见,这是我使用的代码:

- (void)swipeCell:(id)sender
{
    UIPanGestureRecognizer *gesture = (UIPanGestureRecognizer *)sender;
    CGPoint point = [gesture translationInView:self];

    if ([gesture state] == UIGestureRecognizerStateBegan)
    {
        // CGPoint initialPosition
        _initialPosition = _topView.frame.origin;
    }

    if ([gesture state] == UIGestureRecognizerStateChanged)
    {

        // Hacky Elastic method
        float newPosition = _initialPosition.x + point.x;

        // Starting point
        if (_initialPosition.x == 0)
        {
            // Once we get to our pre-defined kDeleteViewWidth, start dividing by 5
            if (_topView.frame.origin.x < -kDeleteViewWidth) {
                float distance = (point.x - -kDeleteViewWidth);
                float newDistance = distance / 5;
                newPosition = (-kDeleteViewWidth + newDistance);
            }

            else if (_topView.frame.origin.x > 0)
            {
                float distance = point.x;
                float newDistance = distance / 5;
                newPosition = newDistance;
            }
        }

        else if (_topView.frame.origin.x < -kDeleteViewWidth)
        {
            float distance = point.x;
            float newDistance = distance / 5;
            newPosition = (-kDeleteViewWidth + newDistance);
        }

        else if (_topView.frame.origin.x > 0)
        {
            float distance = point.x + _initialPosition.x;
            float newDistance = distance / 5;
            newPosition = newDistance;
        }

        [_topView setFrame:CGRectMake(newPosition, 0, self.contentView.frame.size.width, self.contentView.frame.size.height)];
    }

    if ([gesture state] == UIGestureRecognizerStateEnded || [gesture state] == UIGestureRecognizerStateCancelled) {

        // If we have scrolled over a 1/3 of the way to kDeleteViewWidth, move to kDeleteViewWidth
        if (point.x < -(kDeleteViewWidth / 3)) {
            [UIView animateWithDuration:kAnimationDuration animations:^{
                [_topView setFrame:CGRectMake(-kDeleteViewWidth, 0, self.contentView.frame.size.width, self.contentView.frame.size.height)];
            }];
            _isSwiped = YES;
        }

        // Otherwise animation back to 0
        else  {
            [UIView animateWithDuration:kAnimationDuration animations:^{
                [_topView setFrame:CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height)];
            } completion:^(BOOL finished) {
                [self removeShadow];
            }];
            _isSwiped = NO;
        }
    }
}
于 2013-01-08T09:23:02.410 回答
1

这是一个不错的示例项目可用。

链接:https ://github.com/crocodella/PullableView

快乐编码!

于 2012-12-21T09:51:50.337 回答