【问题标题】:Make UIPanGestureRecognizer stretch when pulling past certain threshold超过特定阈值时使 UIPanGestureRecognizer 拉伸
【发布时间】:2013-06-24 08:22:01
【问题描述】:

我正在尝试实现与 Mac/iOS 网页的实现方式类似的东西,如果您拉出某个阈值,主视图将变得“有弹性”并根据拉动的速度移动。

不过,不同之处在于我尝试使用 UIPanGestureRecognizer 为 UITableViewCells 水平执行此操作。

我有一个handlePan 方法:

- (void)handlePan:(UIPanGestureRecognizer *)recognizer
{
    [recognizer setMaximumNumberOfTouches:1];
    CGPoint velocity = [recognizer velocityInView:self];

    int panDelta = ([recognizer locationInView:self].x - [recognizer locationInView:self.superview].x);

    if (recognizer.state == UIGestureRecognizerStateBegan) {
        _originalCenter = self.center;
    }

    if (recognizer.state == UIGestureRecognizerStateChanged) {

        CGPoint translation = [recognizer translationInView:self];

        float xVal = 0;
        if (panDelta <= 38) {
            xVal = _originalCenter.x + translation.x;
        } /*else {


           // this is where I struggle.


            xVal = _originalCenter.x;
        }*/
        self.center = CGPointMake(xVal, _originalCenter.y);

    }

    if (recognizer.state == UIGestureRecognizerStateEnded) {
        CGRect newFrame;
        int xOffset = 0;

        newFrame = CGRectMake(xOffset, self.frame.origin.y,
                                          self.bounds.size.width, self.bounds.size.height);

        [UIView animateWithDuration:0.2 animations:^{
            self.frame = newFrame;
        }];
    }
}

我没有任何特定的算法来创建“弹性”;我要做的就是让它滑动不会像用户的交互那样广泛,并且是流畅的。

想法?

【问题讨论】:

    标签: iphone ios objective-c cocoa-touch uikit


    【解决方案1】:

    我认为您正在寻找一个带有表示最大偏移量的水平渐近线的简单函数:

    // this is where I struggle.
    CGFloat maxOffset = 50.0; // change as you like
    CGFloat elementWidth = ?; // fill in the width of your view animated, eg: self.frame.size.width
    CGFloat absPannedOffset = fabsf(translation.x);
    CGFloat rico = powf(elementWidth, 2) / maxOffset;
    CGFloat absOffset = (((- 1 / rico) * powf(absPannedOffset - elementWidth, 2)) + maxOffset);
    
    if (pannedOffset < 0) {
        xVal = -absOffset;
    } else {
        xVal = absOffset;
    }
    

    【讨论】:

      猜你喜欢
      • 2018-10-27
      • 1970-01-01
      • 1970-01-01
      • 2018-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多