如何在应用程序中保存和加载UIButton的Alpha值? [英] How can I save and load the alpha values of a UIButton in an app?
问题描述
我试图在我的应用程序中保存6个UIButton中的alpha值。如何在按下按钮时保存状态,并在按下另一个按钮时加载状态?
我是相当新的保存和加载,所以最简单的
div>
我真的不明白你想做什么,但这里是如何提取一个按钮的背景颜色的alpha组件并保存它(我没有编译,但这应该是接近) :
//从按钮的backgroundColor中获取CGColor组件的alpha值
UIColor * color = button.backgroundColor ;
const CGFloat * components = CGColorGetComponents(color.CGColor);
CGFloat alpha = components [CGColorGetNumberOfComponents(color.CGColor)-1];
现在可以使用setFloat:forKey:将值保存到NSUserDefaults:
//保存alpha值(0.0 =透明到1.0 =不透明)
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
[defaults setFloat:alpha forKey:@buttonColorKey]; // use your own key string
Possible Duplicate:
How would I save a UIButton's properties and load with a button?
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...
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];
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屋!