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


基础教程推荐
- iOS4 创建后台定时器 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01