CABasicAnimation斯威夫特游乐场 [英] CABasicAnimation in Swift Playground

查看:181
本文介绍了CABasicAnimation斯威夫特游乐场的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经持续坚果试图让在斯威夫特操场动画的实时取景像在WWDC雨燕操场的视频。我试图做一个 UIView.animateWithDuration 没有运气,所以我现在试图做一个CABasicaAnimation,因为这似乎是什么在演示中使用。 I'be摆脱了很多混乱的错误,并已导入适当的框架,但我仍然没有运气让我的小圆圈做任何事情,但在我的 XCPShowView 。这里是我的code:

 进口基金会
 进口XCPlayground
 进口的UIKit
 进口QuartzCore//圆圈键
类timerButtonGraphics:UIView的{
    覆盖FUNC的drawRect(RECT:的CGRect){
        让颜色绿色=的UIColor(红色:0.310,绿色环保:0.725,蓝:0.624,阿尔法:1.000)
        让colorRed =的UIColor(红色:二百五十五分之二百四十一,绿色环保:255分之93,蓝:二百五十五分之七十九,阿尔法:100)
        VAR边界= self.bounds
        VAR中心= CGPoint()
        center.x = bounds.origin.x + bounds.size.width / 2
        center.y = bounds.origin.y + bounds.size.height / 2
        VAR半径= 61
        VAR路径:UIBezierPath = UIBezierPath()
        path.addArcWithCenter(圆心,半径:CGFloat的(半径),由startAngle:CGFloat的(0.0),endAngle:CGFloat的(浮动(M_PI)* 2.0),顺时针方向:真)
        path.strokeWithBlendMode(kCGBlendModeNormal,阿尔法:100)
        path.lineWidth = 5
        colorGreen.setStroke()
        path.stroke()        //此处定义动画
        VAR动画= CABasicAnimation()
        anim.keyPath =scale.x
        anim.fromValue = 1
        anim.toValue = 100
        anim.delegate =自
        self.layer.addAnimation(动画,forKey:无)
    }
}VAR测试= timerButtonGraphics(帧:的CGRect(X:0,Y:0,宽度:400,高度:400))
test.setTranslatesAutoresizingMaskIntoConstraints(真)
XCPShowView(圆动画,测试)`


解决方案

解决方案的第一部分是启用的在全模拟器中运行选项。请参见这个答案了一步一步的。

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

 让容器=的UIView(帧:的CGRect(X:0,Y:0,宽度:400,高度:400))
XCPShowView(圆动画容器)
让测试= timerButtonGraphics(帧:的CGRect(X:198,Y:0,宽度:4,身高:400))
container.addSubview(测试)

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

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

您应该然后就可以在你的操场上观看动画。

Been going nuts trying to get a live view of an animation in a Swift Playground like in the WWDC "Swift Playgrounds" video. I tried to do a UIView.animateWithDuration with no luck so I am now trying to do a CABasicaAnimation because this seems to be what was used in the demo. I'be gotten rid of a lot of confusing errors and have imported the proper frameworks but am still having no luck getting my little circle to do anything but sit still in the center of my XCPShowView. Here is my code:

 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)`

解决方案

The first part of the solution is to enable the Run in Full Simulator option. See this answer for a step-by-step.

The second part of the solution is to contain your animated view in a container view, e.g.:

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)

The third part of the solution is to correct your animation. The keyPath should be transform.scale.x instead of scale.x and you need to add a duration, e.g.:

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

You should then be able to view the animation in your playground.

这篇关于CABasicAnimation斯威夫特游乐场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆