使用IBOutletCollection使用接口(故事板)
@property (nonatomic, strong) IBOutletCollection(UIButton) NSArray *allButtons;
@property (nonatomic, strong) NSArray *transformArray;
-(IBAction) buttonsHighlighted:(id)sender;
-(IBAction) buttonsNormalState:(id)sender;
然后与所有的按钮附加IBAction为方法中的所有按钮链接。我想你会需要两种方法(一种用于高亮状态,一种用于正常状态)。您可以从列表中
标签每个按钮适当地选择适当的方法操作。然后加入全部转换对象到一个数组根据标记
//add according to tag
self.transformArray = [NSArray arrayWithObjects:NSStringFromCGAffineTransform(self.snareButtonTransform), nil];
后-(IBAction) buttonsNormalState:(id)sender;
方法做这样的事情
-(IBAction) buttonsHighlighted:(id)sender
{
for(UIButton *btn in self.allButtons)
{
NSLog(@"Btn Tag: %d", btn.tag); //You can get the tag by this and do appopriate action if needed
//allow button to be pressed during animation stage
UIViewAnimationOptions options = UIViewAnimationOptionBeginFromCurrentState |
UIViewAnimationOptionAllowUserInteraction;
CGAffineTransform transform = CGAffineTransformFromString([transformArray objectAtIndex:btn.tag]);
if(btn.isHighlighted)
{ //suble growth, pulse like
float scale = 1.05;
transform = CGAffineTransformScale(transform, scale, scale);
}
else
{
//transform = self.snareButtonTransform;
}
//fast animation, represents fast drum hit
[UIView animateWithDuration:0.025
delay:0.0
options:options
animations:^{
btn.transform = transform;
}
completion:nil];
}
}
然后,只需重新转换为每个按钮或者其按钮规模很大。
-(IBAction) buttonsNormalState:(id)sender
{
for(UIButton *btn in self.allButtons)
{
NSLog(@"Btn Tag: %d", btn.tag); //You can get the tag by this and do appopriate action if needed
//allow button to be pressed during animation stage
UIViewAnimationOptions options = UIViewAnimationOptionBeginFromCurrentState |
UIViewAnimationOptionAllowUserInteraction;
CGAffineTransform transform = CGAffineTransformFromString([transformArray objectAtIndex:btn.tag]);
//fast animation, represents fast drum hit
[UIView animateWithDuration:0.025
delay:0.0
options:options
animations:^{
btn.transform = transform;
}
completion:nil];
}
}
对于方案在注释中所描述
,如果你想只使用一个方法/动作(如果它是根据我在评论部分中描述的场景)做到这一点,请检查。只有使用触摸的内心,然后再连接方法
-(IBAction) buttonsHighlighted:(id)sender
{
for(UIButton *btn in self.allButtons)
{
NSLog(@"Btn Tag: %d", btn.tag); //You can get the tag by this and do appopriate action if needed
//allow button to be pressed during animation stage
UIViewAnimationOptions options = UIViewAnimationOptionBeginFromCurrentState |
UIViewAnimationOptionAllowUserInteraction;
CGAffineTransform transform = CGAffineTransformFromString([transformArray objectAtIndex:btn.tag]);
CGAffineTransform originalTranform = transform;
if(btn.isHighlighted)
{ //suble growth, pulse like
float scale = 1.05;
transform = CGAffineTransformScale(transform, scale, scale);
}
else
{
//transform = self.snareButtonTransform;
}
//fast animation, represents fast drum hit
[UIView animateWithDuration:0.025
delay:0.0
options:options
animations:^{
btn.transform = transform;
}
completion:^(BOOL finished) {
[UIView animateWithDuration:0.025
delay:0.0
options:options
animations:^{
btn.transform = originalTranform;
}
completion:nil];
}];
}
}
更新
在viewDidLoad方法或当你已经赋值给你的变换,将它们添加到transformArray
self.transformArray = [NSArray arrayWithObjects:
NSStringFromCGAffineTransform(self.snareButtonTransform),
NSStringFromCGAffineTransform(self.snareButtonTransform2),
NSStringFromCGAffineTransform(self.snareButtonTransform3),
NSStringFromCGAffineTransform(self.snareButtonTransform4),
NSStringFromCGAffineTransform(self.snareButtonTransform5),
NSStringFromCGAffineTransform(self.snareButtonTransform6),
nil];
新代码
-(IBAction) buttonsHighlighted:(id)sender
{
for(UIButton *btn in self.allButtons)
{
NSLog(@"Btn Tag: %d", btn.tag); //You can get the tag by this and do appopriate action if needed
//allow button to be pressed during animation stage
UIViewAnimationOptions options = UIViewAnimationOptionBeginFromCurrentState |
UIViewAnimationOptionAllowUserInteraction;
CGAffineTransform transform = CGAffineTransformFromString([transformArray objectAtIndex:btn.tag]);
if(btn.isHighlighted)
{ //suble growth, pulse like
float scale = 1.05;
transform = CGAffineTransformScale(transform, scale, scale);
}
else
{
//transform = self.snareButtonTransform;
}
//fast animation, represents fast drum hit
[UIView animateWithDuration:0.025
delay:0.0
options:options
animations:^{
btn.transform = transform;
}
completion:nil];
}
}
旁注:您无法在按钮的“突出显示”状态下使用KVO。子类按钮并改写'setHighlighted:'。 –
回滚请不要从问题中删除文本,因此它没有任何意义。如果你不想让人们看到你的代码,请不要问一个问题。 – Popeye
我已经回滚了一次,请停止改变你的问题,使其没有意义,就像我已经说过的,如果你不想让人们知道你的代码不问问题,那真的很简单。 – Popeye