Swift 在iPhone上的scrollview上缩放到位置(纵向);不太对
在我的应用程序中,用户可以添加图像(地图),然后向该图像添加PIN(小图像+附加注释)。地图在滚动视图中。添加pin等功能很好,但有一个小例外:在纵向模式(或更精确的traitcollection==.compact)的iPhone上,我无法使pin处于中心位置并稍微缩小。所有这些都在自定义splitview控制器中;注意:在左视图控制器中,在右视图控制器中映射(图像+pin) 在景观中的iPad/iPhone上,只需拨打以下电话即可:Swift 在iPhone上的scrollview上缩放到位置(纵向);不太对,swift,uiscrollview,uikit,swift5,Swift,Uiscrollview,Uikit,Swift5,在我的应用程序中,用户可以添加图像(地图),然后向该图像添加PIN(小图像+附加注释)。地图在滚动视图中。添加pin等功能很好,但有一个小例外:在纵向模式(或更精确的traitcollection==.compact)的iPhone上,我无法使pin处于中心位置并稍微缩小。所有这些都在自定义splitview控制器中;注意:在左视图控制器中,在右视图控制器中映射(图像+pin) 在景观中的iPad/iPhone上,只需拨打以下电话即可: mapImageScrollView.zoom(to:
mapImageScrollView.zoom(to: mapImageLocationPin!.frame, animated: false)
mapImageScrollView.setZoomScale(1.0, animated: true)
结果如下
在纵向模式下的iPhone上,用户必须单击双箭头按钮才能使用地图导航到右viewcontroller。使用与上面相同的代码行将导致地图被放大,但不在管脚上居中。
我能得到的最好结果是使用以下代码(请注意,要使其工作,我必须在缩放之前设置Zoomscale!)
这将导致居中显示,但不会缩小
我认为这是因为在第二种情况下,右viewcontroller尚未出现(只有在用户点击双箭头按钮后才会出现),但我似乎找不到更好的解决方案
感谢您的帮助
我用于将pin添加到地图的代码:
private func addMapImageLocationPin() {
if let location = annotation?.location {
//Activate the corresponding Map Image and update picker
selectedMapImage = location.map
mapImagePickerView.selectRow(mapImagesController.getRowFor(location.map), inComponent: 0, animated: true)
//Instantiate new Pin with annotation
mapImageLocationPin = MapImageLocationPin(with: annotation!, andMapImageSize: mapImageView.image!.size)
mapImageView.addSubview(mapImageLocationPin!)
//Desired height and width
let desiredWidth: CGFloat = 160
let desiredHeight: CGFloat = 80
//compensate for scrollView zoomscale, but never bigger than desired width & height!
let minZoomScale = mapImageScrollView.minimumZoomScale
let width = min(desiredWidth, desiredWidth / minZoomScale)
let height = min(desiredHeight, desiredHeight / minZoomScale)
//center the pin image horizontal on location
let y = location.coordinateY
let x = location.coordinateX - (width / 2)
//position (frame) the pin
mapImageLocationPin?.frame = CGRect(x: x, y: y, width: width, height: height)
//zoom to the pin, different approach for smaller screen sizes
if traitCollection.horizontalSizeClass == .compact {
mapImageScrollView.setZoomScale(0.85, animated: false)
mapImageScrollView.zoom(to: mapImageLocationPin!.frame, animated: false)
} else {
mapImageScrollView.zoom(to: mapImageLocationPin!.frame, animated: false)
mapImageScrollView.setZoomScale(1.0, animated: true)
}
}
}
在布局周期的哪个点调用
addMapImageLocationPin
?如果在某些情况下未立即加载子视图,则需要确保在主视图布局后调用定位代码,例如在ViewDidDisplay中:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
mapImageScrollView.zoom(to: mapImageLocationPin!.frame, animated: false)
}
这有点让人困惑,因为你展示的是两个不同的地方——为什么你不在这两种情况下展示北瑞典,这样我们就可以看到区别了?不过,您可能希望在
viewDidLayoutSubviews()
或ViewDidDisplay()
中执行.setZoomScale(…)
和.zoom(…)
。更改了图像!稍后将对评论作出回应……这是一位房地产观察家发出的呼吁。在.compact模式下,viewcontroller尚未出现(尽管已加载)。调用“放大视图显示”似乎可以做到这一点。我将做更多的测试,然后发布一个基于此的答案!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
mapImageScrollView.zoom(to: mapImageLocationPin!.frame, animated: false)
}