您的位置:首页 > 产品设计 > UI/UE

实习笔记:uiscrollview与pagecontrol的综合应用

2017-09-05 22:09 459 查看
实习开发iOS地图租车界面时用到的界面UI元素。

趁有空在这里记录一下。

这是引入项目之前拿来做小测试的小小demo。

import UIKit

class ViewController: UIViewController,UIScrollViewDelegate {

@IBOutlet weak var myScrollView: UIScrollView!

var codePageControl: UIPageControl!

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

let currentPageNumber = Int(round(myScrollView.contentOffset.x / scrollView.frame.size.width))

codePageControl.currentPage = currentPageNumber
}

func codePageControlAction(sender: UIPageControl) {
let currentPageNumber = sender.currentPage
let width = myScrollView.frame.size.width

let offset = CGPoint(x: width * CGFloat(currentPageNumber), y: 0)

myScrollView.setContentOffset(offset, animated: true)
}

override func viewDidLayoutSubviews() {

myScrollView.contentSize.width = myScrollView.frame.width * 3

let viewColors = [UIColor.red,UIColor.green,UIColor.blue]

for i in 0..<3{
let oneView = UIView(
frame: CGRect(
x: CGFloat(i) * myScrollView.frame.size.width,
y: 0,
width: myScrollView.frame.size.width,
height: myScrollView.frame.size.height))
oneView.backgroundColor = viewColors[i]
myScrollView.addSubview(oneView)
}

myScrollView.isPagingEnabled = true

myScrollView.bounces = false

//建立PageControl
codePageControl = UIPageControl(frame: CGRect(x: view.frame.size.width/2 - 30, y: view.frame.size.height - 50, width: 60, height: 37))

codePageControl.pageIndicatorTintColor = UIColor.black

codePageControl.currentPageIndicatorTintColor = UIColor.lightGray

codePageControl.numberOfPages = 3

codePageControl.currentPage = 0

codePageControl.addTarget(self, action: #selector(ViewController.codePageControlAction(sender:)), for: .valueChanged)

view.addSubview(codePageControl)
}


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: