如何在按钮的位置创建变量?

时间:2014-02-27 22:41:22

标签: xcode variables button applescript

我有这么多代码的游戏,但大多数代码都是一遍又一遍地重复相同的空白。

我为此目的简化了代码:

-(void)button1Action {
    if (CGRectIntersectsRect(button1.frame, image1.frame)) {
        //button 1 is used a few times between these two brackets
    }
}
-(void)button2Action {
    if (CGRectIntersectsRect(button2.frame, image1.frame)) {
        //button 2 is used a few times between these two brackets
    }
}
-(void)button3Action {
    if (CGRectIntersectsRect(button3.frame, image1.frame)) {
        //button 3 is used a few times between these two brackets
    }
}

有没有办法让我用这样的东西制作一个空白?:

-(void)buttonXAction {
    if (CGRectIntersectsRect(buttonX.frame, image1.frame)) {
        //button X is used a few times between these two brackets
    }
}

感谢!!!

1 个答案:

答案 0 :(得分:0)

创建一个这样的方法:

- (void)buttonAction:(UIButton *)sender {

        if (CGRectIntersectsRect(sender.frame, image1.frame)) {

        //This will perform the action on what ever buttons calls the method. 
}

希望这有帮助!