一般来说: 我得到了一个带有 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:
,也没有提到设置 maximumZoomScale
或 minimumZoomScale
。尝试执行这些操作,然后检查是否仍有问题。
关于ios - 将自定义 UIView 更改为 UIScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11508890/