【问题标题】:Adding buttons to an array / dictionary in objective C将按钮添加到目标 C 中的数组/字典
【发布时间】:2014-05-21 00:54:33
【问题描述】:

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

向数组添加按钮的一些帮助会很棒

谢谢

【问题讨论】:

  • 旁注:您不能在按钮的“突出显示”状态下使用 KVO。子类化按钮并覆盖setHighlighted:
  • 回滚了,请不要从问题中删除文本,所以它没有意义。如果您不希望人们看到您的代码,请不要费心提出问题。
  • 我已经回滚了一次,请停止将您的问题更改为没有意义,就像我已经说过,如果您不希望人们知道您的代码,请不要提问,它真的就是这么简单。
  • @Popeye 我只是在整理代码,所以它的代码更少,但我的问题以更容易阅读的方式表达出来,这到底有什么问题?
  • 显然你没有这样做,虽然检查了编辑历史,但你已经从根本上改变了问题,因为对于已经解决问题的答案没有任何意义,所以反过来那些不再解决原来的问题问的问题。

标签: iphone objective-c arrays ios6 appdelegate


【解决方案1】:

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

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

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

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

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

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

    }
}

【讨论】:

  • 请不要将 cmets 用于扩展调试会话。如果您想互相联系并以这种方式交流,那很好——但这不是他们的用途。
  • 我正要检查一下。昨天没能查到,抱歉。
【解决方案2】:

请尝试此代码。不需要您创建的 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];

                     }];

}

【讨论】:

  • 添加另一个答案的目的是什么????您已经提供并回答了,请使用Edit 功能-1 更新那个
  • @Popeye 这个答案基于一个非常不同的策略,所以为了避免混淆,我添加了一个新答案。如果您查看前面的答案,它已经有很多代码。因此,为简单起见,我添加了一个新答案。我认为没有任何规则可以阻止我这样做,否则 stackoverflow 会将其作为政策落实到位。
  • 这不是你应该添加到现有答案的方式,这一切看起来像是你试图获得额外的分数 -1 仍然
  • 想什么就想什么。你不应该判断对方在想什么。我不在乎-1,将其设为-1000,我仍然不在乎。但不要对某人的想法做出判断。
  • 这不是判断,而是建议您做错事。有一个编辑按钮是有原因的,你应该使用它而不是提供另一个答案。如果您想要我的判断,这不会增加您的其他答案,而且似乎毫无意义。该答案无法解释自己应该提供哪些答案,因此,如果您要提供另一个答案,那么您至少可以提供比您的第一个答案更好的答案。所以再一次,这个答案似乎毫无意义,顺便说一下,上下票和 cmets 在这里让我们通过判断。如果您不喜欢它,请不要回答或询问
猜你喜欢
  • 1970-01-01
  • 2012-03-20
  • 1970-01-01
  • 1970-01-01
  • 2020-10-05
  • 1970-01-01
  • 1970-01-01
  • 2011-09-11
  • 1970-01-01
相关资源
最近更新 更多