繁体   English   中英

Objective-C UIButton网格生成

[英]Objective-C UIButton Grid Generation

可可领主 - 我需要帮助!

我正在编写我的第一个应用程序(游戏),我正在尝试在Core Frameworks和UIKit中完成所有工作。 我的游戏涉及一个由UIButtons制作的5 x 8板,我一直在手动创建(!)。 当我改变一个的行为时,要更新的大量PItA。 我已经尝试过使用一个可变数组和一个UIButton生成器但是我对这个东西很糟糕,而且我在使用它的过程中非常不成功。

该按钮存储它的状态,价值,所有者和来源。 我目前有A,B,C,D,E reoresenting列和1-8行。 按钮为62x62,网格两侧有1个像素边距,按钮之间有2个像素缓冲区和边距。

有人可以帮助我从类中获取一个漂亮的小数组生成器,我为每个按钮指定标签,值等...并以编程方式吐出网格?

按钮示例:

    -(IBAction) buttonA1:(id)sender {
UIButton *theButton = (UIButton *)sender;
theButton.titleLabel.font = [UIFont fontWithName:@"Gotham Rounded" size:22];
NSString *Title = theButton.titleLabel.text;

if (tapA1 != 1) {

    [theButton  setAlpha:1.0];

    tapSum++;
    tapA1 = 1;

    [theButton setTitle: @"A1" forState: UIControlStateNormal];
    NSLog(@"Press recieved from %@:\n\nSum is %i.\n", Title, tapSum);
    [theButton setBackgroundImage:[UIImage imageNamed:@"red-dot.png"] forState:UIControlStateNormal];



}
else if (tapA1 == 1)
{
    [theButton  setAlpha:0.3];

    tapA1 = 0;
    tapSum--;

    NSLog(@"Press recieved from %@:\n\nSum is %i.\n", Title, tapSum);
}

提前致谢!

那你可以创建一个GRID像这样: -

#define WIDTH 62
#define HEIGHT 62
#define PADDING 3
#define NUMBEROFBUTTONSINAROW 8
#define X 100
#define Y 100
#define TOTALBUTTONS 40

NSMutableArray *array = [[NSMutableArray alloc] init];

    for(int i=0 ; i<TOTALBUTTONS;i++)
    {
        UIButton *btnClick = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnClick setImage:[UIImage imageNamed:@"ship.jpg"] forState:UIControlStateNormal];
        [btnClick setFrame:CGRectMake(X+((WIDTH + PADDING) * (i%NUMBEROFBUTTONSINAROW)), Y + (HEIGHT + PADDING)*(i/NUMBEROFBUTTONSINAROW), WIDTH, HEIGHT)];
        [btnClick addTarget:self action:@selector(btnTapped:) forControlEvents:UIControlEventTouchUpInside];
        btnClick.tag=i + 1;
        [self.view addSubview:btnClick];

        [array addObject:btnClick];
    }

最后按钮动作: -

-(void)btnTapped:(id)sender
{

    NSLog(@"Button Clicked %d",((UIButton *)sender).tag);
}

您可以按标签值访问按钮。 该阵列不是必需的。 如果你愿意,可以通过数组访问它。

或者,您可以创建一个类,将所有按钮自定义与其关联,并将类对象存储在Array中,以便稍后访问它。在这种情况下,按钮实现将在类中。

好的,对于这个解决方案,我假设你有一个“TapxY”BOOL变量和每个按钮的“buttonxY:”选择器,其中x:[A,B,C,D,E]和Y:[1-8]。

您想要做的是使每个按钮的按钮标签为1000 + n(1000只是为了使标签永远不会为0.只要您跟踪它就可以是任何东西。)

这样标签将是1000,1001,...,1000 + col * row-1(在5x8中将是1039)。

我也假设tapxY(ex tapA1)只会是0或1.你想要做的是做一个布尔的NSMutableArray(可以存储为[NSNumber numberWithBool:boolVal]),它有col * row( 40)所有元素都以[NSNumber numberWithBool:NO]开头;

在你的buttonAction:(id)发送方法你可以做到:

UIButton *theButton = (UIButton *)sender;
int buttonNum = theButton.tag-1000;
BOOL buttonTapped = [[tapped objectAtIndex:buttonNum] boolValue];
if (buttonTapped) { //Assuming tapped is your array of tapped buttons.
    tapSum++;
    //Set this button up however you want for the tapped state.
}
else {
    tapSum--;
    //Set this button up as not tapped
}
[tapped replaceObjectAtIndex:buttonNum withObject [NSNumber numberWithBool:!buttonTapped]]; //set this button as the opposite of what it just was.

然后,当您创建按钮时,您只需执行以下操作:

int rows = 5;
int columns = 8;
for (int n=0;n<rows;n++) {
    for (int m=0;m<columns;m++) {
        CGRect frame = CGRectMake(1+64*n,1+64*m,62,62);
        Button *button = [[UIButton alloc] initWithFrame:frame]; // or create a rounded rect and set its frame.
        [button setTag:1000+n*columns+m]; //sets tag to a number 1000 to rows*columns-1
        //Then do other setup for the button.
        [button addTarget:self forSelector:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
    }
}

注意:不要完全复制此代码,因为可能存在一些拼写错误并且未经测试

你是说这个吗?

#define kButtonSize 62
#define kMaxRows 6
#define kMaxColumns 4
#define kButtonSpacingWidth (([[UIScreen mainScreen].bounds.size.width - kMaxColumns * kButtonSize) / (kMaxColumns + 1))
#define kButtonSpacingHeight (([[UIScreen mainScreen].bounds.size.height - kMaxRows * kButtonSize) / (kMaxRows + 1))

- (UIButton *)buttonAtRow:(int)r column:(int)c
{
    CGRect frm = CGRectMake(
        kButtonSpacingWidth + c * (kButtonSize + kButtonSpacingWidth),
        kButtonSpacingHeight + r * (kButtonSize + kButtonSpacingHeight),
        kButtonSize,
        kButtonSize
    );

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.titleLabel.font = [UIFont fontWithName:@"FooBar-Bold" size:42.0];
    btn.frame = frm;
    [btn setBackgroundImage:[UIImage imageNamed:@"Baz.jpg"] forState:UIControlStateNormal];

    return btn;
}

想象一下如何放置按钮,应用小学数学,如减法和除法,其余的都是微不足道的。

为了拯救循环! 你可以制作一个具有一点视觉空间思维的网格:

-(void)generateSomeButtons {
    int row = 0;
    int col = 0;
    for (int tag = 0; tag < 40; tag++)
        UIButton *btn = //...
        btn.tag = tag;
        int buf = 1;
        //no division by 0 
        if (tag == 0) {
            buf = 2;
            row = 0; 
        }
        //Reset when it moves to the sixth element
        else if (tag % 6 == 0) {
            row = 0;
            col++;
        } 
        //Else buffer by 2 pixels
        else {
            buf = 2;
        }
        btn.frame = CGRectMake(buf + (row * 62), (col*62) + 2, 62, 62);
        [self.view addSubview:btn];
        row++;
    }
}

这只是基本的数学。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM