ios Swift 中的 colorWithAlphaComponent 示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24334653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:01:33  来源:igfitidea点击:

colorWithAlphaComponent example in Swift

iosswiftalphauicolor

提问by kmiklas

What is the correct syntax for this function in Swift?

Swift 中此函数的正确语法是什么?

The following works fine, and colors the background purple:

以下工作正常,并将背景颜色为紫色:

 self.view.backgroundColor = UIColor.purpleColor()

When I chain the colorWithAlphaComponent function, the view shows the correct alpha for a moment, and then changes to an opaque purple that is relatively dark:

当我链接 colorWithAlphaComponent 函数时,视图会显示正确的 alpha 片刻,然后变为相对较暗的不透明紫色:

 self.view.backgroundColor = UIColor.purpleColor().colorWithAlphaComponent(0.5)

Is this the recommended function for adding an alpha value to a UIColor?

这是向 UIColor 添加 alpha 值的推荐函数吗?

Furthermore, why does the intellisense popup say that this function expects a UIColor as a parameter? E.g.,

此外,为什么智能感知弹出窗口说这个函数需要一个 UIColor 作为参数?例如,

   self.view.backgroundColor = UIColor.colorWithAlphaComponent(<#UIColor#>)

EDIT: The behavior is strange. I am setting the background color on a view controller that is being loaded in a modal. As the modal slides up from the bottom, the alpha is correct. When the modal finishes loading, the background color changes to opaque?!

编辑:行为很奇怪。我正在以模式加载的视图控制器上设置背景颜色。当模态从底部向上滑动时,alpha 是正确的。当模态完成加载时,背景颜色变为不透明?!

EDIT 2: The problem was not with the code--both the code above and the suggestion below were properly applying the alpha. The issue is the way that modals are being presented--the underlying view is being removed. See:
Transparent Modal View on Navigation Controller

编辑 2:问题不在于代码——上面的代码和下面的建议都正确地应用了 alpha。问题在于模态的呈现方式——底层视图正在被移除。请参阅:
导航控制器上的透明模态视图

回答by Mick MacCallum

It's not strange, it's behaving exactly as it should. Although many of UIColor's methods are class methods, there are still a few instance methods, and this is one of them. From the UIColor documentation.

这并不奇怪,它的行为完全符合它应有的要求。UIColor的方法虽然很多都是类方法,但还是有一些实例方法,这就是其中之一。来自UIColor 文档

colorWithAlphaComponent:

Creates and returns a color object that has the same color space and component values as the receiver, but has the specified alpha component.

colorWithAlphaComponent:

创建并返回一个颜色对象,该对象具有与接收器相同的颜色空间和分量值,但具有指定的 alpha 分量。

So, colorWithAlphaComponent: just changes the alpha value of its receiver. Example:

所以, colorWithAlphaComponent: 只是改变其接收器的 alpha 值。例子:

let purple = UIColor.purpleColor() // 1.0 alpha
let semi = purple.colorWithAlphaComponent(0.5) // 0.5 alpha

And the reason why you're seeing autocompletion for this instance method on the type, is because Swift allows you to use instance methods as curried type methods. In the example you provided, colorWithAlphaComponentactually returns a function that takes a CGFloatas input and returns a UIColor.

并且您在类型上看到此实例方法的自动完成的原因是因为 Swift 允许您将实例方法用作柯里化类型方法。在您提供的示例中,colorWithAlphaComponent实际上返回一个将 aCGFloat作为输入并返回 a的函数UIColor

let purple = UIColor.purpleColor()
let purpleFunc: (CGFloat -> UIColor) = UIColor.colorWithAlphaComponent(purple)

So, if you wanted to, you could call the type method passing in the instance you want to modify, and then call the resulting function with the alpha that you want to apply, like so.

因此,如果您愿意,可以调用传入要修改的实例的类型方法,然后使用要应用的 alpha 调用结果函数,如下所示。

let purple = UIColor.purpleColor()
let purpleTrans = UIColor.colorWithAlphaComponent(purple)(0.5)

Then as far as the issues you're having with the modal view controller go, you shouldn't be attempting to change the alpha of the view of a modal view controller. See this for more info. Instead, you should be manually creating a view and adding it to the view hierarchy of your existing view controller (if you absolutely have to alter its alpha)

然后,就您在模态视图控制器中遇到的问题而言,您不应该尝试更改模态视图控制器的视图的 alpha。有关更多信息,请参阅此内容。相反,您应该手动创建一个视图并将其添加到现有视图控制器的视图层次结构中(如果您绝对必须更改其 alpha)

回答by Arjun Yadav

Swift 4.0

斯威夫特 4.0

self.view.backgroundColor = UIColor.black.withAlphaComponent(0.7)

回答by Ashu

in Swift 3.0

在 Swift 3.0 中

This works for me in xcode 8.2.

这在 xcode 8.2 中对我有用。

yourView.backgroundColor = UIColor.black.withAlphaComponent(0.5)

yourView.backgroundColor = UIColor.black.withAlphaComponent(0.5)

It may helps you.

它可能会帮助你。

回答by JoGoFo

Since UIColoris part of UIKit, it has been replaced in SwiftUIwith Color. The equivalent method is .opacity(_ opacity: Double)for example:

由于UIColor是 UIKit 的一部分,它在SwiftUI 中已被替换为Color. 等效方法.opacity(_ opacity: Double)例如:

Color.purple.opacity(0.5)

回答by Waran-

Try smth like this to set color

像这样尝试设置颜色

 view.backgroundColor = UIColor(red: (64/255.0), green: (54/255.0), blue: (105/255.0), alpha: 1.0)