照片应用程序,如使用手势识别器在缩放模式下滚动滚动视图

时间:2012-05-24 19:23:27

标签: iphone ipad uiscrollview scroll uigesturerecognizer

经过很长一段时间,我已经回答了问题,我还没有学到足够的知识:(。

我的UIScrollView里面有UIImageViewUIImageView的图像将通过向右和向左滑动进行更改。我知道覆盖UIScrollView的触摸不会是一个好的和有效的解决方案,所以我在UIScrollView(不是它的子视图)上添加了一个透明的视图,覆盖了UIScrollView的边界并被覆盖它的触摸方法可以在滑动时更改图像。

现在,在我的透明视图中添加了夹点gesture recognizer的帮助下,我能够以编程方式缩放UIScrollView和图像。直到这里,我能够顺利地完成工作,但我有另一个典型的功能。我必须用手指上下移动缩放图像,以便我可以看到裁剪后的图像。我在UILongPressGestureRecognizer的帮助下添加到透明视图中。在1秒的触摸持续时间后,我添加一个带有四向箭头图像的UIImageView并添加触摸位置。现在,当我移动手指时,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;
    }
}

1 个答案:

答案 0 :(得分: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;
    }
}

因此,缩放模式下的滚动现在就像照片应用程序一样。

希望这有帮助