Objective-C UIButton网格生成 [英] Objective-C UIButton Grid Generation
问题描述
可可领主-我需要帮助!
我正在编写我的第一个应用程序(游戏),并且试图在核心框架和UIKit。我的游戏包含一个由我手动创建的UIButtons制成的5 x 8木板(!)。当我更改行为时,将更新大量的PItA。我曾经尝试过使用可变数组和UIButton生成器,但我仍然对此感到非常恐惧,而且在使事情正常进行方面一直做不到很大的努力。
该按钮存储其状态,价值,所有者和原点。我目前有A,B,C,D,E重新表示列,对行表示1-8。这些按钮为62x62,在网格的两侧具有1像素的边距,在按钮之间具有2像素的缓冲区和边距。
有人可以帮助我从一个我为每个按钮指定标签,值等...并以编程方式吐出网格的类中得到一个漂亮的小数组生成器吗? / p>
按钮示例:
-(IBAction)按钮A1:(id)发件人{
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(@从%@:\n\nSum收到的新闻是%i.\n,标题,tapSum);
[theButton setBackgroundImage:[UIImage imageNamed:@ red-dot.png] forState:UIControlStateNormal];
}
否则,如果(tapA1 == 1)
{
[theButton setAlpha:0.3];
tapA1 = 0;
tapSum--;
NSLog(@从%@:\n\nSum收到的新闻是%i.\n,标题,tapSum);
}
谢谢!
您可以像这样创建GRID:-
#define WIDTH 62
#定义高度62
#定义填充3
#定义NUMBEROFBUTTONSINAROW 8
#定义X 100
#定义Y 100
#定义总数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操作:@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中,只有稍后才能访问。在这种情况下,按钮实现将在类中。
Lords of Cocoa - I need assistance!
I'm coding up my first app (a game) and I'm trying to do the entire thing in the Core Frameworks and UIKit. My game involves a 5 x 8 board made from UIButtons that I've been creating manually (!). A massive PItA to update when I change the behaviour of one. I've tried using a mutable array and a UIButton generator but I'm terrible at this stuff still and I have been very unsuccesful in getting thing working.
The button stores it's state, a worth, an owner, and an origin. I currently have A,B,C,D,E reoresenting the columns and 1-8 for the rows. The buttons are 62x62 with a 1 pixel margin on either side of the grid and 2 pixel buffer and margin between the buttons.
Can someone help me get a nice little array generator from a class where I specify the tags, values etc... for each button and spit out the grid programatically?
Button Sample:
-(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);
}
Thanks in advance!
Well You Can Create a GRID Like this:-
#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];
}
and finally the button action :-
-(void)btnTapped:(id)sender
{
NSLog(@"Button Clicked %d",((UIButton *)sender).tag);
}
you can access the button by tag value . The array is not required. WELL BUT if you like , access it by array.
Alternatively You can create a class associating all the button customizations with it and store the class object in Array , only to access it later on.In , this case the button implementation will be in the class.
这篇关于Objective-C UIButton网格生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!