1

久しぶりに質問でSOに来ましたが、まだ十分に学んでいません:(。

私はその中にUIScrollViewあるを持っUIImageViewています。の画像はUIImageView左右にスワイプすると変化します。のタッチをオーバーライドするUIScrollViewことは、適切で効率的な解決策ではないことを知っているので、UIScrollView(サブビューとしてではなく)に透明なビューを追加し、UIScrollViewそのタッチメソッドをオーバーライドして、スワイプで画像を変更しました。

これで、透明なビューにピンチを追加することで、プログラムでと画像gesture recognizerをズームすることができます。UIScrollViewここまではスムーズに作業できますが、もう一つの典型的な機能があります。トリミングした画像が見えるように、ズームした画像を指で上下に動かす必要があります。私はUILongPressGestureRecognizer私の透明なビューに追加された助けを借りてそれをやっています。タッチ時間が1秒になるUIImageViewと、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;
    }
}
4

1 に答える 1

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

そのため、ズームモードでのスクロールは写真アプリとまったく同じように機能するようになりました。

お役に立てれば

于 2012-05-29T06:34:33.650 に答える