day one
先建立scrollView對(duì)象(大小先self.view.bounds)
然后添加imageView上去
最后改變scollView大小 一般為self.view.bounds的寬和高的倍數(shù)
方法有幾個(gè)比較常用的 contenSize(確定容量大邢朋稀) contenOffset 內(nèi)容偏移量
pagingEnabled(是否分頁(yè)) directionLocked(鎖定方向 每次滑動(dòng)只能一個(gè)方向)
以下是幾個(gè)方法使用
//==========從UIView繼承下來的屬性==========
//1.創(chuàng)建UIScrollView對(duì)象
//這個(gè)時(shí)候UIScrollView內(nèi)容的大小和scrollView本身是一樣的
let scrollView = UIScrollView.init(frame: self.view.bounds)
//2.添加到界面上
self.view.addSubview(scrollView)
//3.設(shè)置背景顏色
scrollView.backgroundColor = UIColor.yellowColor()
//4.添加imageView
//imageView的坐標(biāo)是(0,0),大小是圖片的大小
/*
let imageView = UIImageView.init(image: UIImage.init(named: "1092.jpg"))
//在scrollView上添加子視圖拣宰,實(shí)質(zhì)是將子視圖添加到它的內(nèi)容上的
scrollView.addSubview(imageView)
*/
//創(chuàng)建多張圖片
for item in 1...4 {
let imageView = UIImageView.init(frame: CGRectMake(CGFloat(item-1)*scrollView.bounds.width, 0, scrollView.bounds.width, scrollView.bounds.height))
scrollView.addSubview(imageView)
imageView.image = UIImage.init(named: "35_\(item).jpg")
}
//=========UIScrollView專有屬性======
//核心屬性
//!!!1.內(nèi)容大小
//scrollView.contentSize = CGSizeMake(self.view.bounds.width * 2, self.view.bounds.height*2)
//如果想要能夠通過滾動(dòng)將圖片內(nèi)容顯示全溜畅,就將scrollView的內(nèi)容大小設(shè)置為和圖片一樣的大小
scrollView.contentSize = CGSizeMake(4*scrollView.bounds.width, scrollView.bounds.height*3)
//!!!2.內(nèi)容偏移量
//scrollView默認(rèn)顯示的是內(nèi)容左上角的部分
scrollView.contentOffset = CGPointMake(scrollView.bounds.width, 0)
//3.設(shè)置是否可以分頁(yè)
//true->每次滾動(dòng)的距離是一頁(yè)的距離
//一頁(yè)的大小就是scrollView的frame的大小舒裤。如果分頁(yè)笤成,那么每次橫向滾動(dòng)滾動(dòng)的距離是scrollView的寬度嘹狞,上下滾動(dòng)的距離是scrollView的高度
scrollView.pagingEnabled = true
//其他屬性
//1.內(nèi)容邊距
//設(shè)置scrollView內(nèi)容到scrollView本身上左下右的距離(默認(rèn)都是0)
//scrollView.contentInset = UIEdgeInsetsMake(20, 20, 20, 20)
//3.設(shè)置是否鎖定一定方法(默認(rèn)false->同一時(shí)間既可以上下滾動(dòng)又可以左右滾動(dòng))
scrollView.directionalLockEnabled = true //true->同一時(shí)間只能上下滾動(dòng)或者左右滾動(dòng)
//3.滑到內(nèi)容邊緣的時(shí)候是否可以滾動(dòng)一段距離再?gòu)椈厝?默認(rèn)true)
scrollView.bounces = false
//4.設(shè)置是否可以滾動(dòng)(默認(rèn)true->可以滾動(dòng))
scrollView.scrollEnabled = true
//5.設(shè)置是否顯示滾動(dòng)條(默認(rèn)true->顯示)
//是否顯示水平方向的滾動(dòng)條
scrollView.showsHorizontalScrollIndicator = true
//是否顯示垂直方向的滾動(dòng)條
scrollView.showsVerticalScrollIndicator = true
//6.設(shè)置滾動(dòng)條到上下左右的邊距
//注意:這個(gè)結(jié)構(gòu)體中的值只有下和右有效赂蠢。下->水平方向的滾動(dòng)條。右->垂直方向的滾動(dòng)條
scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 100, 10)
//7.設(shè)置滾動(dòng)條的風(fēng)格
scrollView.indicatorStyle = .Black
//8.滾動(dòng)到指定區(qū)域(讓指定區(qū)域可見)
//scrollView.scrollRectToVisible(CGRectMake(1000, 100, 100, 100), animated: true)
//9.是否滾動(dòng)到頂部(點(diǎn)擊狀態(tài)欄是否可以自動(dòng)回到頂部)
scrollView.scrollsToTop = true
確定縮放對(duì)象
//7.通過帶動(dòng)畫效果的方式設(shè)置偏移量
//self.scrollView.setContentOffset(CGPointMake(self.scrollView.bounds.width, 0), animated: true)
//8.設(shè)置縮放倍數(shù)
self.scrollView.maximumZoomScale = 3
self.scrollView.minimumZoomScale = 0.5
//2.告訴UIScrollView在縮放的時(shí)候砰蠢,對(duì)哪個(gè)視圖進(jìn)行縮放(設(shè)置縮放對(duì)象)
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView?{
return scrollView.subviews[0]
}
以下是代理
//6.設(shè)置代理
self.scrollView.delegate = self
extension ViewController: UIScrollViewDelegate{
//============滾動(dòng)相關(guān)============
//1.在scrollView滾動(dòng)的時(shí)候?qū)崟r(shí)調(diào)用
func scrollViewDidScroll(scrollView: UIScrollView){
print("正在滾動(dòng)")
print(scrollView.contentOffset)
}
//2.在scrollView將要開始拖動(dòng)的時(shí)候調(diào)用
func scrollViewWillBeginDragging(scrollView: UIScrollView){
print("將要開始拖拽")
}
//3.停止減速的時(shí)候會(huì)調(diào)用(實(shí)質(zhì)就是停止?jié)L動(dòng)的時(shí)候會(huì)調(diào)用)
func scrollViewDidEndDecelerating(scrollView: UIScrollView){
print("停止減速")
}
//4.將要停止拖拽的時(shí)候調(diào)用(手將要松開的時(shí)候,不代表停止?jié)L動(dòng))
//參數(shù)2:手松開時(shí)候在x方向上和在y方向的速度
//參數(shù)3:預(yù)判的滾動(dòng)停止時(shí)候的偏移量
func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>){
print("將要停止拖拽")
print(targetContentOffset)
}
//5.手將要停止拖拽的時(shí)候調(diào)用
func scrollViewWillBeginDecelerating(scrollView: UIScrollView){
print("將要開始減速")
}
//6.已經(jīng)停止?jié)L動(dòng)動(dòng)畫的時(shí)候調(diào)用
//只有在通過setContentOffset的方法使用動(dòng)畫效果讓scrollView滾動(dòng)唉铜,在停止?jié)L動(dòng)的時(shí)候才會(huì)調(diào)用這個(gè)方法
func scrollViewDidEndScrollingAnimation(scrollView: UIScrollView){
print("停止?jié)L動(dòng)動(dòng)畫")
}
//7.只有在通過點(diǎn)擊狀態(tài)欄自動(dòng)滾動(dòng)到頂部的時(shí)候才會(huì)調(diào)用
func scrollViewDidScrollToTop(scrollView: UIScrollView){
print("已經(jīng)滾動(dòng)到頂部")
}
//============縮放相關(guān)=============
//UIScrollView已經(jīng)實(shí)現(xiàn)了縮放功能
//想要讓UIScrollView上的內(nèi)容進(jìn)行縮放必須滿足以下條件:
//a.設(shè)置縮放的最大倍數(shù)和最小倍數(shù)(默認(rèn)都是1)
//b.通過協(xié)議方法告訴UIScrollView縮放對(duì)象
//1.正在縮放的時(shí)候會(huì)實(shí)時(shí)調(diào)用
func scrollViewDidZoom(scrollView: UIScrollView){
print("正在縮放")
}
一般UIScrollView結(jié)合前面滾動(dòng)停止的函數(shù)獲取當(dāng)前是第幾頁(yè)(講偏移的x值除以其寬度)
//MARK: - ScrollView Delegate
extension ViewController: UIScrollViewDelegate{
//停止?jié)L動(dòng)
func scrollViewDidEndDecelerating(scrollView: UIScrollView){
//拿到當(dāng)前是第幾頁(yè)
let page = scrollView.contentOffset.x / scrollView.bounds.width
//更新pageControl
self.pageControl?.currentPage = Int(page)
}
}
//MARK: - PageControl
extension ViewController{
func creatPageControl() {
//1.創(chuàng)建UIPageControl對(duì)象
self.pageControl = UIPageControl.init(frame: CGRectMake(0, 230, Screen_W, 20))
//2.添加到界面上
self.view.addSubview(self.pageControl!)
//3.設(shè)置背景顏色
//self.pageControl?.backgroundColor = UIColor.yellowColor()
//4.核心屬性
//設(shè)置總的頁(yè)數(shù)
self.pageControl?.numberOfPages = self.scrollView.subviews.count
//設(shè)置當(dāng)前頁(yè)(從0開始)
self.pageControl?.currentPage = 0
//5.設(shè)置圓點(diǎn)的顏色
self.pageControl?.pageIndicatorTintColor = UIColor.yellowColor()
self.pageControl?.currentPageIndicatorTintColor = UIColor.redColor()
//6.添加事件
self.pageControl?.addTarget(self, action: "pageControlAction:", forControlEvents: .ValueChanged)
}
//值改變事件發(fā)生調(diào)用的方法
func pageControlAction(pageControl:UIPageControl) {
let offsetX = CGFloat(pageControl.currentPage) * Screen_W
self.scrollView.setContentOffset(CGPointMake(offsetX, 0), animated: true)
}
}
day two
這里主要講的是UITableView的一些用法
主要是兩個(gè)
1.方法
設(shè)置行高
self.tableView.rowHeight = 150
頭文件頭像
self.tableView.tableHeaderView = imageView
分割線
tableView.separatorInset = UIEdgeInsetsMake(0, 10, 0, 10)
//設(shè)置分割線的風(fēng)格
//None -> 沒有分割線
tableView.separatorStyle = .SingleLine
//設(shè)置分割線的顏色
tableView.separatorColor = UIColor.redColor()
2.代理的一些方法
主要有兩個(gè)代理
1.DataSource
就是對(duì)行數(shù)和組數(shù)的設(shè)定 還有行數(shù)里面添加的內(nèi)容圖片
對(duì)行數(shù)有多少行設(shè)定
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
對(duì)組數(shù)有多少組設(shè)定
func numberOfSectionsInTableView(tableView: UITableView) -> Int
創(chuàng)建cell 判斷cell是否有重復(fù)的 用id進(jìn)行判斷 然后在對(duì)數(shù)據(jù)進(jìn)行刷新
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
設(shè)置每個(gè)分組的題目
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?
這里是設(shè)置索引欄
就是右邊出現(xiàn)一個(gè)索引 然后點(diǎn)擊相應(yīng)的地方進(jìn)行跳轉(zhuǎn)
func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]?
2.Delegate
就是對(duì)組數(shù)和行數(shù)的高度設(shè)定
設(shè)定cell的高度
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
設(shè)置header和footer的高度
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0
}
設(shè)定每個(gè)分組的headerView的大小坐標(biāo)顏色
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
注意:
這里還有一個(gè)重要的方法
選擇相應(yīng)的行數(shù)或者組數(shù)進(jìn)行跳轉(zhuǎn) 跳轉(zhuǎn)到下一個(gè)頁(yè)面
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)