ios - 将自定义 UIView 更改为 UIScrollView

标签 ios uiscrollview uiimageview xib custom-view

一般来说: 我得到了一个带有 xib 文件的自定义 UIView 作为 UIViewController 上的 subview 。我在我的 Controller 中以编程方式添加 subview 。

在我的 UIViewController 中:

NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"MyCustomSubView" owner:self  options:nil];
mySubView = [subviewArray objectAtIndex:0];
...
[self.view addSubview:mapView];

因为我需要在这个 subview 上滚动,它上面还有一个 UIImageView,所以我决定将我的自定义 UIView 更改为 UIScrollView。

我做了以下步骤:

  • 更改了 MyCustomSubView.xib,添加了一个带有自定义类 MyCustomSubView 的 UIScrollView(和之前一样添加了一个 UIImageView)
  • 更改我的 MyCustomSubView 类以扩展 UIScrollView
  • 更改我的 MyViewController 以实现 UIScrollViewDelegate
  • 将 mySubView.delegate 设置为 self( Controller )
  • 实现 (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView 并返回 subview 的 UIImageView
  • 在 imageview 和 uiscrollview 上启用用户交互

这些是我认为的总步数。但作为示例,未调用 viewForZoomingInScrollView 方法。我也尝试放大,但没有任何反应。

任何人的想法,可能是错的?

最佳答案

简要查看 UIScrollView Class Reference page ,我看到以下内容:

The UIScrollView class can have a delegate that must adopt the UIScrollViewDelegate protocol. For zooming and panning to work, the delegate must implement both viewForZoomingInScrollView: and scrollViewDidEndZooming:withView:atScale:; in addition, the maximum (maximumZoomScale) and minimum ( minimumZoomScale) zoom scale must be different.

您提到了实现 viewForZoomingInScrollView:,但没有提到 scrollViewDidEndZooming:atScale:,也没有提到设置 maximumZoomScaleminimumZoomScale。尝试执行这些操作,然后检查是否仍有问题。

关于ios - 将自定义 UIView 更改为 UIScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11508890/

相关文章:

objective-c - 防止 UIScrollView 将内容移动到左上角

iphone - UITableView/UIScrollView 滚动优化/性能

ios - 图像在 ios 模拟器中看起来与在照片预览中不同

ios - UIView setImage :]: unrecognized selector sent to instance

iOS 创建具有动态内容的 PDF

ios - 由于一般错误,这次无法处理应用程序....每次在验证我的应用程序时都会显示此错误。为什么?

html - 位置:iPhone iOS 中的固定页脚

iphone - iOS - UIScrollView 不起作用(它根本不滚动 - 图像保持固定)

swift - 将数据从自定义 UIView 传递到主 VC

ios - PHAsset creationDate使用哪个时区?