Swift游乐场中的CABasicAnimation

在WWDC“ Swift Playgrounds”视频中试图在Swift Playground中获得动画的实时视图.我试图做一个没有运气的UIView.animateWithDuration所以我现在正在尝试做CABasicaAnimation,因为这似乎是演示中使用的内容.我已经摆脱了许多令人困惑的错误,并导入了适当的框架,但我仍然没有运气让我的小圈子做任何事情,但坐在我的XcpshowView的中心.这是我的代码
import Foundation
 import XCPlayground
 import UIKit
 import QuartzCore

//Circle Button
class timerButtonGraphics: UIView {
    override func drawRect(rect: CGRect) {
        let colorGreen = UIColor(red: 0.310,green: 0.725,blue: 0.624,alpha: 1.000)
        let colorRed = UIColor(red: 241/255,green: 93/255,blue: 79/255,alpha: 100)
        var bounds = self.bounds
        var center = CGPoint()
        center.x = bounds.origin.x + bounds.size.width / 2
        center.y = bounds.origin.y + bounds.size.height / 2
        var radius = 61
        var path:UIBezierPath = UIBezierPath()
        path.addArcWithCenter(center,radius: CGFloat(radius),startAngle: CGFloat(0.0),endAngle: CGFloat(Float(M_PI) * 2.0),clockwise: true)
        path.strokeWithBlendMode(kCGBlendModenormal,alpha: 100)
        path.linewidth = 5
        colorGreen.setstroke()
        path.stroke()

        //Define the animation here
        var anim = CABasicAnimation()
        anim.keyPath = "scale.x"
        anim.fromValue = 1
        anim.tovalue = 100
        anim.delegate = self
        self.layer.addAnimation(anim,forKey: nil)
    }
}

var test = timerButtonGraphics(frame: CGRect(x: 0,y: 0,width: 400,height: 400))
test.setTranslatesAutoresizingMaskIntoConstraints(true)


XcpshowView("Circle Animation",test)`
解决方案的第一部分是启用“在完全模拟器中运行”选项.请参阅 this answer获取详细步骤.

解决方案的第二部分是在容器视图中包含您的动画视图,例如:

let container = UIView(frame: CGRect(x: 0,height: 400))
XcpshowView("Circle Animation",container)
let test = timerButtonGraphics(frame: CGRect(x: 198,width: 4,height: 400))
container.addSubview(test)

解决方案的第三部分是纠正您的动画. keyPath应该是transform.scale.x而不是scale.x,你需要添加一个持续时间,例如:

anim.keyPath = "transform.scale.x"
anim.duration = 5

然后,您应该能够在游乐场中查看动画.

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...