我的ScrollView运行顺畅。我已将图像输入资产库。名称从1开始并结束39.我想放大我的图像。我尝试了不同的方法,但我没有成功。
这是我的代码:
import UIKit
class fotolarView: UIViewController, UIScrollViewDelegate {
@IBOutlet weak var mainScroolView: UIScrollView!
var imageArray = [UIImage]()
override func viewDidLoad() {
super.viewDidLoad()
mainScroolView.delegate = self
mainScroolView.frame = view.frame
imageArray = [#imageLiteral(resourceName: "1"), #imageLiteral(resourceName: "2"), #imageLiteral(resourceName: "3"), #imageLiteral(resourceName: "4"), #imageLiteral(resourceName: "5"), #imageLiteral(resourceName: "6"), #imageLiteral(resourceName: "7"), #imageLiteral(resourceName: "10"), #imageLiteral(resourceName: "8"), #imageLiteral(resourceName: "9"), #imageLiteral(resourceName: "11"), #imageLiteral(resourceName: "12"), #imageLiteral(resourceName: "20"), #imageLiteral(resourceName: "13"), #imageLiteral(resourceName: "21"), #imageLiteral(resourceName: "22"), #imageLiteral(resourceName: "15"), #imageLiteral(resourceName: "23"), #imageLiteral(resourceName: "16"), #imageLiteral(resourceName: "31"), #imageLiteral(resourceName: "24"), #imageLiteral(resourceName: "17"), #imageLiteral(resourceName: "25") , #imageLiteral(resourceName: "32"), #imageLiteral(resourceName: "33"), #imageLiteral(resourceName: "19"), #imageLiteral(resourceName: "26"), #imageLiteral(resourceName: "34"), #imageLiteral(resourceName: "27"), #imageLiteral(resourceName: "35"), #imageLiteral(resourceName: "28"), #imageLiteral(resourceName: "29"), #imageLiteral(resourceName: "36"), #imageLiteral(resourceName: "37"), #imageLiteral(resourceName: "38"), #imageLiteral(resourceName: "39")]
for i in 0..<imageArray.count {
let imageView = UIImageView()
imageView.image = imageArray[i]
imageView.isUserInteractionEnabled = true
imageView.contentMode = .scaleAspectFit
let xPozition = self.view.frame.width * CGFloat(i)
imageView.frame = CGRect(x: xPozition, y: 0, width: self.mainScroolView.frame.width, height: self.mainScroolView.frame.height)
mainScroolView.contentSize.width = mainScroolView.frame.width * CGFloat(i + 1)
mainScroolView.addSubview(imageView)
}
}
}
答案 0 :(得分:8)
var scrollView: UIScrollView!
var imageView: UIImageView!
override func viewDidLoad()
{
super.viewDidLoad()
imageView = UIImageView(image: UIImage(named: "image.png"))
scrollView = UIScrollView(frame: view.bounds)
scrollView.backgroundColor = UIColor.black
scrollView.contentSize = imageView.bounds.size
scrollView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
scrollView.contentOffset = CGPoint(x: 1000, y: 450)
scrollView.addSubview(imageView)
view.addSubview(scrollView)
scrollView.delegate = self
setZoomScale()
setupGestureRecognizer()
}
func viewForZooming(in scrollView: UIScrollView) -> UIView?
{
return imageView
}
override func viewWillLayoutSubviews()
{
setZoomScale()
}
func scrollViewDidZoom(_ scrollView: UIScrollView)
{
let imageViewSize = imageView.frame.size
let scrollViewSize = scrollView.bounds.size
let verticalPadding = imageViewSize.height < scrollViewSize.height ? (scrollViewSize.height - imageViewSize.height) / 2 : 0
let horizontalPadding = imageViewSize.width < scrollViewSize.width ? (scrollViewSize.width - imageViewSize.width) / 2 : 0
scrollView.contentInset = UIEdgeInsets(top: verticalPadding, left: horizontalPadding, bottom: verticalPadding, right: horizontalPadding)
}
func setZoomScale()
{
let imageViewSize = imageView.bounds.size
let scrollViewSize = scrollView.bounds.size
let widthScale = scrollViewSize.width / imageViewSize.width
let heightScale = scrollViewSize.height / imageViewSize.height
scrollView.minimumZoomScale = min(widthScale, heightScale)
scrollView.zoomScale = 1.0
}
func setupGestureRecognizer()
{
let doubleTap = UITapGestureRecognizer(target: self, action: #selector(ScrollViewController.handleDoubleTap(_:)))
doubleTap.numberOfTapsRequired = 2
scrollView.addGestureRecognizer(doubleTap)
}
func handleDoubleTap(_ recognizer: UITapGestureRecognizer)
{
if (scrollView.zoomScale > scrollView.minimumZoomScale)
{
scrollView.setZoomScale(scrollView.minimumZoomScale, animated: true)
} else {
scrollView.setZoomScale(scrollView.maximumZoomScale, animated: true)
}
}
答案 1 :(得分:0)
只需替换上述代码中的以下功能即可获得更高质量。
override func viewWillLayoutSubviews()
{
setZoomScale()
scrollView.setZoomScale(scrollView.minimumZoomScale, animated: true)
}
func setZoomScale()
{
let imageViewSize = imageView.bounds.size
let scrollViewSize = scrollView.bounds.size
let widthScale = scrollViewSize.width / imageViewSize.width
let heightScale = scrollViewSize.height / imageViewSize.height
scrollView.minimumZoomScale = min(widthScale, heightScale)
scrollView.maximumZoomScale = 6.0
}