iphone - UIImage 在 iPhone 中不滚动

标签 iphone ipad uiscrollview uiimage

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

而且 bg.png 没有应用到我的背景。

任何帮助将不胜感激。

最佳答案

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

  1. 正如潘所说,你需要设置你的scrollView委托(delegate)。否则,你的 viewForZoomingInScrollView:不会调用委托(delegate)实现。 我已经在 viewDidLoad: 中提供了这个调用,尽管你也可以 Hook 这个 在界面构建器或 Storyboard中。
  2. 正如 Teodor Carstea 在原帖评论中所说,在您的 原始代码,您正在设置您的 .imageView.size 和您的 .scrollView.contentsSize 完全相同。你不需要 设置 .imageView.frame. 的代码将作为 initWithImage:。这将使尺寸不同。
  3. 您确实需要将 .scrollView.contentSize 设置为 image.size 。 在哪里做这件事很重要,至少就必须这样做而言 发生在设置缩放比例之前。
  4. 正如 Kunal Balani 提到的,你必须启用scrollingEnabled,尽管你 可以在 Storyboard或界面生成器中设置它。
  5. 正如 MidhunMP 所说,你必须有 .userInteractionEnabled = YES,不过 一般来说,这些是在 Storyboard/界面生成器中设置的默认值, 所以我不会将它们包含在下面的代码更改中。
  6. 您的方法centerScrollViewContents并没有真正使内容居中 就像它正在 reshape imageView 框架一样。我只是简单地注释掉了 打电话,因为我认为这是完全没有必要的。相反,我做了 一个简单的调用即可在左上角启动图像。
  7. 如果您只想滚动,请设置 zoomScaleminimumZoomScale 和 maximumZoomScale 不是必需的。如果您还想缩放,那么 您需要将这三个设置全部设置在适当的位置。
  8. 如果 View 小于内容,您似乎确实想要拉伸(stretch) View 界限。如果这确实是一个问题(即如果您知道您的 bg.png 是这样 小到需要拉伸(stretch)),然后计算 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;
}

关于iphone - UIImage 在 iPhone 中不滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11559899/

相关文章:

iphone - 在iPad编程中使用多线程时出现问题(用于加载大量图像)

iphone - 如何从 AVCaptureSession 产生的 CMSampleBuffer 中获取 Y 分量?

javascript - Html 通过 jquery/javascript 在 iphone 上保持键盘可见

iphone - UIWebView - 自动调整大小,以便不需要滚动(在 webView 本身中)

iphone - IBAction 或 [UIButton addTarget :action: forControlEvents:] for UIButton created in . xib 文件?哪种方法更好?

ipad - iOS MapKit : Selected MKAnnotation coordinates

ios - 使用 iBooks Author 自动制作动画

ios - 快速从服务器响应中获取值(value)

ios - 分页 UIScrollView "Prototype Pages"

ios - 具有自动布局的 UIScrollView 中的动态大小的 UITableView