swift Playground的singleView界面编写

先上个图

我们先打开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。

这是不是很简单呢?

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

推荐阅读更多精彩内容

  • [译] 零基础 macOS 应用开发(二) 本文翻译自 raywenderlich.com 的 macOS 开发经...
    SR2k阅读 3,663评论 1 3
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,250评论 4 61
  • 问题与思考:在项目中需要获取h5页面中图片的url,方便将url取出在分享界面使用。要完成这个目标,就要涉及到解析...
    蓝猫淘气三千问阅读 2,094评论 4 6
  • 不少人都经历过高三,是苦是乐各位心中都有数。反正我认为在我活过的这十七年中,最是苦不堪言。 每天五点起床十一点睡觉...
    看青蛙的天空阅读 195评论 0 0