ios - iOS Swift 中的 CALayer 或 UIView?

标签 ios uiview swift

任何人都可以向我指出一个教程或示例代码(或两者!)来解释为 iOS 添加 View 和图层的正确方法。

理想情况下是 Swift,而不是 Obj C。

我有自己添加 SKNode 时有效的代码,没问题,但如果我添加渐变层 - 我希望它是背景中的天空,渐变会覆盖其他所有内容。

我尝试了各种形式的在上面的索引处添加子层等,但似乎没有任何效果。我想这就是我对CALayer和UIView的基本理解。我找不到好的教程:(

任何帮助表示赞赏。 干杯 亚当。

这是一个示例。
View 填充了渐变,这很棒,但是按钮似乎隐藏在下面,尽管试图将其强制置于背景层之上。

import QuartzCore
class NightScene: SKScene, SKPhysicsContactDelegate {

let playButton = SKSpriteNode(imageNamed: "play")


let gradient = CAGradientLayer()
    gradient.frame = view.bounds
    let colorTop = UIColor(red: 0.0, green: 0.0, blue: 0.100, alpha: 1.0).CGColor
    let colorMid = UIColor(red: 0.0, green: 0.0, blue: 0.450, alpha: 1.0).CGColor
    let colorBottom = UIColor(red: 0.0, green: 0.0, blue: 0.260, alpha: 1.0).CGColor
    let arrayColors: Array <AnyObject> = [colorTop, colorMid, colorBottom]
    gradient.colors = arrayColors
    gradient.locations = [ 0.0, 0.4, 1.0]
    self.view.layer.insertSublayer(gradient, atIndex:0)


    let sceneLayer = CALayer()
    sceneLayer.frame = view.bounds
    self.view.layer.insertSublayer(sceneLayer, above: gradient)

    self.playButton.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidX(self.frame)*2.5)
    self.addChild(playButton)

}

最佳答案

即使这个问题已经存在一年多了,我也会在自己无数个小时的搜索后提供答案。我发现了许多类似的问题,但没有/模糊的答复。

我发现节点不像渐变那样有层支持。这会导致渐变占据整个 UI View 。即使设置了 zLocation,节点也不可见。这适用于基于 SpriteKit 的项目。

我偶然发现了一个可以用来创建渐变背景的库。该库基本上将渐变转换为 SKSpriteNode。 渐变 -> 纹理 -> 节点

https://www.github.com/braindrizzlestudio/BDGradientNode

来自开发者

We wrote BDGradientNode because gradients are a bit of a pain to set up on the fly in SpriteKit. UIViews are CALayer-backed and have access to CAGradientLayer, making them simple--fairly simple--to make. (Though sweep gradients can't be done.) SKNodes are NOT layer-backed. While you can jump through some hoops to make images, and so textures, from layers that can be assigned to an SKSpriteNode (which is what we first did when we just needed a simple gradient in SpriteKit) it's both cumbersome and inflexible.

简单的线性背景

将库添加到您的 Xcode 项目后,您可以使用以下内容创建线性渐变背景:

    let color1 = UIColor(red: 0/255, green: 118/255, blue: 162/255, alpha: 1.0)
    let color2 = UIColor(red: 0/255, green: 85/255, blue: 116/255, alpha: 1.0)
    let colors = [color1, color2]

    let blending : Float = 1.0

    let startPoint = CGPoint(x: 0.5, y: 1.0)
    let endPoint = CGPoint(x: 0.5, y: 0.0)

    let nodeSize = CGSize(width: self.frame.width, height: self.size.height)

    let texture = SKTexture(imageNamed: "dummypixel")

    let myGradientNode = BDGradientNode(linearGradientWithTexture: texture, colors: colors, locations: nil, startPoint: startPoint, endPoint: endPoint, blending: blending, keepTextureShape: false, size: nodeSize)

    myGradientNode.zPosition = 0
    myGradientNode.position = CGPointMake(self.size.width/2, self.size.height/2)

关于ios - iOS Swift 中的 CALayer 或 UIView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24761290/

相关文章:

ios - 是否可以在不同的应用程序之间共享一个 APNS 推送证书?

ios - 在 iOS 上,实现充满小部件的滑出式面板的好方法是什么?

iphone - UIPanGestureRecognizer 不适用于 iOS5

ios - 如何在特定时间自动删除iOS通知中心的推送通知?

ios - iOS10使用Xcode8时AutoLayout无法正确显示

移动设备上的 iOS 无法正确显示 CSS 1px 边框

ios - 使用 SwiftUI 从结构中的完成变量调用方法

iphone - iOS:如何从它的 subview 中删除覆盖?

SwiftUI macOS View 在显示多个图表时开始滞后

ios - 向 Pod 添加静态框架。