游乐场程序不运行
Posted
技术标签:
【中文标题】游乐场程序不运行【英文标题】:playground program doesn't run 【发布时间】:2018-02-08 20:06:04 【问题描述】:我在应用程序开发方面相对较新,但出于兴趣,我想在 Xcode 上的游乐场项目中实现此应用程序:
http://merowing.info/2015/11/the-beauty-of-imperfection/
所有错误都已更正,但是当我按下运行 Playground 按钮时,它会一直显示“正在运行”,而不会显示任何内容。你知道我做错了什么吗?
import UIKit
import XCPlayground
import QuartzCore
import PlaygroundSupport
class Animator
class TargetProxy
init (target: Animator)
self.target = target
weak var target: Animator!
var totalTime: TimeInterval = 0
@objc func onFire (dl: CADisplayLink)
totalTime += dl.duration
target.block(totalTime)
private lazy var displayLink: CADisplayLink =
return CADisplayLink(target: TargetProxy(target: self), selector: #selector(TargetProxy.onFire))()
private let block: (TimeInterval) -> Void
init (block: @escaping (TimeInterval) -> Void)
self.block = block
displayLink.add(to: RunLoop.main, forMode: RunLoopMode.commonModes)
deinit
displayLink.invalidate()
func createBlobShape() -> UIBezierPath
let ovalPath = UIBezierPath()
ovalPath.move(to: CGPoint.init(x: 13.71, y: -29.07))
ovalPath.addCurve(to: CGPoint.init(x: 27.92, y: -14), controlPoint1: CGPoint.init(x: 20.64, y: -25.95), controlPoint2: CGPoint.init(x: 24.57, y: -20.72))
ovalPath.addCurve(to: CGPoint.init(x: 33, y: 0.5), controlPoint1: CGPoint.init(x: 30.08, y: -9.68), controlPoint2: CGPoint.init(x: 33, y: -4.64))
ovalPath.addCurve(to: CGPoint.init(x: 20.82, y: 26), controlPoint1: CGPoint.init(x: 333, y: 10.93), controlPoint2: CGPoint.init(x: 27.47, y: 17.84))
ovalPath.addCurve(to: CGPoint.init(x: 0, y: 33), controlPoint1: CGPoint.init(x: 16.02, y: 31.88), controlPoint2: CGPoint.init(x: 7.63, y: 33))
ovalPath.addCurve(to: CGPoint.init(x: -16.72, y: 28.33), controlPoint1: CGPoint.init(x: -6.21, y: 33), controlPoint2: CGPoint.init(x: -11.89, y: 31.29))
ovalPath.addCurve(to: CGPoint.init(x: -23.86, y: 22), controlPoint1: CGPoint.init(x: -19.59, y: 26.57), controlPoint2: CGPoint.init(x: -22.22, y: 24.28))
ovalPath.addCurve(to: CGPoint.init(x: -28, y: 17), controlPoint1: CGPoint.init(x: -25.19, y: 20.16), controlPoint2: CGPoint.init(x: -26.74, y: 19.46))
ovalPath.addCurve(to: CGPoint.init(x: -33, y: 0.5), controlPoint1: CGPoint.init(x: -30.24, y: 12.61), controlPoint2: CGPoint.init(x: -33, y: 5.74))
ovalPath.addCurve(to: CGPoint.init(x: -23.86, y: -23), controlPoint1: CGPoint.init(x: -33, y: -9.63), controlPoint2: CGPoint.init(x: -31.23, y: -17.04))
ovalPath.addCurve(to: CGPoint.init(x: -4.57, y: -33), controlPoint1: CGPoint.init(x: -18.17, y: -27.6), controlPoint2: CGPoint.init(x: -12.51, y: -33))
ovalPath.addCurve(to: CGPoint.init(x: 13.71, y: -29.07), controlPoint1: CGPoint.init(x: 0.32, y: -33), controlPoint2: CGPoint.init(x: 9.53, y: -30.95))
ovalPath.close()
return ovalPath
func toRadian(degree: Int) -> Float
return Float(Double(degree) * (Double.pi / 180.0))
let blob = CAShapeLayer()
let blobShape = createBlobShape()
blob.path = blobShape.cgPath
blob.frame = blobShape.bounds
blob.anchorPoint = CGPoint.init(x: 0, y: 0)
blob.fillColor = UIColor.orange.cgColor
var view = UIView(frame: CGRect(x: 0, y: 0, width: 640, height: 480))
view.backgroundColor = UIColor.gray
//grayColor().colorWithAlphaComponent(0.8)
PlaygroundPage.current.liveView = view
blob.position = view.center
view.layer.addSublayer(blob)
let playImageView = UIImageView(image: UIImage(named: "play"))
playImageView.transform = CGAffineTransform.init(scaleX: 0.05, y: 0.05)
playImageView.center = blob.position
view.addSubview(playImageView)
PlaygroundPage.current.needsIndefiniteExecution = true
//XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
let dl = Animator
let skewBaseTime = $0 * 0.3
let rotation = CATransform3DMakeRotation(CGFloat(acos(cos(skewBaseTime))), 0, 0, 1)
let upscale = 5.0
let scaleAdjustment = 0.1
let scale = CATransform3DMakeScale(CGFloat(upscale + abs(sin(skewBaseTime) * scaleAdjustment)), CGFloat(upscale + abs(cos(skewBaseTime) * scaleAdjustment)), 1)
let skewTransform = CGAffineTransform.init(a: 1.0, b: 0.0, c: CGFloat(cos(skewBaseTime + Double.pi) * 0.1), d: 1.0, tx: 0.0, ty: 0.0)
CATransaction.begin()
CATransaction.setValue(kCFBooleanTrue, forKey: kCATransactionDisableActions)
view.layer.sublayerTransform = CATransform3DConcat(CATransform3DMakeAffineTransform(skewTransform), scale)
blob.transform = rotation
CATransaction.commit()
【问题讨论】:
不幸的是,这对于 Playgrounds 来说是正常的。通常没有押韵或理由。 也就是说,我不会让它运行? 尝试重启 Xcode。 这个链接可能对你有帮助***.com/questions/39978092/… 【参考方案1】:您的代码有效。您需要做的是从“查看”菜单中打开“助手编辑器”来查看结果。
【讨论】:
【参考方案2】:你应该更正:
private lazy var displayLink: CADisplayLink =
return CADisplayLink(target: TargetProxy(target: self), selector: #selector(TargetProxy.onFire))()
与:
var displayLink: CADisplayLink
let target = TargetProxy(target: self)
return CADisplayLink(target: target, selector: #selector(target.onFire))
【讨论】:
我更换了它,但它仍然没有显示任何内容。但是,至少我现在在代码的最后一部分(动画部分)中看到一些数字在增加(计算正在进行)。知道吗,为什么我没有看到气泡?? 在 dl = Animator(... 将 upscale 值改为 1.0 你会看到一些东西。【参考方案3】:默认情况下,Playgrounds 与 iCloud 同步。 有时它会导致这种“卡在“正在运行”的错误。
只要去:
系统偏好设置 - Apple ID - 这台 Mac 上使用 iCloud 的应用程序 - iCloud Drive - 选项 - 然后取消选中“Playground”
这个小技巧可以防止同步并解决 Xcode 的冻结状态。
【讨论】:
以上是关于游乐场程序不运行的主要内容,如果未能解决你的问题,请参考以下文章
是否可以在 iPad Swift 游乐场应用程序中保存显示在文件应用程序中的文件