iphone - UIView 的弹性拖动,就像从 UITableView 顶部拖动一样

标签 iphone ios uigesturerecognizer

我试图找出拖动 View 的逻辑,然后在拖动到某个点时以指数方式减慢速度。我有点让它工作了,虽然有车而且有点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)];
}

最佳答案

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

- (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;
        }
    }
}

关于iphone - UIView 的弹性拖动,就像从 UITableView 顶部拖动一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13987598/

相关文章:

ios - 自定义 View 有时会出现错误的背景颜色

iphone - 知道由于 Dismissal(不是 segue)而出现 ViewController

iOS 通过 SNS 推送通知

ios - 如何拦截 mapview 平移和缩放 (Swift 4)

swift - Siri 远程触摸位置?

iphone - 加速度计 - 运动模式识别(iPhone)

iphone - 创建许多对象时 FPS(每秒帧数)下降

ios - 内存管理。 CF 对象的字节副本

iOS TableView 错误 - 在键值观察者仍注册时释放 - DGElasticPullToRefresh

ios - Collection View 中的 touchesBegan 和 ended