2013-06-13 3 views
9

작동하는 화면에서 하위보기를 이동하려고하지만 관성이나 운동량을 개체에 추가하려고합니다.
이미 가지고있는 UIPanGestureRecognizer 코드가 아래에 있습니다.UIPanGestureRecognizer에 관성을 추가하십시오.

미리 감사드립니다. 다시감사합니다. 감사합니다.

답변

4

RotationWheelAndDecelerationBehaviour을 살펴보십시오. 선형 패닝과 회전 운동 모두에 대해 감속을 수행하는 방법에 대한 예가 있습니다. 트릭은 사용자가 터치를 끝내고 작은 감속으로 그 방향으로 계속할 때의 속도를 확인하는 것입니다.

+0

이것은 매우 철저한 대답은 아닙니다. 나는 당신이 [** 답변하는 방법 **] (http://stackoverflow.com/questions/how-to-answer)을 읽고 그것을 확장 할 것을 제안한다. – brandonscript

+0

이것은 훨씬 더 나은 대답입니다. http://stackoverflow.com/a/6614319/558575 – amergin

1

글쎄, 나는 프로가 아니지만 여러 답을 확인하면서 자신 만의 고유 한 코드를 만들었습니다.

개선 방법 및 사용 된 나쁜 습관이있는 경우 알려주십시오. 나는 두 개의 서로 다른 애니메이션을 사용했다

- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer { 

CGPoint translatedPoint = [recognizer translationInView:self.postViewContainer]; 
CGPoint velocity = [recognizer velocityInView:recognizer.view]; 

float bottomMargin = self.view.frame.size.height - containerViewHeight; 
float topMargin = self.view.frame.size.height - scrollViewHeight; 

if ([recognizer state] == UIGestureRecognizerStateChanged) { 

    newYOrigin = self.postViewContainer.frame.origin.y + translatedPoint.y; 

    if (newYOrigin <= bottomMargin && newYOrigin >= topMargin) { 
     self.postViewContainer.center = CGPointMake(self.postViewContainer.center.x, self.postViewContainer.center.y + translatedPoint.y); 
    } 
    [recognizer setTranslation:CGPointMake(0, 0) inView:self.postViewContainer]; 
} 

if ([recognizer state] == UIGestureRecognizerStateEnded) { 

    __block float newYAnimatedOrigin = self.postViewContainer.frame.origin.y + (velocity.y/2.5); 

    if (newYAnimatedOrigin <= bottomMargin && newYAnimatedOrigin >= topMargin) { 
     [UIView animateWithDuration:1.2 delay:0 
          options:UIViewAnimationOptionCurveEaseOut 
         animations:^ { 
          self.postViewContainer.center = CGPointMake(self.postViewContainer.center.x, self.postViewContainer.center.y + (velocity.y/2.5)); 
         } 
         completion:^(BOOL finished) { 
          [self.postViewContainer setFrame:CGRectMake(0, newYAnimatedOrigin, self.view.frame.size.width, self.view.frame.size.height - newYAnimatedOrigin)]; 
         } 
     ]; 
    } 
    else { 
     [UIView animateWithDuration:0.6 delay:0 
          options:UIViewAnimationOptionCurveEaseOut 
         animations:^ { 
          if (newYAnimatedOrigin > bottomMargin) { 
           self.postViewContainer.center = CGPointMake(self.postViewContainer.center.x, bottomMargin + self.postViewContainer.frame.size.height/2); 
          } 

          if (newYAnimatedOrigin < topMargin) { 
           self.postViewContainer.center = CGPointMake(self.postViewContainer.center.x, topMargin + self.postViewContainer.frame.size.height/2); 
          } 
         } 
         completion:^(BOOL finished) { 
          if (newYAnimatedOrigin > bottomMargin) 
           [self.postViewContainer setFrame:CGRectMake(0, bottomMargin, self.view.frame.size.width, scrollViewHeight)]; 

          if (newYAnimatedOrigin < topMargin) 
           [self.postViewContainer setFrame:CGRectMake(0, topMargin, self.view.frame.size.width, scrollViewHeight)]; 
         } 
     ]; 
    } 
} 

}

, 하나는 기본 관성 한 사용자가 빠른 속도로 containerView를 놀아나 때의 경우 다른입니다.

그것은 iOS 7에서 잘 작동합니다.