ios實(shí)現(xiàn)tableView頂部彈簧圖片效果
大家可能注意到有些tableView的頂部圖片,會(huì)隨著你拉伸而跟著拉伸變大。本文實(shí)例為大家分享了ios實(shí)現(xiàn)tableView頂部“彈簧”圖片,供大家參考,具體內(nèi)容如下
一種思路是將圖片放置tableView的tableHeaderView上當(dāng)tablview下移改變圖片的frame達(dá)到效果。當(dāng)然這個(gè)效果特別簡單,高手可以略過。
代碼如下
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! } } }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS中大尺寸圖片的旋轉(zhuǎn)與縮放實(shí)例詳解
圖片縮小旋轉(zhuǎn)是我們?cè)陂_發(fā)中經(jīng)常會(huì)遇到的一個(gè)功能,下面這篇文章主要給大家介紹了關(guān)于iOS中大尺寸圖片的旋轉(zhuǎn)與縮放的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧2018-09-09iOS實(shí)現(xiàn)點(diǎn)擊狀態(tài)欄自動(dòng)回到頂部效果詳解
在IOS開發(fā)過程中,經(jīng)常會(huì)有這種需求,需要通過點(diǎn)擊狀態(tài)欄返回到頂部,給用戶更好的體驗(yàn)效果,下面這篇文章給大家詳細(xì)介紹了實(shí)現(xiàn)過程,有需要的可以參考借鑒。2016-09-09iOS實(shí)現(xiàn)卡片式滾動(dòng)效果 iOS實(shí)現(xiàn)電影選片效果
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)卡片式滾動(dòng)效果,實(shí)現(xiàn)電影選片效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02淺談iOS 數(shù)據(jù)結(jié)構(gòu)之鏈表
這篇文章主要介紹了淺談iOS 數(shù)據(jù)結(jié)構(gòu)之鏈表,本文詳細(xì)的介紹了單鏈表和雙鏈表,具有一定的參考價(jià)值,有興趣的可以了解一下2017-09-09IOS 開發(fā)之Object-C中的對(duì)象詳解
這篇文章主要介紹了IOS 開發(fā)之Object-C中的對(duì)象詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06iOS實(shí)現(xiàn)APP程序內(nèi)部打開APP的AppStore頁面
這篇文章主要給大家介紹了關(guān)于iOS實(shí)現(xiàn)APP程序內(nèi)部打開APP的AppStore頁面的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編一起來看看吧。2017-06-06iOS如何獲取設(shè)備型號(hào)的最新方法總結(jié)
在開發(fā)中,我們經(jīng)常需要獲取設(shè)備的型號(hào)以進(jìn)行數(shù)據(jù)統(tǒng)計(jì)或者做不同的適配。這篇文章主要給大家介紹了關(guān)于iOS如何獲取設(shè)備型號(hào)的最新方法,需要的朋友可以參考下2018-11-11