通过两天学习,对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加以重视哦!运行结果:
1.gif
网友评论