iOS:一次设置多个按钮的Alpha [英] iOS: setting alphas of multiple buttons at once
问题描述
好的,所以我真的不知道NSDictionary是否是正确的方法,但是我正在使用淡入淡出动画来在屏幕上和屏幕下使用不同的按钮组。可以这么说,我想将它们分隔为不同的组,这样我可以一两行来完成此操作,而不必每次都重写所有按钮名称(数量很多)。有关如何执行此操作的任何想法?
[UIView animateWithDuration:1.5
动画:^(void)
{
//作为示例,我仅将它们称为button1,button2等。
self.button1.alpha = 1;
self.button2.alpha = 1;
self.button3.alpha = 1;
self.button4.alpha = 1;
self.button5.alpha = 1;
}
];
我将以上内容作为我所要求的示例。有没有一种方法可以将所有按钮放入NSDictionary中并编写一行(按钮当前位于的位置)以更改其所有Alpha?
如今,在Interface Builder中添加按钮的首选机制是 IBOutletCollection
:
@property(强,非原子)IBOutletCollection(UIButton)NSArray * buttons;
很显然,在Interface Builder中,您可以将各个按钮连接到插座集合。有关 如果您不使用Interface Builder,而是以编程方式创建了这些按钮,则只需 然后您要填充自己: 无论您如何构建此数组(出口集合或手动填充),都可以循环访问当您更新按钮的属性时,该数组: 或更简单的是,您可以使用<$ c $更新所有这些c> setValue:forKey:: Okay, so I don't really know if NSDictionary is the correct approach to this, but I am using a fade animation to take different groups of buttons on and off a screen. I want to "compartmentalize" them, so to speak, into different groups so I can do this in one or two lines without having to rewrite all the buttons names every time (there are a good amount). Any ideas on how to do this? I included the above as an example of what I am asking. Is there a way to put all the buttons in an NSDictionary and write one line (where the buttons are currently located) that changes all of their alphas? The preferred mechanism nowadays for buttons you've added in Interface Builder would be an Clearly, in Interface Builder you would connect the individual buttons to the outlet collection. For a discussion of If you're not using Interface Builder, but rather have created these buttons programmatically, you could just have an Which you'd then populate yourself: Regardless of how you build this array (either outlet collection or manually populated), you can then iterate through that array as you update a property of the buttons: Or, even simpler, you can update all of them with
这篇关于iOS:一次设置多个按钮的Alpha的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! IBOutletCollection
的讨论,请参阅NSHipster的 IBAction /
NSArray
:
@property(strong,nonatomic)NSArray *按钮;
self.buttons = @ [self.button1,self.button2,self.button3,self.button4,self.button5,self.button6];
for(UIButton * self.buttons中的按钮){
button.alpha = 1;
}
[self.buttons setValue:@ 1 forKey:@ α];
[UIView animateWithDuration: 1.5
animations: ^(void)
{
//As an example i just called them button1, button2, etc.
self.button1.alpha = 1;
self.button2.alpha = 1;
self.button3.alpha = 1;
self.button4.alpha = 1;
self.button5.alpha = 1;
}
];
IBOutletCollection
:@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *buttons;
IBOutletCollection
, see NSHipster's IBAction / IBOutlet / IBOutletCollection article.NSArray
:@property (strong, nonatomic) NSArray *buttons;
self.buttons = @[self.button1, self.button2, self.button3, self.button4, self.button5, self.button6];
for (UIButton *button in self.buttons) {
button.alpha = 1;
}
setValue:forKey:
:[self.buttons setValue:@1 forKey:@"alpha"];