gpt4 book ai didi

ios - UIPageControl 和 UIScrollView 不滚动

转载 作者:行者123 更新时间:2023-11-28 05:55:10 25 4
gpt4 key购买 nike

我有一个相对简单的 UIViewController,其中一个 UIScrollView 占据了屏幕的一半,还有一个 UIImageView 放在 中UIScrollViewUIScrollView 大小完全相同。

UIImageView 之上有一个 UIPageControl。要点是水平滚动并根据数组中的图像数量呈现像 slider 一样的图像。问题是 ScrollView 没有滚动,我不知道为什么。

 @IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var pageControl: UIPageControl!

let imagelist = ["3.jpg", "1.jpg", "2.png", "4.png", "5.png"]

override func viewDidLoad() {
super.viewDidLoad()
scrollView.delegate = self
configurePageControl()

for i in stride(from: 0, to: imagelist.count, by: 1) {
var frame = CGRect.zero
frame.origin.x = self.scrollView.frame.size.width * CGFloat(i)
frame.origin.y = 0
frame.size = self.scrollView.frame.size
scrollView.isPagingEnabled = true
scrollView.isScrollEnabled = true

let myImage:UIImage = UIImage(named: imagelist[i])!
let bgColorFromImage = myImage.averageColor
imageView.image = myImage
imageView.contentMode = UIViewContentMode.scaleAspectFit
imageView.frame = frame


scrollView.backgroundColor = bgColorFromImage
scrollView.addSubview(imageView)
}

self.scrollView.contentSize = CGSize(width: self.scrollView.frame.size.width * CGFloat(imagelist.count), height: self.scrollView.frame.size.height)
pageControl.addTarget(self, action: #selector(changePage), for: UIControlEvents.valueChanged)

}

func configurePageControl() {
self.pageControl.numberOfPages = imagelist.count
self.pageControl.currentPage = 0
self.pageControl.tintColor = UIColor.red
self.pageControl.pageIndicatorTintColor = UIColor.black
self.pageControl.currentPageIndicatorTintColor = UIColor.green
self.imageView.addSubview(pageControl)
}

@objc func changePage() {
let x = CGFloat(pageControl.currentPage) * scrollView.frame.size.width
scrollView.setContentOffset(CGPoint(x: x,y :0), animated: true)
}

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let pageNumber = round(scrollView.contentOffset.x / scrollView.frame.size.width)
pageControl.currentPage = Int(pageNumber)
}

这是 UIStoryboard 的样子:

enter image description here

最佳答案

在 for 循环中你使用了相同的 imageView 对象,你只设置了框架和图像

imageView.frame = frame
imageView.image = myImage

但您必须创建一个新的 UIImageView 实例

//

假设您在 IB 中有一个 scrollView,它具有 superView 的顶部、前导和尾随约束,并且高度为 200,您可以像这样动态添加 imageView

class ViewController: UIViewController {

@IBOutlet weak var scrollView: UIScrollView!

override func viewDidLoad() {
super.viewDidLoad()


for i in 0..<10 {

let f = CGRect(x: CGFloat(i) * scrollView.frame.width, y: 0, width: scrollView.frame.width, height: scrollView.frame.height)

let imgV = UIImageView(frame: f)

imgV.image = UIImage(named: "re-fuel.png")

scrollView.addSubview(imgV)


}

scrollView.contentSize = CGSize(width: CGFloat(10) * scrollView.frame.width, height: scrollView.frame.height)
}
}

结果

enter image description here

关于ios - UIPageControl 和 UIScrollView 不滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51675303/

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