【问题标题】:CABasicAnimation in Swift PlaygroundSwift Playground 中的 CABasicAnimation
【发布时间】:2014-08-11 03:05:40
【问题描述】:

一直在疯狂地尝试在 Swift Playground 中获得动画的实时视图,就像在 WWDC“Swift Playgrounds”视频中一样。我试图做一个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)`

【问题讨论】:

    标签: animation swift-playground


    【解决方案1】:

    解决方案的第一部分是启用在完整模拟器中运行选项。请参阅this answer 了解分步操作。

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

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

    解决方案的第三部分是更正您的动画。 keyPath 应该是transform.scale.x 而不是scale.x 并且您需要添加一个duration,例如:

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

    然后您应该可以在 Playground 中查看动画。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多