2013-06-24 51 views
0

我想實現類似Mac/iOS網頁如何實現的地方,如果你拉過一定的門檻,主視圖變得「有彈性」,並基於拉力的速度。使UIPanGestureRecognizer伸展時拉過一定的門檻

不同的是,我試圖用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; 
     }]; 
    } 
} 

我沒有任何具體的算法創建了「拉伸性」;我所要做的就是讓滑動不會像用戶的交互一樣廣泛,並且流暢。

想法?

回答

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