How can I save and load the alpha values of a UIButton in an app?(如何在应用程序中保存和加载 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 值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在应用程序中保存和加载 UIButton 的 alpha 值?


- 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01
- SetOnItemSelectedListener上的微调程序错误 2022-01-01
- Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01
- UITextView 内容插图 2022-01-01
- 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01
- 网上有没有好的 UIScrollView 教程? 2022-01-01
- 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01
- URL编码Swift iOS 2022-01-01
- 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01
- GPS状态的广播接收器? 2022-01-01