iOS:一次设置多个按钮的Alpha [英] iOS: setting alphas of multiple buttons at once

查看:69
本文介绍了iOS:一次设置多个按钮的Alpha的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我真的不知道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中,您可以将各个按钮连接到插座集合。有关 IBOutletCollection 的讨论,请参阅NSHipster的 IBAction /

如果您不使用Interface Builder,而是以编程方式创建了这些按钮,则只需 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;
}

或更简单的是,您可以使用<$ c $更新所有这些c> setValue:forKey::

  [self.buttons setValue:@ 1 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?

[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;
        }
 ];

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

@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *buttons;

Clearly, in Interface Builder you would connect the individual buttons to the outlet collection. For a discussion of IBOutletCollection, see NSHipster's IBAction / IBOutlet / IBOutlet​Collection article.

If you're not using Interface Builder, but rather have created these buttons programmatically, you could just have an NSArray:

@property (strong, nonatomic) NSArray *buttons;

Which you'd then populate yourself:

self.buttons = @[self.button1, self.button2, self.button3, self.button4, self.button5, self.button6];

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:

for (UIButton *button in self.buttons) {
    button.alpha = 1;
}

Or, even simpler, you can update all of them with setValue:forKey::

[self.buttons setValue:@1 forKey:@"alpha"];

这篇关于iOS:一次设置多个按钮的Alpha的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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