Swift的实际应用

通过两天学习,对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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,195评论 4 61
  • 到底什麽是爱 是否晴天娃娃的等待 悲伤 在风中轻轻摇摆 微笑 也是一种无奈 从开始到现在 我们之间两三句的对白如此...
    范范子诗阅读 1,657评论 0 0