以编程方式放大UIS滚动视图不起作用



我使用双击来放大滚动视图中的imageView。

当通过按钮点击操作调用下面的方法时,它可以很好地工作(imageView放大/缩小(。

然而,我需要以编程方式调用该方法来放大,当我这样做时,什么都不会发生:imageView没有放大

我错过了什么?

以下是我使用的@objc func doubleTapped()方法的精确副本,抽头位置被任意CGPoint 取代

func doubleTappedCustom() {
var scale: CGFloat = 2
let ratioW = imageView.frame.width / imageView.image!.size.width
let ratioH = imageView.frame.height / imageView.image!.size.height
let ratio = ratioW < ratioH ? ratioW:ratioH
let newWidth = imageView.image!.size.width * ratio
let newHeight = imageView.image!.size.height * ratio

if (imageView.image.size.width < imageView.size.height){
if scale > imageView.frame.width / newWidth {
scale = imageView.frame.width / newWidth
}
}

let location = CGPoint(x: imageView.frame.width/2, y: imageView.frame.height/2)
if scrollView.zoomScale == 1 {
// here tap location is repalced with 'location' cgpoint
scrollView.zoom(to: zoomRectForScale(scale, center: location), animated: true)
} else {
scrollView.setZoomScale(1, animated: true)
}

}
func viewForZooming(in scrollView: UIScrollView) -> UIView?
{
return image
}
override func viewDidLoad() {
super.viewDidLoad()
scrollView.delegate = self
scrollView.minimumZoomScale = 1
scrollView.maximumZoomScale = 20
}

通过将缩放代码移动到viewDidAppear解决了问题

最新更新