我在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()
,但我得到了相同的结果
答案 0 :(得分:2)
您是否尝试过调用centerScrollViewContents
中的layoutSubView
?如下面的开源做:https://github.com/huynguyencong/ImageScrollView