gpt4 book ai didi

swift - Playground 程序不运行

转载 作者:行者123 更新时间:2023-12-04 17:45:32 26 4
gpt4 key购买 nike

我在应用程序开发方面相对较新,但出于兴趣,我想在 Xcode 上的 Playground 项目上实现此应用程序:

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()
}

最佳答案

您的代码有效。您需要做的是从“查看”菜单中打开“助理编辑器”以查看结果。

enter image description here

关于swift - Playground 程序不运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48693901/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com