2014-04-08 98 views
1

我是一个带有目标C的总noob,所以任何帮助/解释都将被大量赞赏!将按钮添加到目标中的数组/字典中C

我正在制作一个有趣的鼓应用程序。我为鼓的每个部分设置了一个按钮,并且当按钮被轻敲时,它会动画成长。我在故事板上创建了按钮,而不是在代码中。

我已经有按钮来设置动画,但我不想重复鼓组的每个元素的代码,但我在将按钮分配给数组时遇到了一些麻烦,决定按下按钮(与标签??),然后使该按钮动画。

下面是我的一些代码,以帮助:

@interface mainViewController : UIViewController 
//set buttons 
@property (weak, nonatomic) IBOutlet UIButton *button1; 
@property (weak, nonatomic) IBOutlet UIButton *button2; 

- (IBAction)buttonTrigger:(UIButton *)sender; 

我不会为每一个按钮添加代码,因为它是相同的:

@interface mainViewController() 
@property (nonatomic) CGAffineTransform button1transform, button2transform;; 

@end 

@implementation mainViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [self.button1 addObserver:self forKeyPath:@"highlighted" options:0 context:0]; 
    self.button1transform = self.button1.transform; 

    [self.button1 addObserver:self forKeyPath:@"highlighted" options:0 context:0]; 
    self.button2transform = self.button2.transform; 
} 

//动画:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 
{ 
    if (object == self.button1) 
    { 

     CGAffineTransform transform; 

     if (self.button1.isHighlighted) 
     { 
      float scale = 2.0; 
      transform = CGAffineTransformScale(self.button1transform, scale, scale); 
     } 
     else 
     { 
      transform = self.button1transform; 
     } 

     [UIView animateWithDuration:0.5 
           delay:0.5 
          options:options 
         animations:^{ 
      self.button1.transform = transform; 
     } 
         completion:nil]; 
    } 

    //this code is repeated for the other 6 buttons 
} 


- (void)dealloc 
{ 
    [self.button1 removeObserver:self forKeyPath:@"highlighted"]; 

    [self.button2 removeObserver:self forKeyPath:@"highlighted"]; 
} 

有些帮助添加按钮阵列会很好

由于

+0

旁注:您无法在按钮的“突出显示”状态下使用KVO。子类按钮并改写'setHighlighted:'。 –

+0

回滚请不要从问题中删除文本,因此它没有任何意义。如果你不想让人们看到你的代码,请不要问一个问题。 – Popeye

+0

我已经回滚了一次,请停止改变你的问题,使其没有意义,就像我已经说过的,如果你不想让人们知道你的代码不问问题,那真的很简单。 – Popeye

回答

1

使用IBOutletCollection使用接口(故事板)

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

-(IBAction) buttonsHighlighted:(id)sender; 
-(IBAction) buttonsNormalState:(id)sender; 

enter image description here

enter image description here

然后与所有的按钮附加IBAction为方法中的所有按钮链接。我想你会需要两种方法(一种用于高亮状态,一种用于正常状态)。您可以从列表中

enter image description here

标签每个按钮适当地选择适当的方法操作。然后加入全部转换对象到一个数组根据标记

//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]; 

    } 
} 
+0

请不要使用注释进行扩展调试会话。如果你想互相联系并以这种方式进行交流,那就太好了 - 但这不是他们所用的。 –

+0

好的,谢谢。我会检查并通过电子邮件回复您。 –

+0

我正要检查它。昨天无法检查,对此抱歉。 –

0

请尝试此代码。不需要您创建的transformArray或Collection出口或变换变量。请检查它给你想要的结果。

-(IBAction) buttonsHighlighted:(id)sender 
{ 
    UIButton *btn = (UIButton *)sender; 

    NSLog(@"Btn Tag: %d", btn.tag); 

    UIViewAnimationOptions options = UIViewAnimationOptionBeginFromCurrentState | 
    UIViewAnimationOptionAllowUserInteraction; 


    CGAffineTransform transform = btn.transform; 

    float scale = 1.0; 

    if(btn.isHighlighted) 
    { 
     //suble growth, pulse like 
     scale = 1.05; 
    } 

    transform = CGAffineTransformScale(transform, scale, scale); 

    //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 = CGAffineTransformScale(transform, 1.0, 1.0); 
              } 
              completion:nil]; 

        }]; 

} 
+1

添加另一个答案的目的是什么?您已经提供并回答,只需使用'编辑'功能更新该答案-1 – Popeye

+0

@Popeye此答案基于一种非常不同的策略,为避免混淆,我添加了一个新答案。如果你看一下以前的答案,它已经有很多代码。所以,我为简单添加了一个新的答案。我认为没有任何规则阻止我这样做,否则,stackoverflow会将其作为一项政策。 –

+0

这不是这样的工作方式,你应该只是添加到你现有的答案,所有这些看起来像是你想获得更多的积分-1仍然 – Popeye

相关问题