2012-12-14 6 views
10

GestureRecognizer을 빌드하여 오른쪽에서 왼쪽으로 UIView을 드래그하려고합니다. 사용자가보기를 화면 절반으로 드래그하면보기가 자동으로 왼쪽 모서리로 드래그되어 삭제되지만 사용자가 화면 절반까지 드래그하지 않으면 화면 오른쪽 구석으로 돌아갑니다. 여기에 약간의 자습서 또는 다른 것이 있습니까? 감사UIView를 드래그하는 UIGestureRecognizer

편집 : Heres는 일부 코드, 자사의 UIImageView하는 UIScrollView 안에, 내가 전체를 스크롤하거나 이미지를 그 안에 드래그해야합니다 내가 드래그하려고

_myScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.window.bounds.size.width, self.window.bounds.size.height)]; [_myScroll setContentSize:CGSizeMake(self.window.bounds.size.width , 1300)]; 

_tutorial = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.window.bounds.size.width, 1300)]; [_tutorial setImage:[UIImage imageNamed:@"Default"]]; 
_tutorial.contentMode = UIViewContentModeScaleAspectFit; [_myScroll addSubview:_tutorial]; 

_tutorial.userInteractionEnabled = YES; 
    UIPanGestureRecognizer * recognizer = [[UIPanGestureRecognizer alloc]initWithTarget:_tutorial action:@selector(handlePan:)]; 
[_tutorial addGestureRecognizer:recognizer]; 

    [self.window addSubview:_myScroll]; 

그리고 방법을 the UIImageView

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

    CGPoint translation = [recognizer translationInView:_myScroll]; 
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, 
             recognizer.view.center.y + translation.y); 
    [recognizer setTranslation:CGPointMake(0, 0) inView:_myScroll]; 

} 
+0

나는 귀하의 질문에 대답했지만 "보기를 끌 수는 있지만 올바른 위치로가는 방법을 모르겠다"와 같이 시도한 것에 대한 자세한 내용을 제공해야합니다 ... – rdurand

+0

@ rdurand 예, UIPanGestureRecognizer를 사용하여 뷰를 끌 수는 있지만 위치를 감지하는 방법을 정확히 모르고 원하는 위치로 뷰를 보냅니다. 그리고 두 번째 문제는 뷰를 드래그하는 방법, 가로 방향에만 – darkman

+0

일부 코드를 제공합니다. * 시도하지 않은 것을 시도해보십시오. – rdurand

답변

22

보기는 here입니다. 이것은 UIGestureRecognizer 자습서로 핀치 및 팬 동작을 처리 할 수있는 기초를 제공합니다. 기본 사항을 배우고 나면 원하는 것을 성취하기가 정말 쉽습니다. 팬이 완료되면 올바른 위치로 보내기 위해 뷰의 위치를 ​​확인하면됩니다. this other tutorialUIScrollViews에 올리면 두 번째 부분에서 길을 찾을 수 있습니다.

두 튜토리얼은 모두 raywenderlich.com에서 왔으며, 아마도 가장 유용한 iOS 자습서 사이트입니다. 당신이를 찾기 위해 코드를 작동해야합니다, 여기 당신에게 완벽한 해답을 제공하지 않습니다

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

    CGPoint translation = [recognizer translationInView:_myScroll]; 

    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, 
             recognizer.view.center.y + translation.y); 

    if (recognizer.state == UIGestureRecognizerStateEnded) { 

     // Check here for the position of the view when the user stops touching the screen 

     // Set "CGFloat finalX" and "CGFloat finalY", depending on the last position of the touch 

     // Use this to animate the position of your view to where you want 
     [UIView animateWithDuration: aDuration 
           delay: 0 
          options: UIViewAnimationOptionCurveEaseOut 
         animations:^{ 
      CGPoint finalPoint = CGPointMake(finalX, finalY); 
      recognizer.view.center = finalPoint; } 
         completion:nil]; 
    } 


    [recognizer setTranslation:CGPointMake(0, 0) inView:_myScroll]; 

} 

: 여기


당신이 작업을 할 수 handlePan: 방법에 대한 몇 가지 코드 당신이 원하는대로 작동하도록하는 방법.

마지막 팁이 하나 있습니다. slideFactor를 사용하여 "부드러운"애니메이션을 만들 수 있습니다. 애니메이션이보다 사실적 이도록 도와줍니다. 당신이 바로 "만약"의 시작 부분에 추가하는 것이,이 코드에 작업 :

CGPoint velocity = [recognizer velocityInView:self.view]; 
CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y)); 
CGFloat slideMult = magnitude/200; 

float slideFactor = 0.1 * slideMult; // Increase for more slide 

그런 다음 UIView의 animateWithDuration:에, 기간으로 slideFactor를 사용합니다.

관련 문제