UIImage不会在iPhone中滚动

Kru*_*nal 5 iphone uiscrollview uiimage ipad

我是iPhone开发人员的新手,

我想在我的图片中滚动,

这是我的代码片段,

- (void)viewDidLoad {
UIImage *image1 = [UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"bg.png"]];
    self.imageView = [[UIImageView alloc] initWithImage:image1];
    self.imageView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image1.size};
    [self.scrollView addSubview:self.imageView];
    self.scrollView.contentSize = image.size;

 [self centerScrollViewContents];
}


- (void)centerScrollViewContents {
    CGSize boundsSize = self.scrollView.bounds.size;
    CGRect contentsFrame = self.imageView.frame;

    if (contentsFrame.size.width < boundsSize.width) {
        contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
    } else {
        contentsFrame.origin.x = 0.0f;
    }

    if (contentsFrame.size.height < boundsSize.height) {
        contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
    } else {
        contentsFrame.origin.y = 0.0f;
    }

    self.imageView.frame = contentsFrame;
}

- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    // Return the view that you want to zoom
    return self.imageView;
}
Run Code Online (Sandbox Code Playgroud)

bg.png没有适用于我的背景.

任何帮助将不胜感激.

joh*_*doe 4

答案实际上是许多其他答案和评论的组合,再加上一些其他调整。

  1. 正如潘所说,你需要设置你的scrollView委托。否则,您的 viewForZoomingInScrollView: 委托实现将不会被调用。我已经在 viewDidLoad: 中提供了这个调用,尽管您也可以在界面生成器或故事板中将其连接起来。
  2. 正如 Teodor Carstea 在原始帖子的评论中所说,在你的原始代码中,你将 your.imageView.size和 your 设置.scrollView.contentsSize为完全相同的东西。您不需要设置.imageView.frame. 将作为 的一部分发生的 代码initWithImage:。这将使尺寸不同。
  3. 您确实需要将.scrollView.contentSize设为image.size。在哪里执行此操作很重要,至少在设置缩放比例之前必须执行此操作。
  4. 正如 Kunal Balani 提到的,你必须启用滚动,尽管你可以在故事板或界面生成器中设置它。
  5. 正如 MidhunMP 所说,你必须拥有.userInteractionEnabled = YES,但一般来说,这些是在故事板/界面生成器中设置的默认值,所以我不会将它们包含在下面的代码更改中。
  6. 您的方法centerScrollViewContents并没有真正使内容居中,而是重新塑造 imageView 框架。我只是注释掉了这个电话,因为我认为这是完全没有必要的。相反,我做了一个简单的调用以在左上角启动您的图像。
  7. 如果您只想滚动,则无需设置zoomScaleminimumZoomScale和 。maximumZoomScale如果您还想进行捏缩放,那么您需要将这三个设置全部设置在适当的位置。
  8. 如果视图小于内容边界,您似乎确实想要拉伸视图。如果这确实是一个问题(即,如果您知道您的 bg.png 太小以至于需要拉伸),则分别计算 contentFrame 与图像帧的宽度比和高度比,其中较大的一个将是您的新内容ZoomScale,您将能够向另一个方向滚动。或者,还有其他方法可以决定您想要的比例设置。搜索 stackoverflow.com 以获取根据需要设置缩放比例的其他答案。

这是应用这些项目后的代码。

(void)viewDidLoad {
    self.scrollView.delegate = self;         // can be set in storyboard
    self.scrollView.scrollingEnabled = YES;  // can be set in storyboard
    NSString* bgPng
      = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"bg.png"];
    UIImage *image1 = [UIImage imageWithContentsOfFile:bgPng];
    self.imageView = [[UIImageView alloc] initWithImage:image1];
//  self.imageView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image1.size};
    [self.scrollView addSubview:self.imageView];
    self.scrollView.contentSize = image.size;

    // set zoom scale here if desired.

    // if zoom is implemented, the following call should be made
    // after setting .zoomScale, .minimumZoomScale & .maximumZoomScale
    self.scrollView.contentOffset = CGPointZero;

//  [self centerScrollViewContents];
}


//- (void)centerScrollViewContents {
//    CGSize boundsSize = self.scrollView.bounds.size;
//    CGRect contentsFrame = self.imageView.frame;
//
//    if (contentsFrame.size.width < boundsSize.width) {
//        contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
//    } else {
//        contentsFrame.origin.x = 0.0f;
//    }
//
//    if (contentsFrame.size.height < boundsSize.height) {
//        contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
//    } else {
//        contentsFrame.origin.y = 0.0f;
//    }
//  
//    self.imageView.frame = contentsFrame;
//}

- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    // Return the view that you want to zoom
    return self.imageView;
}
Run Code Online (Sandbox Code Playgroud)