加入協定
class ViewController: UIViewController, UIScrollViewDelegate {
宣告資料
var scrollview = UIScrollView()
var label = UILabel()
let WIDTH = UIScreen.main.bounds.size.width
let HEIGHT = UIScreen.main.bounds.size.height
viewDidLoad
背景顏色
view.backgroundColor = .lightGray
-
scrollview.frame = CGRect(x: 0,
y: 0,
width: WIDTH,
height: HEIGHT))
scrollview.contentSize = CGSize(width: WIDTH * 2,
height: HEIGHT * 2)
scrollview.contentOffset = CGPoint(x: 0,
y: 20)
scrollview.delegate = self
scrollview.showsHorizontalScrollIndicator = true
scrollview.showsVerticalScrollIndicator = true
scrollview.indicatorStyle = .default
scrollview.scrollsToTop = false
scrollview.isScrollEnabled = true
scrollview.isDirectionalLockEnabled = true
scrollview.isPagingEnabled = true
scrollview.bounces = true
view.addSubview(scrollview)
UILabel(增加滑動視覺效果)
for i in 0...1 {
for j in 0...1 {
let I = CGFloat(i)
let J = CGFloat(j)
label = UILabel(frame: CGRect(x: 0,
y: 0,
width: 200,
height: 100))
label.center = CGPoint(x: (1 + 2 * I) * WIDTH / 2,
y: (1 + 2 * J) * HEIGHT / 2)
label.text = "( \(i) , \(j) )"
label.textColor = .black
label.textAlignment = .center
scrollview.addSubview(label)
}
}