美文网首页
swift Playground的singleView界面编写

swift Playground的singleView界面编写

作者: George2016 | 来源:发表于2017-11-17 23:22 被阅读736次

    先上个图

    我们先打开Xcode工程,File->New->Playground或者option+shift+command+N

    选择Single View。

    然后就能看到熟悉的swift的控制器的相关代码了。

    import UIKit
    import PlaygroundSupport
    
    class MyViewController : UIViewController {
        
        override func viewDidLoad() {
            super.viewDidLoad()
            self.view.backgroundColor = UIColor.white
            
            let label = UILabel()
            label.frame = CGRect(x: 100, y: 200, width: 200, height: 20)
            label.text = "Hello World!"
            label.textColor = .black
            label.textAlignment = .center
            label.backgroundColor = UIColor.cyan
            
            self.view.addSubview(label)
            
            let animate = CABasicAnimation(keyPath: "opacity")
            animate.fromValue = 0
            animate.toValue = 0.8
            animate.duration = 0.75
            animate.repeatCount = MAXFLOAT
            animate.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
            animate.fillMode = kCAFillModeBoth
            animate.autoreverses = true
            
            label.layer.add(animate, forKey: "key_opacity")
        }
    }
    // Present the view controller in the Live View window
    PlaygroundPage.current.liveView = MyViewController()
    
    
    //let demoView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
    //demoView.backgroundColor = UIColor.brown
    //PlaygroundPage.current.liveView = demoView
    
    

    通过PlaygroundSupportPlaygroundPage.current.liveView就可以设置当前要显示的View或Controller了。效果如下。

    1. 我们要如何看到界面呢?

    打开assistant editor即可

    2. 我们怎样调试界面呢?

    按住左下角的三角按钮,会弹出一个菜单,选择Manually Run就可以手动运行Playground了,以后每次点击三角按钮才会运行Playground。

    这是不是很简单呢?

    相关文章

      网友评论

          本文标题:swift Playground的singleView界面编写

          本文链接:https://www.haomeiwen.com/subject/fypsvxtx.html