通過兩天學習,對Swift有了簡單的了解,但在實際項目中Swift怎樣使用呢?下面寫了一個小demo,大家來看一下唄!
import UIKit
class RootViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
var backgroundImageView: UIImageView?
override func viewDidLoad() {
super.viewDidLoad()
let screenWidth = UIScreen.mainScreen().bounds.size.width
self.backgroundImageView = UIImageView(frame: CGRect(x: 0, y: -90, width: screenWidth, height: 500))
self.backgroundImageView!.image = UIImage(named: "08.jpg")
self.view .addSubview(self.backgroundImageView!)
let tableView = UITableView(frame: CGRect(x: 0, y: 64, width: screenWidth, height: UIScreen.mainScreen().bounds.size.height - 64), style: .Plain)
tableView.backgroundColor = UIColor.clearColor()
tableView.dataSource = self
tableView.delegate = self
self.view .addSubview(tableView)
let headView = UIView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: 200))
headView.backgroundColor = UIColor.clearColor()
tableView.tableHeaderView = headView
// Do any additional setup after loading the view.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "section :\(section)"
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 10
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("cell")
if cell == nil {
cell = UITableViewCell(style: .Subtitle, reuseIdentifier: "cell")
}
cell!.textLabel?.text = "cell row : \(indexPath.row)"
return cell!
}
func scrollViewDidScroll(scrollView: UIScrollView) {
let contentOffSetY = scrollView.contentOffset.y
var newFrame = self.backgroundImageView!.frame
if contentOffSetY < 0 {
newFrame.origin.y = -90 - contentOffSetY / 6
}else if contentOffSetY >= 0 && contentOffSetY <= 200 {
newFrame.origin.y = -90 - contentOffSetY
}else{
newFrame.origin.y = -90 - 200
}
self.backgroundImageView!.frame = newFrame
}
Swift代碼看起來感覺比OC要簡潔一些,但是始終不太習慣.希望大家對以后對Swift加以重視哦!運行結果:
最后編輯于 :
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者