1

很长一段时间后,我带着一个问题来到SO,我还没有学到足够的东西:(。

我有一个UIScrollView里面有UIImageView它。的图像UIImageView是通过左右滑动来改变的。我知道覆盖触摸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;
    }
}
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 回答