gpt4 book ai didi

ios - swift 在 ScrollView 内缩放 UIImageView

转载 作者:行者123 更新时间:2023-11-28 13:14:15 25 4
gpt4 key购买 nike

我在 ScrollView 中有一个 ImageView。我可以缩放。但是每当我释放光标时,整个图像都会适合屏幕。我只想显示图像的缩放部分。这是我的代码。

  func centerScrollViewContents(){
let boundsSize = scrollView.bounds.size
var contentsFrame = imageView.frame

if contentsFrame.size.width < boundsSize.width{
contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2
}else{
contentsFrame.origin.x = 0
}

if contentsFrame.size.height < boundsSize.height {

contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2
}else{
contentsFrame.origin.y = 0
}

imageView.frame = contentsFrame

}

func scrollViewDidZoom(scrollView: UIScrollView) {
centerScrollViewContents()
}

func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return imageView
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.
}
override func viewDidAppear(animated: Bool) {

scrollView.delegate = self
imageView.frame = CGRectMake(16, 16, scrollView.frame.size.width, scrollView.frame.size.height)
imageView.contentMode = UIViewContentMode.ScaleToFill
imageView.image = UIImage(named: imgstr)
var imagee = UIImage(named: imgstr)
let size = imagee?.size
//imageView.contentMode = UIViewContentMode.ScaleAspectFit
imageView.userInteractionEnabled = true
scrollView.addSubview(imageView)
scrollView.contentSize = size!
let scrollViewFrame = scrollView.frame
let scaleWidth = scrollViewFrame.size.width / scrollView.contentSize.width
let scaleHeight = scrollViewFrame.size.height / scrollView.contentSize.height
let minScale = min(scaleHeight, scaleWidth)

scrollView.minimumZoomScale = minScale
scrollView.maximumZoomScale = 1
scrollView.zoomScale = minScale

centerScrollViewContents()


}

我删除了 centerScrollViewContents() 但我得到了相同的结果

最佳答案

你试过在 layoutSubView 中调用 centerScrollViewContents 吗?像下面的开源一样:https://github.com/huynguyencong/ImageScrollView

关于ios - swift 在 ScrollView 内缩放 UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29797281/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com