ios - 使用 Xcode 场景编辑器的布局约束来布局类似于 Storyboard 的 Sprite ?

标签 ios xcode sprite-kit autolayout skscene

Xcode 场景编辑器似乎没有提供像 Storyboard 那样定义布局约束的方法,以便您可以使用场景编辑器直观地放置和组织静态 Sprite (类似于您如何使用 Storyboard 排列 UIButton)。

有人可以确认这些事情在场景编辑器中是不可能的:

1) 相对于其他 Sprite 和场景使用约束定位 Sprite (即, Sprite A 相对于 Sprite B 居中)?

2) 假设#1 是可能的,然后根据设备调整布局约束和 Sprite 大小?

最佳答案

Sprite-kit 中没有类似于 UIKit 约束的布局约束,例如您标记自动布局和 所有元素被重新定位。

在这个地方,它可能没有意义,因为你可以有长长的背景,人物不断改变他们的大小,消失或重新出现的物体,以及物理定律涉及的其他元素。
正如 Knigh0fDragon 所评论的,这就是原因 SKSceneScaleMode存在

The modes that determine how the scene’s area is mapped to the view that presents it


enum SKSceneScaleMode : Int {
    case Fill
    case AspectFill
    case AspectFit
    case ResizeFill
}

但是在 Sprite-kit 中有 SKConstraints :

An SKConstraint object describes a mathematical constraint on a node’s position or orientation. Constraints are attached to nodes; after a scene processes any actions and physics interactions, it applies constraints attached to nodes in its node tree. Use constraints to ensure that certain relationships are true before a scene is rendered



因此,换句话说,SKConstraint用于限制特定节点的位置和方向,例如,当您希望玩家停留或跑到特定位置并且您有一些玩家必须遵循的引用点/元素时,可以使用它。

一个例子:
let range = SKRange(lowerLimit:50, upperLimit:150)
let leftConstraint = SKConstraint.distance(range, toNode:guardrail)
car.constraints = [leftConstraint]

关于ios - 使用 Xcode 场景编辑器的布局约束来布局类似于 Storyboard 的 Sprite ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38536752/

相关文章:

java - 如何在 Swift 中将 Int 位模式转换为 Float?

ios - 使用 iOS 和 Swift,如何按自然顺序获取播放列表中的歌曲列表

ios - 将 UIPageViewController 添加到 View Controller 的容器 View

ios - UILabel 不应用来自 RTF 的属性文本样式

xcode - 如何使用 osascript 在 SwiftUI 中以管理员权限运行 shell 脚本

ios - 转场后出现的场景

ios - 如何将 SKNode 渲染到 UIImage

ios - 在不使用 SKShapeNode(IOS 7 兼容)的情况下在 swift sprite 工具包中绘制圆圈的最佳方法?

ios - 试图从 NSUserDefaults 解包 double

iphone - 将 UIViewController 代码转换为 UIView