在目标C中向数组/字典添加按钮

时间:2014-04-08 11:08:36

标签: iphone objective-c arrays ios6 appdelegate

我是一个有目标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"];
}

向阵列添加按钮的一些帮助很棒

由于

2 个答案:

答案 0 :(得分: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) 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;方法中,只需重置每个按钮或缩放比例大的按钮的变换。

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

对于评论

中描述的方案

如果您只想使用一种方法/操作(如果它是根据我在评论部分中描述的方案),请检查此项。只使用Touch Up Inside附加方法然后

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

    }
}

答案 1 :(得分: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];

                     }];

}