UIScrollView与UIImageView。图像不会向上滚动

时间:2014-07-10 19:13:55

标签: ios objective-c uiscrollview uiimageview

以下是UIScrollViewUIImageView的代码。 选择下一个图像时,将调用resetStatusImage。返回statusMessageImageView作为放大UIScrollView委托的视图。 使用最小缩放时,图像无法向上滚动,但仍有一个要滚动的图像部分。至于底部,左侧和右侧,它们是完全可见的,不能分别向下,向左和向右滚动。为什么在最小化时无法向上滚动图像?

- (void)viewDidLoad
{
    CGRect r = CGRectMake(0, 0, self.statusMessageTextView.bounds.size.width, 455);
    statusMessageImageView = [[UIImageView alloc] initWithFrame:r];
    statusMessageImageView.contentMode = UIViewContentModeCenter;

    // Set up the scroll view for the image zooming in/out and scrolling.
    imgScrollView = [[UIScrollView alloc] initWithFrame:statusMessageImageView.bounds];
    [imgScrollView setScrollEnabled:YES];
    [imgScrollView setClipsToBounds:YES];

    [self.view addSubview:imgScrollView];

    [imgScrollView setBackgroundColor:[UIColor yellowColor]];

    [imgScrollView setContentSize:statusMessageImageView.frame.size]; // this is your image view size
    imgScrollView.delegate = self;

    UITapGestureRecognizer *doubleTapRecognizer =
        [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewDoubleTapped:)];
    doubleTapRecognizer.numberOfTapsRequired = 2;
    doubleTapRecognizer.numberOfTouchesRequired = 1;
    [imgScrollView addGestureRecognizer:doubleTapRecognizer];

    [imgScrollView addSubview:statusMessageImageView];
    [imgScrollView addSubview:self.statusMessageTextView];
}
- (void)resetStatusImage
{
    imgScrollView.zoomScale = 1.0;

    UIImage *statusImage = self.statusImageManager.currentStatusImage;
    NSLog(@"Image size %f %f", statusImage.size.width, statusImage.size.height);

    // Set the image view fram size equal to the image size.
    statusMessageImageView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=statusImage.size};
    [statusMessageImageView setImage:statusImage];

    // Set the scroll view content size and zoom scale.
    imgScrollView.contentSize = statusImage.size;
    CGFloat scaleWidth = imgScrollView.frame.size.width / imgScrollView.contentSize.width;
    CGFloat scaleHeight = imgScrollView.frame.size.height / imgScrollView.contentSize.height;
    CGFloat maxScale = MAX(scaleWidth, scaleHeight);
    imgScrollView.minimumZoomScale = maxScale;
    imgScrollView.maximumZoomScale = 1.0f;
    imgScrollView.zoomScale = maxScale;

    // Save current scroll view content data
    // to detect that the user changed the visible area.
    contentOffsetX = imgScrollView.contentOffset.x;
    contentOffsetY = imgScrollView.contentOffset.y;
    zoomScale = imgScrollView.zoomScale;
}

2 个答案:

答案 0 :(得分:0)

-(UIView *) viewForZoomingInScrollView:(UIScrollView *)inScroll {
 return imageView;
 }

这会对你有帮助。

答案 1 :(得分:0)

statusMessageTextView身高值是错误的。在缩放,移动图像时,禁用自动缩放也会影响statusMessageTextView的位置:文本视图也会移动。