我正在尝试在用户首次启动应用程序时实现SlideShow并且一切正常,我可以在图像之间滚动,当我滑动时文本会发生变化,但是存在问题。文本不会与图像同时滑动,只会在我停止滑过屏幕时发生变化。
我想要做的是在滚动图像的同时滚动标签和文字。我知道解决方法是将文本放在图像上,但在这种特殊情况下,它不适合我。有没有办法让视图中的元素与图像同时滚动?
到目前为止我得到了什么:
class SlidesViewController: UIViewController, UIScrollViewDelegate {
@IBOutlet var scrollView: UIScrollView!
@IBOutlet var titleView: UILabel!
@IBOutlet var textView: UITextView!
@IBOutlet var pageControl: UIPageControl!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//1
self.scrollView.frame = CGRect(x:0, y:0, width:self.view.frame.width, height:self.view.frame.height)
let scrollViewWidth:CGFloat = self.scrollView.frame.width
let scrollViewHeight:CGFloat = self.scrollView.frame.height
//2
textView.textAlignment = .center
textView.text = "Sweettutos.com is your blog of choice for Mobile tutorials"
textView.textColor = .red
//3
let imgOne = UIImageView(frame: CGRect(x:0, y:0,width:scrollViewWidth, height:scrollViewHeight))
imgOne.image = UIImage(named: "slide1")
let imgTwo = UIImageView(frame: CGRect(x:scrollViewWidth, y:0,width:scrollViewWidth, height:scrollViewHeight))
imgTwo.image = UIImage(named: "slide2")
let imgThree = UIImageView(frame: CGRect(x:scrollViewWidth*2, y:0,width:scrollViewWidth, height:scrollViewHeight))
imgThree.image = UIImage(named: "slide3")
let imgFour = UIImageView(frame: CGRect(x:scrollViewWidth*3, y:0,width:scrollViewWidth, height:scrollViewHeight))
imgFour.image = UIImage(named: "slide4")
self.scrollView.addSubview(imgOne)
self.scrollView.addSubview(imgTwo)
self.scrollView.addSubview(imgThree)
self.scrollView.addSubview(imgFour)
//4
self.scrollView.contentSize = CGSize(width:self.scrollView.frame.width * 4, height:self.scrollView.frame.height)
self.scrollView.delegate = self
self.pageControl.currentPage = 0
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView){
// Test the offset and calculate the current page after scrolling ends
let pageWidth:CGFloat = scrollView.frame.width
let currentPage:CGFloat = floor((scrollView.contentOffset.x-pageWidth/2)/pageWidth)+1
// Change the indicator
self.pageControl.currentPage = Int(currentPage);
// Change the text accordingly
if Int(currentPage) == 0{
textView.text = "Description of slide 1"
titleView.text = "Text Silde 1"
}else if Int(currentPage) == 1{
textView.text = "Description of slide 2"
titleView.text = "Text Silde 2"
}else if Int(currentPage) == 2{
textView.text = "Description of slide 3"
titleView.text = "Text Silde 3"
pageControl.isHidden = false
}else {
textView.text = "Description of slide 4"
titleView.text = "Text Silde 3"
pageControl.isHidden = true
}
}
}