2012-05-24 2 views
1

나는 그렇게 오랜 시간 후 질문, 내가 :(아직 충분히 습득하지 않은 왔습니다.사진 응용 프로그램은

내가 가진 UIScrollView을 데 UIImageView 내부 이미지 UIImageView의 이미지가 오른쪽 및 왼쪽 스 와이프로 변경됩니다. UIScrollView의 터치를 재정의하는 것이 좋고 효율적인 솔루션이 아니므로 UIScrollView (서브 뷰가 아닌)에 투명한 뷰를 추가했습니다. UIScrollView 범위를 덮고 스 와이프하여 이미지를 변경하기 위해 터치 메소드를 재정의했습니다.

이제는 p of pinch gesture recognizer은 (는) 투명하게 볼 수 있습니다. UIScrollView과 이미지를 프로그램 방식으로 확대/축소 할 수 있습니다. 여기까지, 나는 일을 매끄럽게 할 수 있지만 또 다른 전형적인 기능을 가지고있다. 확대 된 이미지를 내 손가락으로 위아래로 움직여 자른 이미지를 볼 수 있어야합니다. 나는 투명한 볼 내용에 UILongPressGestureRecognizer 님의 도움을 다하겠습니다. 터치 지속 시간이 1 초가되면에 4 방향 화살표 이미지를 추가하고 터치 위치를 추가합니다. 이제 손가락을 움직이면 fourwayarrow image이 내 손가락을 따라가지만 스크롤 뷰의 이미지가 내 손가락을 따라 가지 않습니다. 그 종류의 육포와 종종 나는 탄력있는 끈을 묶은 것처럼 작동합니다. 그래서 처음에는 문자열을 어떤 임계 값으로 끌어 올려야합니다 (그 시점까지는 스크롤 뷰가 움직이지 않습니다). 그리고 그 레벨에 도달하면 갑자기 컨트롤이 벗어납니다. 나는 당신을 위해 코드를 여기에 추가하고있다.

사람들에게 다른 정보가 필요하면 언제든지 의견을 추가하십시오.

- (void) handleLongPress:(UILongPressGestureRecognizer *)longPressGestureRecognizer{ 
    CGPoint touchLocation = [longPressGestureRecognizer locationInView:self.transparentLayer]; 
    switch (longPressGestureRecognizer.state) { 
     case UIGestureRecognizerStatePossible: break; 
     case UIGestureRecognizerStateBegan:{ 
      if (fourWayArrowImageView == nil) { 
       fourWayArrowImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"4wayarrow.png"]]; 
       [fourWayArrowImageView setBackgroundColor:[UIColor clearColor]]; 
      } 
      _previousLocation.x = touchLocation.x; 
      _previousLocation.y = touchLocation.y; 
      [fourWayArrowImageView setCenter:touchLocation]; 
      [self.transparentLayer addSubview:fourWayArrowImageView]; 
     } 
      break; 
     case UIGestureRecognizerStateChanged:{ 
      float dx = (touchLocation.x - _previousLocation.x); 
      float dy = (touchLocation.y - _previousLocation.y); 
      CGPoint currentMaxOffset = [self currentMaxOffset]; 
      CGPoint currentOffset = [self.rotatableImageViewContainingScrollView contentOffset]; 
      float theScale = [self.rotatableImageViewContainingScrollView zoomScale]; 
      currentOffset.x -= (dx*theScale); 
      currentOffset.y -= (dy*theScale); 

      [fourWayArrowImageView setCenter:touchLocation]; 
      CGSize size = self.rotatableImageViewContainingScrollView.frame.size; 
      [self.rotatableImageViewContainingScrollView scrollRectToVisible:CGRectMake(currentOffset.x, currentOffset.y, size.width, size.height) animated:YES]; 
      _prevMaxOffset = CGPointMake(currentMaxOffset.x, currentMaxOffset.y); 
     } 
      break; 
     case UIGestureRecognizerStateCancelled: 
     case UIGestureRecognizerStateFailed: 
     case UIGestureRecognizerStateEnded: [fourWayArrowImageView removeFromSuperview]; break; 
    } 
} 

답변

0

해결책을 찾았습니다. 사실 나는 실수하고 있었다. 스크롤 한 후에 이전 위치를 업데이트하지 않고이 경우 애니메이션을 적용해서는 안됩니다. 그러니 답을보십시오. 시간을 주신 모든 분들께 감사드립니다.

- (void) handleLongPress:(UILongPressGestureRecognizer *)longPressGestureRecognizer{ 
    CGPoint touchLocation = [longPressGestureRecognizer locationInView:self.transparentLayer]; 
    switch (longPressGestureRecognizer.state) { 
     case UIGestureRecognizerStatePossible: break; 
     case UIGestureRecognizerStateBegan:{ 
      if (fourWayArrowImageView == nil) { 
       fourWayArrowImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"4wayarrow.png"]]; 
       [fourWayArrowImageView setBackgroundColor:[UIColor clearColor]]; 
      } 
      _previousLocation = CGPointMake(touchLocation.x, touchLocation.y); 
      [fourWayArrowImageView setCenter:touchLocation]; 
      [self.transparentLayer addSubview:fourWayArrowImageView]; 
     } 
      break; 
     case UIGestureRecognizerStateChanged:{ 
      float dx = (touchLocation.x - _previousLocation.x); 
      float dy = (touchLocation.y - _previousLocation.y); 
      CGPoint currentMaxOffset = [self currentMaxOffset]; 
      CGPoint currentOffset = [self.rotatableImageViewContainingScrollView contentOffset]; 
      //float theScale = [self.rotatableImageViewContainingScrollView zoomScale]; 
      //currentOffset.x -= (dx*theScale); 
      //currentOffset.y -= (dy*theScale); 
      currentOffset.x -= (dx); 
      currentOffset.y -= (dy); 
      [fourWayArrowImageView setCenter:touchLocation]; 
      CGSize size = self.rotatableImageViewContainingScrollView.frame.size; 
      [self.rotatableImageViewContainingScrollView scrollRectToVisible:CGRectMake(currentOffset.x, currentOffset.y, size.width, size.height) animated:NO]; //bool value in animated should be false. 
      _prevMaxOffset = CGPointMake(currentMaxOffset.x, currentMaxOffset.y); 
      _previousLocation = CGPointMake(touchLocation.x, touchLocation.y); // added this line in the answer. 
     } 
      break; 
     case UIGestureRecognizerStateCancelled: 
     case UIGestureRecognizerStateFailed: 
     case UIGestureRecognizerStateEnded: [fourWayArrowImageView removeFromSuperview]; break; 
    } 
} 

이제 확대/축소 모드에서의 스크롤은 사진 앱처럼 정확하게 작동합니다.

희망이 있습니다.

+0

정말 로터리 방식으로이 작업을 수행하고 있습니다. Apple의 PhotoScroller 샘플 코드를 살펴 봐야합니다. 페이징 스크롤 뷰를 사용하고 UIScrollView에 기본 제공되는 표준 줌/패닝 지원을 사용할 수 있습니다. 이 모든 작업을 직접 수행 할 필요는 없습니다. –

+0

@MikeWeller 감사합니다 마이크 ..하지만 클라이언트 요구 사항이 모든 확대/축소 수준에서 스 와이프를 사용하여 회전 할 수 있습니다. 스크롤 뷰의 기본 확대/축소 및 스크롤 할 때 스크롤 할 수 없으므로 확대/축소 모드 일 수 없습니다. 그래서 나는 이런 식으로 일을해야했다. –