Playground学习笔记

2015-09-19  本文已影响1586人  ZsIsMe

前年当我第一次看到Xcode里面自带的Playground的时候,我并没有意识
到它的强大:

“真是有趣(没什么egg用)的玩意”,

当时我觉得它的作用只在于让人们更方便的学习swift,写写for循环,打印一下数字三角形什么的,当时我的脑洞只停留在这种程度;但当我发现它可以支持UIKit的时候,
我意识到事情没那么简单。

使用playgrou脑洞实现动画效果

真是酷炫不是么?

import UIKit
import XCPlayground

let containerView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 375.0, height: 667.0))
XCPShowView("Container View", view: containerView)

let circle = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 50.0, height: 50.0))
circle.center = containerView.center
circle.layer.cornerRadius = 25.0

let startingColor = UIColor(red: (253.0/255.0), green: (159.0/255.0), blue: (47.0/255.0), alpha: 1.0)
circle.backgroundColor = startingColor

containerView.addSubview(circle);

let rectangle = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 50.0, height: 50.0))
rectangle.center = containerView.center
rectangle.layer.cornerRadius = 5.0

rectangle.backgroundColor = UIColor.whiteColor()

containerView.addSubview(rectangle)

UIView.animateWithDuration(2.0, animations: { () -> Void in
    let endingColor = UIColor(red: (255.0/255.0), green: (61.0/255.0), blue: (24.0/255.0), alpha: 1.0)
    circle.backgroundColor = endingColor

    let scaleTransform = CGAffineTransformMakeScale(5.0, 5.0)

    circle.transform = scaleTransform

    let rotationTransform = CGAffineTransformMakeRotation(3.14)

    rectangle.transform = rotationTransform
})

我逐渐意识到playground的强大,我觉得playground有以下几个优点:

注意右边的箭头,点击展开更多信息 详细信息 支持markdown,其实你可以在里面添加图片都可以

那么,他是怎么写的呢?

注意空格,只需在每行前面添加//:

当我在玩的Playground,觉得它真是神器,难怪Xcode给了他那么好的位置,
当然playground还有很多需要改进的地方,比如:

  1. 比较慢(建议不用的代码先注释掉,
    如果只是使用swift不实用UIKit即iOS那一套的话,建议选择OSX而不是iOS,快点,还有文件不要太大)
  2. 文件容易损坏,即使把代码删除了还是报错,当然很多是beta版时候才出现,但正式版奔溃会少一点
  3. 不算是缺点(其实是我太弱鸡),我觉得那些涉及人机交互的效果在Playground不太好做,
    当然我知道Playground不是万能的,但如果能够直接zai上面实现交互的话,那一定很爽。

说完了,祝大家玩的开心,祝大家有个愉快的周末 !

上一篇 下一篇

猜你喜欢

热点阅读