[英]Center image in UIScrollView with Swift
I have a picture that is long, that I want to show all scrolling the page from top to bottom, with a ScrollView. 我有一幅很长的图片,我想显示所有使用ScrollView从上到下滚动的页面。
My problem is that the image is all to the right of the page, and there is no way to center it in the middle of the page. 我的问题是图像位于页面的右侧,并且无法将其居中在页面中间。 Instead, when I double tap the page to zoom the photo, the image come to the center of the page, and there are no problems.
取而代之的是,当我双击页面以缩放照片时,图像到达页面的中心,并且没有问题。
The error is only when I enter to that page and the photo is all to the right, cutted because part of it go outside the page. 仅当我进入该页面并且照片在右侧时才出现错误,因为该图像的一部分超出了页面,因此被切除。
I am using Swift, with Autolayout (my app doesn't work without it), and this is the code of the ScrollView. 我正在使用带有自动布局的Swift(没有它我的应用程序将无法工作),这是ScrollView的代码。 I hope that you can help me...
我希望你能帮助我...
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
navigationItem.title = nameString!
imageView?.image = UIImage(named: imageName!)
nameLabel?.text = nameString!
// 1
let image = UIImage(named: imageName!)!
imageView = UIImageView(image: image)
imageView.frame = CGRect(origin: CGPoint(x: 0, y: 0), size:image.size)
scrollView.addSubview(imageView)
// 2
scrollView.contentSize = image.size
// 3
var doubleTapRecognizer = UITapGestureRecognizer(target: self, action: "scrollViewDoubleTapped:")
doubleTapRecognizer.numberOfTapsRequired = 2
doubleTapRecognizer.numberOfTouchesRequired = 1
scrollView.addGestureRecognizer(doubleTapRecognizer)
// 4
let scrollViewFrame = scrollView.frame
let scaleWidth = scrollViewFrame.size.width / scrollView.contentSize.width
let scaleHeight = scrollViewFrame.size.height / scrollView.contentSize.height
let minScale = min(scaleWidth, scaleHeight);
scrollView.minimumZoomScale = minScale;
// 5
scrollView.maximumZoomScale = 1.0
scrollView.zoomScale = minScale;
// 6
centerScrollViewContents()
}
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.0
} else {
contentsFrame.origin.x = 0.0
}
if contentsFrame.size.height < boundsSize.height {
contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0
} else {
contentsFrame.origin.y = 0.0
}
imageView.frame = contentsFrame
}
func scrollViewDoubleTapped(recognizer: UITapGestureRecognizer) {
// 1
let pointInView = recognizer.locationInView(imageView)
// 2
var newZoomScale = scrollView.zoomScale * 1.5
newZoomScale = min(newZoomScale, scrollView.maximumZoomScale)
// 3
let scrollViewSize = scrollView.bounds.size
let w = scrollViewSize.width / newZoomScale
let h = scrollViewSize.height / newZoomScale
let x = pointInView.x - (w / 2.0)
let y = pointInView.y - (h / 2.0)
let rectToZoomTo = CGRectMake(x, y, w, h);
// 4
scrollView.zoomToRect(rectToZoomTo, animated: true)
}
func viewForZoomingInScrollView(scrollView: UIScrollView!) -> UIView! {
return imageView
}
func scrollViewDidZoom(scrollView: UIScrollView!) {
centerScrollViewContents()
}
Where is the problem? 问题出在哪儿? Thank you all
谢谢你们
解决了,只需要取消选中大小类并删除LaunchScreen大小即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.