如何在应用程序中保存和加载 UIButton 的 alpha 值? [英] How can I save and load the alpha values of a UIButton in an app?

查看:10
本文介绍了如何在应用程序中保存和加载 UIButton 的 alpha 值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如何保存 UIButton 的属性和用按钮加载?

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

I am trying to save the values of "alpha" in 6 UIButtons in my app. How can I save the states when a button is pressed and load them when another button is pressed?

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

I am fairly new to this saving and loading so what would be the easiest way of doing this?

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

Searched everywhere and nothing seems to work...

推荐答案

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

I really don't understand what you're trying to do, but here is how you would extract the alpha component of a button's background color and save it (I haven't compiled this but this should be close):

// 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:

Now you can use setFloat:forKey: to save the value to the 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

这篇关于如何在应用程序中保存和加载 UIButton 的 alpha 值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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