ios實現(xiàn)tableView頂部彈簧圖片效果
更新時間:2017年08月02日 15:50:05 作者:Kamto
這篇文章主要為大家詳細介紹了ios實現(xiàn)tableView頂部彈簧圖片效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
大家可能注意到有些tableView的頂部圖片,會隨著你拉伸而跟著拉伸變大。本文實例為大家分享了ios實現(xiàn)tableView頂部“彈簧”圖片,供大家參考,具體內容如下
一種思路是將圖片放置tableView的tableHeaderView上當tablview下移改變圖片的frame達到效果。當然這個效果特別簡單,高手可以略過。
代碼如下
import UIKit
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
lazy var myTableView : UITableView! = {
var tableView = UITableView.init(frame: self.view.frame,style:UITableViewStyle.plain)
tableView.delegate = self
tableView.dataSource = self
tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: "mycell")
return tableView
}()
var headerImageView:UIImageView?
var headerView:UIView?
var headerViewHeight:CGFloat = 0.0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
setupUI()
}
func setupUI(){
headerView = UIView.init(frame: CGRect(x:0,y:0,width:self.view.frame.width,height:300))
headerViewHeight = headerView!.frame.height;
self.view.addSubview(headerView!)
headerImageView = UIImageView.init(frame: headerView!.frame)
headerImageView?.image = UIImage.init(named: "bg-mine")
headerView?.addSubview(headerImageView!)
myTableView.tableHeaderView = headerView
self.view.addSubview(myTableView)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "mycell", for: indexPath)
cell.textLabel?.text = "測試"
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let contentOffSetY = scrollView.contentOffset.y
if contentOffSetY < 0 {
var rect = headerView?.frame
rect?.size.height = headerViewHeight - contentOffSetY
let headerViewWidth = headerView?.frame.size.width
rect?.size.width = headerViewWidth!*(headerViewHeight-contentOffSetY)/headerViewHeight
rect?.origin.x = -((rect?.size.width)! - headerViewWidth!)/2
rect?.origin.y = contentOffSetY
headerView?.frame = rect!
headerImageView?.frame = rect!
}
}
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
iOS實現(xiàn)點擊狀態(tài)欄自動回到頂部效果詳解
在IOS開發(fā)過程中,經(jīng)常會有這種需求,需要通過點擊狀態(tài)欄返回到頂部,給用戶更好的體驗效果,下面這篇文章給大家詳細介紹了實現(xiàn)過程,有需要的可以參考借鑒。2016-09-09
iOS實現(xiàn)卡片式滾動效果 iOS實現(xiàn)電影選片效果
這篇文章主要為大家詳細介紹了iOS實現(xiàn)卡片式滾動效果,實現(xiàn)電影選片效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-02-02
iOS實現(xiàn)APP程序內部打開APP的AppStore頁面
這篇文章主要給大家介紹了關于iOS實現(xiàn)APP程序內部打開APP的AppStore頁面的相關資料,文中介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面跟著小編一起來看看吧。2017-06-06

