I am developing an iOS application. I have 35 buttons on the storyboard. Is there anyway I can use a random generator to randomly rearrange or shuffle the buttons and still maintain the button's functionality?
You can add all the button objects to an NSMutableArray called buttonsArray and then use the following loop to exchange the buttons:
for (int i = 0; i < buttonsArray.count; i++) {
int random1 = arc4random() % [buttonsArray count];
int random2 = arc4random() % [buttonsArray count];
[buttonsArray exchangeObjectAtIndex:random1 withObjectAtIndex:random2];
}
If each of the button is belongs to a class like below then its really easy to shuffle them,
MyRandomButton {
NSInteger uniqueId;
NSString *title;
....
....
}
Now, what you can do is, at initial, prepare number of objets of MyRandomButton (equals to count of buttons on your StoryBoard). And add it into a mutable array (NSMutableArray).
Like this,
for(NSInteger index = 1; index <= 35; index++) {
MyRandomButton *btnInfo = ...;
btnInfo.uniqueId = index;
btnInfo.title = #sometitle;
[myButtonsArray addObject:btnInfo];
}
In next step, if you're done with something (and you want to shuffle all buttons) then get help from this question, What's the Best Way to Shuffle an NSMutableArray? to shuffle your array.
Loop through all buttons on your StoryBoard (in your case: 1 to 35).
for(NSInteger index = 1; index <= 35; index++) {
UIButton *button = (UIButton *)[yourView viewWithTag:index];
MyRandomButton *btnInfo = [myButtonsArray objectAtIndex:(index-1)];
button.tag = btnInfo.uniqueId;
[button setTitle:btnInfo.title forState:UIControlStateNormal];
}
Inside above loop, we've updated buttons title and tag.
Done!
That is possible, it will require some coding:
Create NSMutableArray with all buttons of the view in it.
Randomize its order.
Run over the buttons array and on each iteration keep last
button view and switch its rect with current button and
vice versa.
For better understanding, here is the code (ready for use, just copy paste):
- (void)viewDidLoad {
[super viewDidLoad];
// Getting buttons into array
NSMutableArray * buttonsArr = [self getButtonsArray];
// Shuffling buttons location
if(buttonsArr && [buttonsArr count]) {
[self randomizeButtonsLocation:buttonsArr];
} else {
NSLog(#"No buttons found");
}
}
- (void)randomizeButtonsLocation:(NSMutableArray *)buttonsArr {
// Randomizing order in button array
buttonsArr = [self randomizeArr:buttonsArr];
UIButton * lastBtn = nil;
for (UIButton * btn in buttonsArr) {
if(!lastBtn) {
lastBtn = btn;
} else {
// Put a side frames of last and current buttons
CGRect lastBtnRect = [lastBtn frame];
CGRect currentBtnRect = [btn frame];
// Switch frames
[btn setFrame:lastBtnRect];
[lastBtn setFrame:currentBtnRect];
// Keeping last btn for next iteration
lastBtn = btn;
}
}
}
- (NSMutableArray *)getButtonsArray {
// Getting all buttons in self.view and inserting them into a mutable array
NSMutableArray * resultArr = [[NSMutableArray alloc] init];
for (UIButton * btn in [self.view subviews]) {
if([btn isKindOfClass:[UIButton class]]) {
[resultArr addObject:btn];
}
}
return resultArr;
}
- (NSMutableArray *)randomizeArr:(NSMutableArray *)arr {
NSUInteger count = [arr count];
for (NSUInteger i = 0; i < count; ++i) {
unsigned long nElements = count - i;
unsigned long n = (arc4random() % nElements) + i;
[arr exchangeObjectAtIndex:i withObjectAtIndex:n];
}
return arr;
}
For anyone who needs to do this in SWIFT, here's how:
func randomlyPositionAnswerButtons() {
var posX = CGFloat()
var posY_button1 = CGFloat()
var posY_button2 = CGFloat()
var posY_button3 = CGFloat()
// if self.view.frame.size.height == 667 { /* ======== IPHONE 7, 7S, 8 ======== */
posX = 24 // Set all buttons to start from 37
posY_button1 = 310
posY_button2 = 405
posY_button3 = 500
// }
let tab = [
[1,2], [0,2], [0,1]
]
let indexArray = [
NSValue(cgRect:CGRect(x: posX, y: posY_button1, width: 350, height: 74)),
NSValue(cgRect:CGRect(x: posX, y: posY_button2, width: 350, height: 74)),
NSValue(cgRect:CGRect(x: posX, y: posY_button3, width: 350, height: 74))
] as NSMutableArray
// Shuffle the array
var p = UInt32()
p = arc4random() % 3
indexArray.exchangeObject(at: 0, withObjectAt: Int(p))
indexArray.exchangeObject(at: tab[Int(p)][0], withObjectAt: tab[Int(p)][1])
// Assign the buttons their new position
optionButtonA.frame = (indexArray[0] as AnyObject).cgRectValue
optionButtonB.frame = (indexArray[1] as AnyObject).cgRectValue
optionButtonC.frame = (indexArray[2] as AnyObject).cgRectValue
}
Rearranging the fixed buttons sounds like a headache, if only in terms of autolayout constraints.
Why don't you:
Create and position 35 "generic" buttons on the storyboard, each with their own outlet (properties named button01, button02, etc.), or better: use an outlet collection?
Assign a unique tag to each button (0, 1, 2, 3...) on the storyboard.
Assign the same #IBAction method to all your buttons on the storyboard.
At runtime, determine and assign a specific "role" to each button at random (and set each button's label accordingly). But keep track of which button is which, using -for example- a dictionary.
The action linked to all buttons will use the tag of the sender and the information contained in the dictionary mentioned above to determine which "role" button was tapped, and act accordingly.
Does it make sense?
Related
I need to select multiple images from a scroll view.
If I touch on that image, it shows a selection tick (check) mark; if I touch it again, then it would be deselected.
Now I am doing like this:
I would like it so that when I click on that specific image it would show one tick (check) mark; if I touch on the second one, then it would show one tick (check) for this image, same as we select images from the camera roll.
So, please help me as quickly as possible.
The following is my code:
-(void)listimagesandanmae
{
int myFavV = [favCompcamelIDArr count];
for (int i = 0; i < myFavV; i++)
{
CGFloat xOrigin = i * 100;
favimageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin+20,10,70,70)];
favimageView.layer.cornerRadius = favimageView.frame.size.width / 2;
favimageView.clipsToBounds = YES;
// imageView.layer.cornerRadius = 60;
// imageView.clipsToBounds = YES;
favimageView.tag=i;
NSInteger tag = favimageView.tag;
// then do what you want with this
if (([favCompcamelIDArr count] >0) ) {
}
favnamelistingLbl=[[UILabel alloc]initWithFrame:CGRectMake(xOrigin+23,90,90,27)];
favnamelistingLbl.textAlignment = NSTextAlignmentCenter;
favnamelistingLbl.tag=i;
NSLog(#"%lu",(unsigned long)[favcompnsmeArr count]);
if (([favCompcamelIDArr count] > 0) ) {
favnamelistingLbl.text=[favcompnsmeArr objectAtIndex:i];
}
[_selectCamelFav addSubview:favnamelistingLbl];
UITapGestureRecognizer *tappedmyFav = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tappedmyFav:)];
tappedmyFav.numberOfTapsRequired = 1;
[favimageView addGestureRecognizer:tappedmyFav];
NSLog(#"%ld",(long)tag);
NSString *urStCam=#"http://192.8.1.42:/da/";
NSString *urStCam1=[urStCam stringByAppendingString:[favcompictureArr objectAtIndex:i]];
NSString *urStCam2= [urStCam1 stringByReplacingOccurrencesOfString:#"\n" withString:#""];
[caArray addObject:urStCam2];
NSLog(#"%#",caArray);
NSURL *newUrl=[NSURL URLWithString:[caArray objectAtIndex:i]];
NSData *imageDatash = [NSData dataWithContentsOfURL:newUrl];
UIImage *image123 = [UIImage imageWithData:imageDatash scale:2.0];
[favimageView setImage:image123];
[_selectCamelFav addSubview:favimageView];
favimageView.userInteractionEnabled = YES;
}
_selectCamelFav.contentSize = CGSizeMake(myFavV * 210, 171);
[_indicatorA startAnimating];
_indicatorA.hidden=YES;
}
- (void)tappedmyFav:(UITapGestureRecognizer*)tap
{
UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) tap;
}
You should subclass UIView. Then add UIImageView, UILabel (the check mark) and a property to control if the UIView is selected. Also add Tap gesture handler (selected = !selected when tapped)
To search which view are selected, search through your scroll view for the 'selected' UIView object. Or you can maintain your own list once is an object is 'selected/unselected' so that you don't need to search all the objects.
I'm not sure if this has been asked before, but I'm gonna ask anyways. I am working on a grid of buttons, with letters as the titles of each one. I am getting the contents of these letters from an array, and I believe that is where my issue is coming from. The titles appear just fine, but when I press on any of the buttons in my array, that is where my issue comes in.
Every time I press a letter, the part of my NSLog NSLog(#"Letter Added to Array: %#", self.sectionButton.titleLabel.text);
displays
Letter Added to Array: S
and that is all that is displayed. No matter what button is pushed. I am thinking it might just be because of S being the last object in my array, which is why it's saying that. I don't know, so ANY help would be appreciated.
Here is my code:
- (void) didTouchButton
{
[self.lettersPressedArray addObject:self.sectionButton.titleLabel.text];
NSLog(#"Letter Added to Array: %#", self.sectionButton.titleLabel.text);
NSLog(#"Letters Pressed = %#", self.lettersPressedArray);
}
- (void)showGridWithRows:(int)r columns:(int)c arrayOfContent:(NSArray *)content withSizeOfContent:(CGFloat)contentSize
{
for (int i = 0; i < content.count; i++) {
// vars
int row = (int)i / r; // to figure out the rows
int col = i % c; // to figure out the columns
float x = 0;
float y = 0;
// sizing options
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
CGSize screen = [UIScreen mainScreen].bounds.size;
if (screen.height == 480) {
x = 1 + (40 * col);
y = 1 + (30 * row);
}
else {
x = 2 + (40 * col);
y = 1 + (31 * row);
}
}
else {
x = .5 + (90 * col);
y = 1 + (90 * row);
}
//button
self.sectionButton = [UIButton buttonWithType:UIButtonTypeSystem];
[self.sectionButton setTintColor:[UIColor lightGrayColor]];
self.sectionButton.frame = CGRectMake(x, y, contentSize, contentSize);
self.sectionButton.tag = 100 + row * c + col;
[self.sectionButton addTarget:self action:#selector(didTouchButton) forControlEvents:UIControlEventTouchUpInside];
// font stuff
self.sectionButton.titleLabel.textAlignment = NSTextAlignmentCenter;
self.sectionButton.titleLabel.backgroundColor = [UIColor clearColor];
self.sectionButton.titleLabel.font = [UIFont fontWithName:#"Helvetica-Light" size:22];
[self.sectionButton setTitleColor:[UIColor blackColor]/*[UIColor colorWithRed:241.0/255.0 green:124.0/255.0 blue:22.0/255.0 alpha:1.0]*/ forState:UIControlStateNormal];
// title
s = (NSString *)[content objectAtIndex:i];
[self.sectionButton setTitle:s forState:UIControlStateNormal];
// color
if ([s isEqualToString:#"A"] ) {
[self.sectionButton setBackgroundColor:[UIColor greenColor]];
}
else if([s isEqualToString:#"Z"]) {
[self.sectionButton setBackgroundColor:[UIColor redColor]];
}
else {
[self.sectionButton setBackgroundColor:[UIColor lightGrayColor]];
}
//layer
self.sectionButton.layer.borderWidth = .65;
//self.sectionButton.layer.cornerRadius = 5.0;
for(int j = 0; j < c; j++) {
for (int k = 0; k < r; k++) {
[self addSubview:self.sectionButton];
}
}
}
}
Change this:
- (void) didTouchButton
{
[self.lettersPressedArray addObject:self.sectionButton.titleLabel.text];
NSLog(#"Letter Added to Array: %#", self.sectionButton.titleLabel.text);
NSLog(#"Letters Pressed = %#", self.lettersPressedArray);
}
To this:
- (void) didTouchButton:(UIButton *)sender
{
[self.lettersPressedArray addObject:sender.titleLabel.text];
NSLog(#"Letter Added to Array: %#", sender.titleLabel.text);
NSLog(#"Letters Pressed = %#", self.lettersPressedArray);
}
And when you assign your button selector add a ':'
So, this:
#selector(didTouchButton)
Will be:
#selector(didTouchButton:)
Probably implemented like:
[someButton addTarget:self action:#selector(didTouchButton:) forControlEvents:UIControlEventTouchUpInside];
Because:
The way you originally reference your button via self.selectionButton.titleLabel.text accesses one specific instance of a button. This means that no matter what button triggers didTouchButton it gets the content of self.selectionButton which I'm assuming displays an "S". This means that no matter what happens, you add more of the letter "S" to your array. By configuring our action method like we did, it will pass "self" as an argument. This means that we have a variable representing whoever called the method within the method. We will use that to get our contents and add them to the array.
Couple things here. Firstly, you likely don't want to have the button be a property, because you are constantly overwriting it and are ultimately left with it referencing the last one you created... What you're basically doing is the same as:
int x = 1;
x = 2;
x = 3;
printing x will ALWAYS result in 3... Make sense?
The solution to your problem is to pass the button you are tapping as a parameter to the function that handles the action, by adding in a ":" after "didTouchButton" and changing the way you create that function. When you create the button, add the : after the function name like this:
[button addTarget:self action:#selector(didTouchButton:) forControlEvents:UIControlEventTouchUpInside];
That allows a reference to the button pressed to be passed to the function, so you can do this to handle it:
- (void)didTouchButton:(UIButton *)button {
NSString *titleOfPressedButton = button.titleLabel.text;
}
You need to change didTouchButton method to accept (id)sender parameter and change how you set up it's selector while creating a button to didTouchButton:.
This way you will receive a button object pointer inside the didTouchButton and will be able to get its information.
I'am searching for a (very) simple way to crossfade images within animationDuration time. I tried using NSTimer and some other stuff I found here, but no solution satisfy or helps me.
I create this stuff in a method (without comments) ...
- (UIImageView *)playSequence{
NSArray *structureArray = self.contenManager.ImageViewControllerSize;
NSMutableArray *structuresToPlay = [[NSMutableArray alloc] init];
// walk thru current collection
for(NSInteger i = 0; i < [self.currentCollection count]; i++)
{
NSInteger index = [[self.currentCollection objectAtIndex:i] integerValue];
[structuresToPlay addObject:[UIImage imageNamed:[structureArray objectAtIndex:index]]];
}
_playSequence = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
_playSequence.animationImages = structuresToPlay;
_playSequence.animationDuration = 5 * structuresToPlay.count;
[_playSequence startAnimating];
return _playSequence;
}
Here is the part where the method is called ...
- (void)justPlaySelection
{
UIView *justPlaySelection = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
// some background music code ...
// add play sequence
[justPlaySelection addSubview:self.playSequence];
// add close button
// { ... }
[self presentSemiView:justPlaySelection];
}
- (UIImageView *)playSequence
{
if (_playSequence == nil)
{
NSArray *structureArray = self.contenManager.ImageViewControllerSize;
NSMutableArray *structuresToPlay = [[NSMutableArray alloc] init];
// walk thru current collection
for(NSInteger i = 0; i < [self.currentCollection count]; i++)
{
NSInteger index = [[self.currentCollection objectAtIndex:i] integerValue];
[structuresToPlay addObject:[UIImage imageNamed:[structureArray objectAtIndex:index]]];
}
_playSequence = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
}
_playSequence.animationImages = structuresToPlay;
_playSequence.animationDuration = 5 * structuresToPlay.count;
[_playSequence startAnimating];
return _playSequence;
}
Here is another way to switch between to UIImageViews, it wouldn't be hard to add in the fade in and out.
- (void) performAnimationOfFrameAtIndex:(NSNumber*)indexNum
{
int index = indexNum.intValue;
BOOL completedSequece = index >= [self.frames count];
if (completedSequece) return;
UIImageView *imgIn;
UIImageView *imgOut;
if (index % 2 == 0) {
imgIn = self.animationImageViewOne;
imgOut = self.animationImageViewTwo;
}
else {
imgIn = self.animationImageViewTwo;
imgOut = self.animationImageViewOne;
}
imgIn.image = [self.frames objectAtIndex:index];
[self.view sendSubviewToBack:imgOut];
double speed = 0.1;
[self performSelector:#selector(performAnimationOfFrameAtIndex:) withObject:[NSNumber numberWithInt:index + 1] afterDelay:speed];
}
I created a project that does exactly what you describe (an effect like UIImageView animation sequence, but with a cross-fade between images.)
The project is on github: https://github.com/DuncanMC/Animate-Img
You need to have 2 image views.
The trick I use is to stack them on top of each other, and simply animate the top image view from alpha 1 -> 0, then switch images in the top image view and animate it from alpha 0 -> 1.0, so the new image is revealed.
The project demos a morph transition using only 5 frames. Each frame is marked with a number so you can see the frame changes. It has a switch which lets you turn cross-fading on or off.
in my app i'm using a scroll View (paging). each view in the scroll view (UIImageView) contains an image and each image has tags (buttons) scattered all over the pic. so here is my code:
for (int i = 0; i < self.pictures.count; i++)
{
//The first loop is to loop to every picture
int nbOfTags = classObject.tagsImages.count;
for (int j = 0; j < nbOfTags; j++)
{
//Second loop is to loop to each tag corresponding to the picture
ListTags *listTags = [[ListTags alloc]init];
NSMutableArray *tagInfo = [[NSMutableArray alloc]init];
listTags = [tagInfo objectAtIndex:j];
float tagX = [listTags.tag_x floatValue];
float tagY = [listTags.tag_y floatValue];
float x = 1024*i+ tagX;
float y = tagY;
CGRect rect = CGRectMake(x, tagY, 20, 20);
UIButton *button = [[UIButton alloc] initWithFrame:rect];
UIImage * buttonImage = [UIImage imageNamed:#"blueCircle.png"];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[self.scrollView addSubview:button];
}
}
problem: when i run the code i can't see the buttons (tags) on the picture. if i replace "i" with "j" in
float x = 1024*i+ tagX;
the buttons can be seen, however it's not the desired coordinates. so why "i" can't work? am i doing anything wrong or missing anything?
In interface builder (or storyboard - IB) uncheck autolayout (if it's checked) then your button will be displayed where you want.
See my comments in the code below:
for (int i = 0; i < self.pictures.count; i++)
{
//The first loop is to loop to every picture
/*
* This will give you the same number of tags for every image.
* Is that what you want?
*/
int nbOfTags = classObject.tagsImages.count;
for (int j = 0; j < nbOfTags; j++)
{
//Second loop is to loop to each tag corresponding to the picture
/* This allocates a brand new ListTags object EVERY time through the loop. */
ListTags *listTags = [[ListTags alloc]init];
/* This allocates a brand new EMPTY array EVERY time through the loop. */
NSMutableArray *tagInfo = [[NSMutableArray alloc]init];
/*
* This discards the new ListTags object that you just created two lines ago
* and replaces it with an object from tagInfo. Which doesn't exist because
* you just created a new array in the previous line. This really should
* crash, which leads me to believe that this isn't the actual code that you
* are using. Post the actual code that you are using so that we can help you
* better.
*/
listTags = [tagInfo objectAtIndex:j];
/* None of this is valid since listTags is nil/we never should have gotten here. */
float tagX = [listTags.tag_x floatValue];
float tagY = [listTags.tag_y floatValue];
float x = 1024*i+ tagX;
float y = tagY;
CGRect rect = CGRectMake(x, tagY, 20, 20);
UIButton *button = [[UIButton alloc] initWithFrame:rect];
UIImage * buttonImage = [UIImage imageNamed:#"blueCircle.png"];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[self.scrollView addSubview:button];
}
}
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.
Ok, for this solution I am assuming that you have a "TapxY" BOOL variable and "buttonxY:" selector for each button where x:[A,B,C,D,E] and Y:[1-8].
What you would want to do is make it so your button tags are 1000+n for each button (1000 just so that the tag is never 0. this can be anything as long as you keep track of it.)
This way the tags will be 1000,1001,...,1000+col*row-1 (which will be 1039 in a 5x8).
I am also assuming that tapxY (ex tapA1) will only ever be 0 or 1. What you will want to do is instead make an NSMutableArray of booleans (which can be stored as [NSNumber numberWithBool:boolVal]) which has col*row (40) elements that all start as [NSNumber numberWithBool:NO];
in your buttonAction:(id)sender method you can just have it do:
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.
Then when you create your buttons you just have to do:
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];
}
}
Note: do not copy this code exactly as there are probably a few typos and it is untested
Do you mean this?
#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;
}
Just imagine how the buttons are placed, apply elementary school math like subtraction and division, and the rest is trivial.
For loops to the rescue! You can make a grid with a little visual-spatial thinking:
-(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++;
}
}
It's just basic math here.