swift – 如何为每个象限创建一个圆形末端的圆圈

swift – 如何为每个象限创建一个圆形末端的圆圈,第1张

概述我创建了一个分为象限的圆圈.我试图让每个象限的末端四舍五入,每个象限之间有一个给定的间隙,但我得到了一些奇怪的行为.我想我错过了一些东西.下面是我得到的图像和相关代码.我希望结局类似于Apple Watch活动环. class GameScene: SKScene {let radius = CGFloat(100)var topRightPathNode : SKShapeNode!va 我创建了一个分为象限的圆圈.我试图让每个象限的末端四舍五入,每个象限之间有一个给定的间隙,但我得到了一些奇怪的行为.我想我错过了一些东西.下面是我得到的图像和相关代码.我希望结局类似于Apple Watch活动环.

class GameScene: SKScene {let radius = CG@R_403_5987@(100)var topRightPathNode : SKShapeNode!var bottomrightPathNode : SKShapeNode!var bottomleftPathNode : SKShapeNode!var topleftPathNode : SKShapeNode!overrIDe func dIDMove(to vIEw: SKVIEw) {}overrIDe init(size: CGSize) {    super.init(size: size)    let topRightPath = arcSegment(center: CGPoint.zero,radius: radius,strokeWIDth: 18,gapWIDth: 6)    // top RIGHT    topRightPathNode = SKShapeNode(path: topRightPath)    topRightPathNode.fillcolor = SKcolor.white    topRightPathNode.linewidth = 0    topRightPathNode.position = CGPoint(x: 320,y: 240)    addChild(topRightPathNode)    // BottOM RIGHT    var reflectOnY = CGAffinetransform(scaleX: 1.0,y: -1.0)    let bottomrightPath = topRightPath.copy(using: &reflectOnY)!    bottomrightPathNode = SKShapeNode(path: bottomrightPath)    bottomrightPathNode.fillcolor = SKcolor.red    bottomrightPathNode.linewidth = 0    bottomrightPathNode.position = CGPoint(x: 320,y: 240)   addChild(bottomrightPathNode)    // BottOM left    //var reflectOnX = CGAffinetransform(scaleX: -1.0,y: 1.0)    //let bottomleftPath = bottomrightPath.copy(using: &reflectOnX)!    var reflectOnXAndY = CGAffinetransform(scaleX: -1.0,y: -1.0)    let bottomleftPath = topRightPath.copy(using: &reflectOnXAndY)!    bottomleftPathNode = SKShapeNode(path: bottomleftPath)    bottomleftPathNode.fillcolor = SKcolor.purple    bottomleftPathNode.linewidth = 0    bottomleftPathNode.position = CGPoint(x: 320,y: 240)    addChild(bottomleftPathNode)    // top left    var reflectOnX = CGAffinetransform(scaleX: -1.0,y: 1.0)    let topleftPath = topRightPath.copy(using: &reflectOnX)!    topleftPathNode = SKShapeNode(path: topleftPath)    topleftPathNode.fillcolor = SKcolor.cyan    topleftPathNode.linewidth = 0    topleftPathNode.position = CGPoint(x: 320,y:240)    addChild(topleftPathNode)}required init?(coder aDecoder: NSCoder) {    fatalError("init(coder:) has not been implemented")}func arcSegment(center : CGPoint,radius: CG@R_403_5987@,strokeWIDth: CG@R_403_5987@,gapWIDth: CG@R_403_5987@) -> CGPath{    let halfstrokeWIDth = strokeWIDth / 2.0    let outerRadius = radius + halfstrokeWIDth    let innerRadius = radius - halfstrokeWIDth    let halfGap = gapWIDth / 2.0    let outerStartAngle = CG@R_403_5987@(atan2(sqrt(outerRadius * outerRadius - halfGap * halfGap),halfGap))    let outerEndAngle = CG@R_403_5987@(atan2(halfGap,sqrt(outerRadius * outerRadius - halfGap * halfGap)))    let innerStartAngle = CG@R_403_5987@(atan2(halfGap,sqrt(innerRadius * innerRadius - halfGap * halfGap)))    let innerEndAngle = CG@R_403_5987@(atan2(sqrt(innerRadius * innerRadius - halfGap * halfGap),halfGap))    let path = CGMutablePath()    path.addArc(center: center,radius: outerRadius,startAngle: outerStartAngle,endAngle: outerEndAngle,clockwise: true)    // Quartz 2D will assume a "moveto" here    path.addArc(center: CGPoint(x: center.x + radius,y: center.y),radius: halfstrokeWIDth,clockwise: false)    path.addArc(center: center,radius: innerRadius,startAngle: innerStartAngle,endAngle: innerEndAngle,clockwise: false)    path.closeSubpath()    return path}
解决方法 这是另一组代码,其中生成的路径具有圆形末端,但每个弧段是填充路径而不是单个描边段.这应该允许路径在没有端盖重叠的情况下发生碰撞.

import UIKitimport SpriteKitimport PlaygroundSupportlet radius = CG@R_403_5987@(100)let scenesize = CGSize(wIDth: 640,height: 480)let sceneVIEw = SKVIEw(frame: CGRect(origin: CGPoint.zero,size: scenesize))let scene = SKScene(size: scenesize)scene.backgroundcolor = UIcolor.blacklet topRightPath = arcSegment(radius: radius,gapWIDth: 25)let topRightPathNode = SKShapeNode(path: topRightPath)topRightPathNode.fillcolor = SKcolor.whitetopRightPathNode.position = CGPoint(x: 320,y: 240)topRightPathNode.linewidth = 0scene.addChild(topRightPathNode)var reflectOnY = CGAffinetransform(scaleX: 1.0,y: -1.0)let bottomrightPath = topRightPath.copy(using: &reflectOnY)!let bottomrightPathNode = SKShapeNode(path: bottomrightPath)bottomrightPathNode.fillcolor = SKcolor.orangebottomrightPathNode.position = CGPoint(x: 320,y: 240)bottomrightPathNode.linewidth = 0scene.addChild(bottomrightPathNode)var reflectOnX = CGAffinetransform(scaleX: -1.0,y: 1.0)let bottomleftPath = bottomrightPath.copy(using: &reflectOnX)!let bottomleftPathNode = SKShapeNode(path: bottomleftPath)bottomleftPathNode.fillcolor = SKcolor.greenbottomleftPathNode.position = CGPoint(x: 320,y: 240)bottomleftPathNode.linewidth = 0scene.addChild(bottomleftPathNode)let topleftPath = bottomleftPath.copy(using: &reflectOnY)!let topleftPathNode = SKShapeNode(path: topleftPath)topleftPathNode.fillcolor = SKcolor.bluetopleftPathNode.position = CGPoint(x: 320,y:240)topleftPathNode.linewidth = 0scene.addChild(topleftPathNode)sceneVIEw.presentScene(scene)PlaygroundPage.current.liveVIEw = sceneVIEwPlaygroundPage.current.needsIndefiniteExecution = truefunc arcSegment( radius: CG@R_403_5987@,halfGap))    let leftEndAngle = CG@R_403_5987@(atan2(sqrt(radius * radius - halfGap * halfGap),halfGap))    let leftEndCapPoint = CGPoint(x: radius * cos(leftEndAngle),y: radius * sin(leftEndAngle))    let rightEndCapPoint = CGPoint(x: leftEndCapPoint.y,y: leftEndCapPoint.x)    let path = CGMutablePath()    path.addArc(center: CGPoint.zero,clockwise: true)    path.addArc(center: rightEndCapPoint,startAngle : 0,endAngle : CG@[email protected],clockwise: true)    path.addArc(center: CGPoint.zero,clockwise: false)    path.addArc(center: leftEndCapPoint,startAngle : 3.0 * CG@[email protected] / 2.0,endAngle : CG@[email protected] / 2.0,clockwise: true)    path.closeSubpath()    return path}
总结

以上是内存溢出为你收集整理的swift – 如何为每个象限创建一个圆形末端的圆圈全部内容,希望文章能够帮你解决swift – 如何为每个象限创建一个圆形末端的圆圈所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1018724.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存