//關掉縱向的滑動桿
myScrollView.showsVerticalScrollIndicator = false
//取消回彈的效果
myScrollView.bounces = false
練習做出單頁式風景滑動:
結果如圖
myScrollView.contentSize.width = (fullScreenSize.width) * CGFloat(sceneArray.count)
//sceneArray為一個存有六個字串的陣列,fullScreen為當前手機屏幕大小。
myScrollView.contentSize.height = fullScreenSize.height
myPageControll.numberOfPages = sceneArray.count
myPageControll.currentPage = 0
myPageControll.currentPageIndicatorTintColor = .blue
myPageControll.pageIndicatorTintColor = .brown
for i in 0...5{
myScrollImageview = UIImageView()
myScrollImageview.frame = CGRect(x: fullScreenSize.width * CGFloat(i), y: 0, width: fullScreenSize.width, height: fullScreenSize.height)
myScrollImageview.image = UIImage(named: sceneArray[i])
self.myScrollView.addSubview(myScrollImageview)
}
@IBAction func pageChanged(_ sender: UIPageControl) {
let currentPageNumber = sender.currentPage
let width = myScrollView.frame.size.width
let offset = CGPoint(x: width * CGFloat(currentPageNumber), y: 0)
//讓ScrollView隨著PageControl到達我們要的位置
myScrollView.setContentOffset(offset, animated: true)
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let currentPage = Int(myScrollView.contentOffset.x / myScrollView.frame.size.width)
myPageControll.currentPage = currentPage
}
GitHub位置:https://github.com/ethan510010/UIScrollView-With-PageControl