如何在应用程序中保存和加载 UIButton 的 alpha 值? [复制]

Posted

技术标签:

【中文标题】如何在应用程序中保存和加载 UIButton 的 alpha 值? [复制]【英文标题】:How can I save and load the alpha values of a UIButton in an app? [duplicate] 【发布时间】:2011-09-07 15:37:04 【问题描述】:

可能重复:How would I save a UIButton's properties and load with a button?

我正在尝试将“alpha”的值保存在我的应用程序的 6 个 UIButton 中。如何在按下按钮时保存状态并在按下另一个按钮时加载它们?

我对这种保存和加载相当陌生,那么最简单的方法是什么?

到处搜索,似乎没有任何效果......

【问题讨论】:

您的意思是要保存按钮背景颜色 RGBA 值中的“alpha”(透明度)值? 您真正想要完成的是什么?只保存 alpha 而不是完整的颜色没有多大意义。你的按钮是否在透明度方面有所不同? 另外,如果您可以提供您尝试过的代码的相关摘录,并告诉我们哪些不起作用,这将有所帮助。 另一个问题已经作为这个问题的副本关闭,所以我们不妨让这个问题保持打开状态。无论如何,尝试再次投票失败,因为这将是一个循环引用。 【参考方案1】:

我真的不明白你要做什么,但这里是你如何提取按钮背景颜色的 alpha 分量并保存它(我还没有编译这个,但应该很接近):

// get the alpha value from the CGColor components from the button's backgroundColor
UIColor* color = button.backgroundColor;
const CGFloat *components = CGColorGetComponents(color.CGColor);
CGFloat alpha = components[CGColorGetNumberOfComponents(color.CGColor)-1];

现在您可以使用 setFloat:forKey: 将值保存到 NSUserDefaults:

// save alpha value (0.0=transparent to 1.0=opaque)
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setFloat:alpha forKey:@"buttonColorKey"];   // use your own key string

【讨论】:

【参考方案2】:

使用NSUserDefaults 存储和加载 alpha 值。

【讨论】:

我该怎么做?我遵循了许多指示,但没有一个真正起作用。你能提供一些示例代码吗?谢谢!

以上是关于如何在应用程序中保存和加载 UIButton 的 alpha 值? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

退出应用程序并重新加载时保存 UIButton 的状态

Xcode:检查 UIButton 是不是隐藏,使用 NSUserDefaults

复选框保存/加载NSUserdefaults

如何在uitableViewCell中保存UIButton的状态?

如何从HTTP请求将图像加载到UIButton Swift中

如何从 HTTP 请求将图像加载到 UIButton Swift