我想要带有图像内容视图的滚动视图。该图像实际上是比屏幕大得多的地图。地图最初应位于滚动视图的中心,就像当您将 iPhone 转向横向时照片应用程序中的照片一样。
我没有设法将地图置于中心并同时进行正确的缩放和滚动。
假设地图图像从屏幕顶部开始(纵向),代码如下所示:
- (void)loadView {
mapView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"map.jpg"]];
CGFloat mapHeight = MAP_HEIGHT * SCREEN_WIDTH / MAP_WIDTH;
mapView.frame = CGRectMake(0, 0, SCREEN_WIDTH, mapHeight);
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
scrollView.delegate = self;
scrollView.contentSize = mapView.frame.size;
scrollView.maximumZoomScale = MAP_WIDTH / SCREEN_WIDTH;
scrollView.minimumZoomScale = 1;
[scrollView addSubview:mapView];
self.view = scrollView;
}
当我将图像框架移动到中心时,图像仅从框架顶部向下增长。我尝试使用 mapView 变换,动态更改 imageView 的框架。到目前为止,没有什么对我有用。
此代码应该适用于大多数版本的 iOS(并且已经过测试,可在 3.1 及以上版本上使用)。
它基于 Jonah 的回答中提到的 Apple WWDC 代码。
将以下内容添加到 UIScrollView 的子类中,并将tileContainerView 替换为包含图像或图块的视图:
- (void)layoutSubviews {
[super layoutSubviews];
// center the image as it becomes smaller than the size of the screen
CGSize boundsSize = self.bounds.size;
CGRect frameToCenter = tileContainerView.frame;
// center horizontally
if (frameToCenter.size.width < boundsSize.width)
frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
else
frameToCenter.origin.x = 0;
// center vertically
if (frameToCenter.size.height < boundsSize.height)
frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
else
frameToCenter.origin.y = 0;
tileContainerView.frame = frameToCenter;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)