Swift中的colorWithAlphaComponent示例 [英] colorWithAlphaComponent example in Swift

查看:659
本文介绍了Swift中的colorWithAlphaComponent示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What is the correct syntax for this function in Swift?

以下代码可以正常工作,并将背景颜色设置为紫色:

The following works fine, and colors the background purple:

 self.view.backgroundColor = UIColor.purpleColor()

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

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:

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

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

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

此外,为什么intellisense弹出窗口说此函数需要UIColor作为参数?例如,

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

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

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

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?!

编辑2:问题不在于代码-上面的代码和下面的建议都是正确应用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

推荐答案

这并不奇怪,它的行为完全正确。尽管UIColor的许多方法都是类方法,但是仍然有一些实例方法,这就是其中之一。来自 UIColor文档

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.


colorWithAlphaComponent:

colorWithAlphaComponent:

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

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:

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

以及您在类型上看到此实例方法自动完成的原因,是因为Swift允许您将实例方法用作咖喱类型方法。在您提供的示例中, colorWithAlphaComponent 实际上返回一个函数,该函数采用 CGFloat 作为输入并返回 UIColor

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, colorWithAlphaComponent actually returns a function that takes a CGFloat as input and returns a UIColor.

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

因此,您可以调用要传入实例的type方法,然后使用要应用的alpha调用结果函数,就像这样。

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.

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

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

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)

这篇关于Swift中的colorWithAlphaComponent示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆