Iphone 在objective C中向数组/字典添加按钮

Iphone 在objective C中向数组/字典添加按钮,iphone,objective-c,arrays,ios6,appdelegate,Iphone,Objective C,Arrays,Ios6,Appdelegate,我是一个完全没有目标C的人,所以任何帮助/解释都将不胜感激 我正在做一个鼓的应用程序来找点乐趣。我为鼓的每一部分都设置了一个按钮,当按钮被点击时,它会被设置成动画来生长。我在故事板上创建了按钮,而不是代码 我已经有了要设置动画的按钮,但我不想为鼓套件的每个元素重复代码,但是我在为数组指定按钮、确定按下的按钮(带有标记??)以及使该按钮设置动画时遇到了一些问题 以下是我的一些代码以提供帮助: @interface mainViewController : UIViewController //se

我是一个完全没有目标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"];
}
在向数组添加按钮方面提供一些帮助将非常有用


谢谢

使用IBOutletCollection通过界面(故事板)链接所有按钮

然后将iAction方法附加到所有按钮。我认为您需要两种方法(一种用于突出显示状态,另一种用于正常状态)。您可以从列表中选择适当的方法操作

适当地标记每个按钮。然后根据标记将所有变换对象添加到数组中

 //add according to tag
self.transformArray = [NSArray arrayWithObjects:NSStringFromCGAffineTransform(self.snareButtonTransform), 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];

    }
}
然后在
-(iAction)按钮正常状态中:(id)发送方
method只需为每个按钮或规模较大的按钮重置变换

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

                         }];

    }
}
更新

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

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

    }
}

请尝试此代码。不需要您创建的transformArray或Collection Outlet或transform变量。请检查它是否为您提供了所需的结果

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

                     }];

}

旁注:不能在按钮的“突出显示”状态下使用KVO。将按钮子类化并覆盖
setHighlighted:
。回滚后,请不要删除问题中的文本,这样就没有意义了。如果你不想让别人看到你的代码,不要费心问问题。我已经回退过一次,请停止将你的问题更改为没有意义,就像我说过的,如果你不想让别人知道你的代码,不要问问题,真的很简单。@Popeye我只是整理代码,这样代码就少了,但我的问题更容易理解,这到底是怎么回事?显然你没有这么做,尽管你检查了编辑历史记录,但你已经彻底改变了问题,对解决问题的答案没有任何意义,因此这些答案也不再解决最初提出的问题。请不要在延长的调试会话中使用注释。如果你想通过这种方式相互联系和沟通,那很好——但这不是他们的用途。我正要检查一下。昨天无法查看,很抱歉。添加其他答案的目的是什么????您已经提供了答案,请使用
Edit
功能更新该答案-1@Popeye这个答案基于一个非常不同的策略,所以为了避免混淆,我添加了一个新的答案。如果你看前面的答案,它已经有很多代码了。因此,为了简单起见,我添加了一个新的答案。我认为没有任何规则可以阻止我这样做,否则stackoverflow会将其作为一项政策。这不是你应该在现有答案中添加的方式,所有这些看起来都是你试图获得额外的分数-1保留你想要的想法。你不应该对别人的想法做出判断。我不在乎-1,让它成为-1000,我仍然不在乎。但不要对别人的想法做出判断,这不是判断,而是建议你做错事。有一个编辑按钮的原因,你应该使用它,而不是提供另一个答案。如果你想要我的判断,这不会增加你的另一个答案,而且似乎毫无意义。这个答案无法解释自己应该给出哪些答案,因此,如果你打算提供另一个答案,那么你至少可以提供一个更好的答案,而不是第一个答案。因此,这个答案似乎毫无意义,顺便说一句,上下的投票和评论都在这里等待我们作出判断。如果你不喜欢,不要回答或问