Swift 滚动视图分页仅显示数组中的最后一张图像
Swift scrollview paging only showing last image from array
我设置了一个启用分页的滚动视图。滚动视图内部是一个 UIImageView(tutorialImageView),它链接到故事板上的图像视图。
我有一组图像,我想在滚动视图中显示为页面。
一切似乎都正常(正确分页),但只显示从数组加载的最后一张图像。因此第 1、2、3 页都是空白,然后 slide4.png 显示在第 4 页上。
import Foundation
import UIKit
class TutorialViewController: UIViewController {
@IBOutlet weak var back: UIButton?
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var tutorialImageView: UIImageView?
var slides:[UIImage] = [UIImage(named: "slide1.png")!, UIImage(named: "slide2.png")!, UIImage(named: "slide3.png")!, UIImage(named: "slide4.png")!]
var frame: CGRect = CGRectMake(0, 0, 0, 0)
override func viewDidLoad() {
super.viewDidLoad()
for index in 0..<slides.count {
frame.origin.x = self.scrollView!.frame.size.width * CGFloat(index)
frame.size = self.scrollView!.frame.size
self.scrollView!.pagingEnabled = true
var subView = UIView(frame: frame)
tutorialImageView!.image = slides[index]
subView.addSubview(tutorialImageView!)
self.scrollView!.addSubview(subView)
}
self.scrollView!.contentSize = CGSizeMake(self.scrollView!.frame.size.width * CGFloat(slides.count), self.scrollView!.frame.size.height)
}
我正在努力寻找我所缺少的东西。提前致谢
试试这个,只需从您的代码中分出一些内容,它应该可以工作。
全局变量:
var imageWidth: CGFloat = 310
var imageHeight: CGFloat = 400
var xPosition: CGFloat = 0
var scrollViewContentSize: CGFloat = 0
var myImage: UIImage = //array of UIImage
创建进入滚动视图的 UIImageView。
for var index = 0; index < myImages.count; index++ {
var myImage: UIImage = myImages[index]!
var myImageView: UIImageView = UIImageView()
myImageView.image = myImage
myImageView.frame.size.width = imageWidth
myImageView.frame.size.height = imageHeight
myImageView.frame.origin.x = xPosition
myImageView.frame.origin.y = 0
imageScrollView.addSubview(myImageView)
xPosition += imageWidth
scrollViewContentSize += imageWidth
imageScrollView.contentSize = CGSize(width: scrollViewContentSize, height: imageHeight)
}
我设置了一个启用分页的滚动视图。滚动视图内部是一个 UIImageView(tutorialImageView),它链接到故事板上的图像视图。
我有一组图像,我想在滚动视图中显示为页面。
一切似乎都正常(正确分页),但只显示从数组加载的最后一张图像。因此第 1、2、3 页都是空白,然后 slide4.png 显示在第 4 页上。
import Foundation
import UIKit
class TutorialViewController: UIViewController {
@IBOutlet weak var back: UIButton?
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var tutorialImageView: UIImageView?
var slides:[UIImage] = [UIImage(named: "slide1.png")!, UIImage(named: "slide2.png")!, UIImage(named: "slide3.png")!, UIImage(named: "slide4.png")!]
var frame: CGRect = CGRectMake(0, 0, 0, 0)
override func viewDidLoad() {
super.viewDidLoad()
for index in 0..<slides.count {
frame.origin.x = self.scrollView!.frame.size.width * CGFloat(index)
frame.size = self.scrollView!.frame.size
self.scrollView!.pagingEnabled = true
var subView = UIView(frame: frame)
tutorialImageView!.image = slides[index]
subView.addSubview(tutorialImageView!)
self.scrollView!.addSubview(subView)
}
self.scrollView!.contentSize = CGSizeMake(self.scrollView!.frame.size.width * CGFloat(slides.count), self.scrollView!.frame.size.height)
}
我正在努力寻找我所缺少的东西。提前致谢
试试这个,只需从您的代码中分出一些内容,它应该可以工作。
全局变量:
var imageWidth: CGFloat = 310
var imageHeight: CGFloat = 400
var xPosition: CGFloat = 0
var scrollViewContentSize: CGFloat = 0
var myImage: UIImage = //array of UIImage
创建进入滚动视图的 UIImageView。
for var index = 0; index < myImages.count; index++ {
var myImage: UIImage = myImages[index]!
var myImageView: UIImageView = UIImageView()
myImageView.image = myImage
myImageView.frame.size.width = imageWidth
myImageView.frame.size.height = imageHeight
myImageView.frame.origin.x = xPosition
myImageView.frame.origin.y = 0
imageScrollView.addSubview(myImageView)
xPosition += imageWidth
scrollViewContentSize += imageWidth
imageScrollView.contentSize = CGSize(width: scrollViewContentSize, height: imageHeight)
}