ios - 在两个对象完成操作后,如何确定指示器显示哪个部分?

标签 ios swift graphics sprite-kit

我有一个如下所示的轮子 Sprite 节点,它可以以不同的速度、方向和角度旋转。我还有一个指示器,它可以旋转不同的功能(就像我上面引用的轮子一样)首先点击一个按钮后,我删除了指示器的 UIBezierPath 跟随操作,同时从我的轮子上删除了所有操作。

这是我的问题。一切都停止后。我如何确定/处理该指标指向的颜色?

我无法创建关系指示器的 zRotation 和 wheel 的 zRotation。我可以从第一状态获得旋转角度吗?但如果我继续第一个状态将会改变。

如果有人解释一下这个案例,那就太好了。

enter image description here

Update for indicator returning:

func moveClockWise() {

    let dx = person.position.x - self.size.width / 2
    let dy = person.position.y - self.size.height / 2

    let rad = atan2(dy, dx)

    path = UIBezierPath(arcCenter: CGPoint(x: self.size.width / 2, y: self.size.height / 2), radius: 294, startAngle: rad, endAngle: rad + CGFloat(M_PI * 4), clockwise: true)
    let follow = SKAction.follow(path.cgPath, asOffset: false, orientToPath: true, speed: 500)

    person.run(SKAction.repeatForever(follow).reversed(), withKey: "personFollow")


}

Update indicatorAngle return these values (independent from direction)

enter image description here

 let dx = person.position.x - self.size.width / 2
 let dy = person.position.y - self.size.height / 2

 let rad = atan2(dy, dx)
 var indicatorAngle = (rad * 180 / .pi).truncatingRemainder(dividingBy: 360)

wheelAngle returns gradually increase in positive or negative (depends on wheel's rotate direction)

 var wheelAngle = (wheel.zRotation * 180 / .pi).truncatingRemainder(dividingBy: 360)

最佳答案

@Whirlwind所述,您只需将每个角度映射到一种颜色即可。

func colorName(byRotationOf wheel: SKSpriteNode) -> String {
    let degree = wheel.zRotation * 180 / .pi
    switch degree {
    case 0..<45: return "Dark Green"
    case 45..<90: return "Dark Blue"
    case 90..<135: return "Purple"
    case 135..<180: return "Bordeaux"
    case 180..<225: return "Red"
    case 225..<270: return "Orange"
    case 270..<315: return "Green"
    case 315..<360: return "Blue"
    default: fatalError()
    }
}

更新

如果指示器也移动,您将需要获得车轮角度和指示器角度的增量。

这段代码应该可以完成工作

func colorName(by wheel: SKSpriteNode, indicatorRotation: CGFloat) -> String {
    let wheelAngle = (wheel.zRotation * 180 / .pi).truncatingRemainder(dividingBy: 360)
    let indicatorAngle = (indicatorRotation * 180 / .pi).truncatingRemainder(dividingBy: 360)
    let deltaAngle = indicatorAngle - wheelAngle

    let slice = Int(deltaAngle / 45)
    let colorNames = ["Dark Green", "Dark Blue", "Purple", "Bordeaux", "Red", "Orange", "Green", "Blue"]
    return colorNames[slice]
}

关于ios - 在两个对象完成操作后,如何确定指示器显示哪个部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41058459/

相关文章:

ios - NSMutableArray 到 UITableView

ios - iPhone 未从 Apple Watch 接收应用程序上下文

iOS - 在运行 iOS7 的旧设备上模糊和错误的视网膜图形

ios - 使用 UIProgressBar 显示下载状态

ios - Swift如何使导航中间部分图像和文本彼此相邻

java - 如何在重绘时保存先前绘制到 Canvas 上的对象?

graphics - 在 Mathematica 中排列和显示网格的各个部分

ios - 快速访问不同 View Controller 之间的 UI 属性

ios - 实现 tableView :heightForFooterInSection: 时,UITableView 的页脚未随表格一起滚动

c++ - 如何绘制变亮的边框(外发光效果)?