1

ビューをドラッグし、特定のポイントに到達したときにドラッグを指数関数的に遅くするロジックを理解しようとしています。バギーで少しハッキーですが、私はそれをある程度機能させました。代わりに使用できるより良い式があるかどうか疑問に思いました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 に答える