gpt4 book ai didi

ios - 使用图像数组进行页面控制

转载 作者:行者123 更新时间:2023-11-30 13:03:59 24 4
gpt4 key购买 nike

我正在尝试制作与图像数组关联的页面控件。我已正确设置它,只需更改背景颜色并加载到初始图像中,但是当我滚动时,不会再显示任何内容。这是我的代码:

var images: [UIImage] = [
UIImage(named: "slide1.png")!,
UIImage(named: "loginButton.png")!,
UIImage(named: "slide1.png")!,
UIImage(named: "slide1.png")!
]

func setPageViewinScroll() {

for index in 0..<4 {

frame.origin.x = self.scrollView.frame.size.width * CGFloat(index)
frame.size = self.scrollView.frame.size
self.scrollView.pagingEnabled = true
self.imageView.image = images[index]
let subview = UIView(frame: frame)
self.scrollView.addSubview(subview)
}

self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * 4, self.scrollView.frame.size.height)

pageControl.addTarget(self, action: Selector("changePage:"), forControlEvents: UIControlEvents.ValueChanged)



}

func changePage(sender: AnyObject) -> () {
let x = CGFloat(pageControl.currentPage) * scrollView.frame.size.width
scrollView.setContentOffset(CGPointMake(x, 0), animated: true)
}

有人知道我哪里出错了吗?

最佳答案

如何将 UIImageview 添加到 UIScrollview 而不是像这段代码那样的 UIView ?

var imageOne:UIImageView!
var imageTwo:UIImageView!
var imageThree:UIImageView!
var imageFour:UIImageView!

func functionCalledFromViewDidLoad() {

scrollView.frame = CGRectMake(0, 0, self.view.frame.width,self.view.frame.height)

let scrollViewWidth = scrollView.frame.width
let scrollViewHeight = scrollView.frame.height

imageOne = UIImageView(frame: CGRectMake(0, 0,scrollViewWidth, scrollViewHeight))
imageTwo = UIImageView(frame: CGRectMake(scrollViewWidth*1, 0,scrollViewWidth, scrollViewHeight))
imageThree = UIImageView(frame: CGRectMake(scrollViewWidth*2, 0,scrollViewWidth, scrollViewHeight))
imageFour = UIImageView(frame: CGRectMake(scrollViewWidth*3, 0,scrollViewWidth, scrollViewHeight))

scrollView.addSubview(imageOne)
scrollView.addSubview(imageTwo)
scrollView.addSubview(imageThree)
scrollView.addSubview(imageFour)

}

override func viewDidLayoutSubviews() {


scrollView.contentSize = CGSizeMake(scrollView.frame.width * 4, scrollView.frame.height)
imageOne.frame.size.height = scrollView.frame.height
imageTwo.frame.size.height = scrollView.frame.height
imageThree.frame.size.height = scrollView.frame.height
imageFour.frame.size.height = scrollView.frame.height
}

您似乎还需要添加此代码。

 func scrollViewDidEndDecelerating(scrollView: UIScrollView) {

// Test the offset and calculate the current page after scrolling ends
let pageWidth:CGFloat = CGRectGetWidth(scrollView.frame)

let currentPage = Int(floor((scrollView.contentOffset.x-pageWidth/2)/pageWidth)+1)

// Change the page indicator
self.pageControl.currentPage = Int(currentPage)
}

有一个与此主题相关的非常好的教程,here

关于ios - 使用图像数组进行页面控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39605586/

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