How can we perform actions on UIButtons sequences?

enter image description here

As shown on the My Screen screen, I’m working on a match-matching game. In this game, I assign my words to different UIButtons in a certain sequence at different levels (my red arrow shows this sequence), and the rest of the UIButtons I assign one of the random characters (AZ). When I click on any UIButtons, its title will be assigned to UILabel, which is in the Fornt of the current section: I place this UILabel text below the text of UILabels, which is in timer mode. When it matches any of my UILabels it will be deleted. Now all this has already been implemented.

But my problem is that this show is black lines. If the player finds the first word that is "DOG". he clicks “Two UIButtons in Sequence”, but does not click “Third” in the “Sequence” (as shown by the black line). Here, I want when a player clicks on any UIButtons that are not in Sequence, then delete the previous text (which is "DO") by UILabel, and now the text of UILabel is only "G". Here is my code to get the names of UIButtons and assign it to UILabel.

- (void)aMethod:(id)sender { UIButton *button = (UIButton *)sender; NSString *get = (NSString *)[[button titleLabel] text]; NSString *origText = mainlabel.text; mainlabel.text = [origText stringByAppendingString:get]; if ([mainlabel.text length ]== 3) { if([mainlabel.text isEqualToString: a]){ lbl.text=@"Right"; [btn1 removeFromSuperview]; score=score+10; lblscore.text=[NSString stringWithFormat:@"%d",score]; words=words-1; lblwords.text=[NSString stringWithFormat:@"%d",words]; mainlabel.text=@""; a=@"tbbb"; } else if([mainlabel.text isEqualToString: c]){ lbl.text=@"Right"; [btn2 removeFromSuperview]; score=score+10; lblscore.text=[NSString stringWithFormat:@"%d",score]; words=words-1; lblwords.text=[NSString stringWithFormat:@"%d",words]; mainlabel.text=@""; c=@"yyyy"; } else if([mainlabel.text isEqualToString: d]){ lbl.text=@"Right"; [btn3 removeFromSuperview]; score=score+10; lblscore.text=[NSString stringWithFormat:@"%d",score]; words=words-1; lblwords.text=[NSString stringWithFormat:@"%d",words]; mainlabel.text=@""; d=@"yyyy"; } else { lbl.text=@"Wrong"; mainlabel.text=@""; } }} 

Thanx in advance

+8
algorithm ios iphone uilabel uibutton
source share
4 answers

Assign a tag to each button from left to right. So you will have

GButton.tag = 0; TButton.tag = 1; DButton.tag = 2; ,,, VButton.tag = 9; ,,, EButton.tag = 18; ,,, CButton.tag = 26;

Now save the track of the previous pressed button and the pressed button. Call the function below when your delegates clicked a button:

Write below code in your .h file

 #define SEQ_TYPE_ANY 0 #define SEQ_TYPE_RIGHT 1 #define SEQ_TYPE_LEFT 2 #define SEQ_TYPE_TOP 3 #define SEQ_TYPE_BOTTOM 4 #define SEQ_TYPE_RIGHT_DIAGONAL_DOWN 5 #define SEQ_TYPE_RIGHT_DIAGONAL_UP 6 #define SEQ_TYPE_LEFT_DIAGONAL_DOWN 7 #define SEQ_TYPE_LEFT_DIAGONAL_UP 8 #define NO_OF_BUTTONS_IN_ROW 9 //Add below variables into your class int curentSequence; UILabel *resultLabel; UIButton *previousButton; //Declare property for previousButton @property(nonatomic, retain) UIButton *previousButton; //Write below code to .m file @synthesize previousButton; -(BOOL) isAdjacent:(UIButton *)currentButton { if(previousButton == nil) { resultLabel.text = currentButton.titleLabel.text; curentSequence = SEQ_TYPE_ANY; return TRUE; } if((curentSequence == SEQ_TYPE_ANY || curentSequence == SEQ_TYPE_RIGHT) && (previousButton.tag + 1 == currentButton.tag)) { resultLabel.text = [resultLabel.text stringByAppendingString:currentButton.titleLabel.text]; curentSequence = SEQ_TYPE_ANY; return TRUE; } else if((curentSequence == SEQ_TYPE_ANY || curentSequence == SEQ_TYPE_LEFT) && (previousButton.tag - 1 == currentButton.tag)) { resultLabel.text = [resultLabel.text stringByAppendingString:currentButton.titleLabel.text]; curentSequence = SEQ_TYPE_LEFT; return TRUE; } else if((curentSequence == SEQ_TYPE_ANY || curentSequence == SEQ_TYPE_TOP) && (previousButton.tag - NO_OF_BUTTONS_IN_ROW == currentButton.tag)) { resultLabel.text = [resultLabel.text stringByAppendingString:currentButton.titleLabel.text]; curentSequence = SEQ_TYPE_TOP; return TRUE; } else if((curentSequence == SEQ_TYPE_ANY || curentSequence == SEQ_TYPE_BOTTOM) && (previousButton.tag + NO_OF_BUTTONS_IN_ROW == currentButton.tag)) { resultLabel.text = [resultLabel.text stringByAppendingString:currentButton.titleLabel.text]; curentSequence = SEQ_TYPE_BOTTOM; return TRUE; } else if((curentSequence == SEQ_TYPE_ANY || curentSequence == SEQ_TYPE_RIGHT_DIAGONAL_DOWN) && (previousButton.tag + NO_OF_BUTTONS_IN_ROW + 1 == currentButton.tag)) { resultLabel.text = [resultLabel.text stringByAppendingString:currentButton.titleLabel.text]; curentSequence = SEQ_TYPE_RIGHT_DIAGONAL_DOWN; return TRUE; } else if((curentSequence == SEQ_TYPE_ANY || curentSequence == SEQ_TYPE_RIGHT_DIAGONAL_UP) && (previousButton.tag - NO_OF_BUTTONS_IN_ROW + 1 == currentButton.tag)) { resultLabel.text = [resultLabel.text stringByAppendingString:currentButton.titleLabel.text]; curentSequence = SEQ_TYPE_RIGHT_DIAGONAL_UP; return TRUE; } else if((curentSequence == SEQ_TYPE_ANY || curentSequence == SEQ_TYPE_LEFT_DIAGONAL_UP) && (previousButton.tag - NO_OF_BUTTONS_IN_ROW - 1 == currentButton.tag)) { resultLabel.text = [resultLabel.text stringByAppendingString:currentButton.titleLabel.text]; curentSequence = SEQ_TYPE_LEFT_DIAGONAL_UP; return TRUE; } else if((curentSequence == SEQ_TYPE_ANY || curentSequence == SEQ_TYPE_LEFT_DIAGONAL_DOWN) && (previousButton.tag + NO_OF_BUTTONS_IN_ROW - 1 == currentButton.tag)) { resultLabel.text = [resultLabel.text stringByAppendingString:currentButton.titleLabel.text]; curentSequence = SEQ_TYPE_LEFT_DIAGONAL_DOWN; return TRUE; } else { resultLabel.text = @""; curentSequence = SEQ_TYPE_ANY; return FALSE; } } // Event handler for button event - (void)aMethod:(id)sender { UIButton *currentButton = (UIButton *)sender; BOOL result = [self isAdjacent:currentButton]; if(result == FALSE) { self.previousButton = nil; resultLabel.text = @""; curentSequence = SEQ_TYPE_ANY; } else { self.previousButton = sender; } } 

Hope this helps.

+2
source share

Funny question: In this particular case, I agree with Luke in the UIButton subclass. Thus, you can give each button (X, Y) in its grid, as well as a list (Xnext, Ynext) for all possible expected next places to print (if the button itself can be used to create several words). Outwardly, you compare the current hit with the expected one (Xnext, Ynext). If the two do not match, this is the signal you are looking for.

This is an answer that takes into account all your situations: back and forth horizontally (if you decide to implement backware), up and down vertically (if you decide to implement up) and any diagonal or any other combination that you can think of!

It also means that you can say that pressing D, then O, then trying to press D again compared to hitting G. He will also make sure to hit the wrong G.

Create a new .mh pair of files (new object) and give it your name.

Sample code for implementing a custom UIButton (h file):

 @interface myConnectedUIButton : UIButton { BOOL isAWordBeginCharacter; unsigned int beginWordKey; unsigned int myGridX; unsigned int myGridY; NSMutableArray * myConnectedSet; } -(id)init; -(void)initWithGridX:(unsigned int)X GridY:(unsigned int)Y BeginChar:(BOOL)yesNo BeginWordKey:(unsigned int)key; -(void)setGridPosWithX:(unsigned int)XY:(unsigned int)Y; -(void)setGridX:(unsigned int)X; -(void)setGridY:(unsigned int)Y; -(unsigned int)getGridX; -(unsigned int)getGridY; -(void)setIsABeginChar:(BOOL)yesNo; -(BOOL)getIsABeginChar; -(void)addPosToConnectedSetGridX:(unsigned int)X GridY:(unsigned int)Y WordKey:(unsigned int)key; -(NSArray *)getMyConnectedSetArray; -(void)clearConnectedSet; @end 

In your .m file

 @implementation myConnectedUIButton -(id)init{ [super init]; // Lets go ahead and initialize the NSMutableArray here also IFF it hasnt already been allocated if( nil == myConnectedSet ){ myConnectedSet = [[NSMutableArray alloc] init]; } // Lets also zero out the x, y position myGridX = 0; myGridY = 0; // Lets also state that this is NOT a begin char for the time being and 0 for the begin char key isAWordBeginCharacter = NO; beginWordKey = 0; return self; } -(void)initWithGridX:(unsigned int)X GridY:(unsigned int)Y BeginChar:(BOOL)yesNo BeginWordKey:(unsigned int)key{ // Lets go ahead and initialize the NSMutableArray here also IFF it hasnt already been allocated if( nil == myConnectedSet ){ myConnectedSet = [[NSMutableArray alloc] init]; } myGridX = X; myGridY = Y; isAWordBeginCharacter = yesNo; beginWordKey = key; } -(void)setGridPosWithX:(unsigned int)XY:(unsigned int)Y{ myGridX = X; myGridY = Y; } -(void)setGridX:(unsigned int)X{ myGridX = X; } -(void)setGridY:(unsigned int)Y{ myGridY = Y; } -(unsigned int)getGridX{ return myGridX; } -(unsigned int)getGridY{ return myGridY; } -(void)setIsABeginChar:(BOOL)yesNo{ isAWordBeginCharacter = yesNo; } -(BOOL)getIsABeginChar{ return isAWordBeginCharacter; } -(void)addPosToConnectedSetGridX:(unsigned int)X GridY:(unsigned int)Y WordKey:(unsigned int)key{ [myConnectedSet addObject:[GridPointNext GridPointNextWithX:X GridPointY:Y NextWordKey:key]]; } -(NSArray *)getMyConnectedSetArray{ return myConnectedSet; } -(void)clearConnectedSet{ [myConnectedSet removeAllObjects]; } -(void)dealloc{ [myConnectedSet release]; [super dealloc]; } @end 

Now you also need the "GridPointNext" object.

The title of the Grid object should look like this:

 @interface GridPointNext : NSObject { unsigned int GridPointX; unsigned int GridPointY; unsigned int nextWordKey; } +(GridPointNext *)GridPointNextWithX:(unsigned int)X GridPointY:(unsigned int)Y NextWordKey:(unsigned int)key; -(id)initWithX:(unsigned int)X GridPointY:(unsigned int)Y NextWordKey:(unsigned int)key; -(unsigned int)getGridX; -(unsigned int)getGridY; -(unsigned int)getNextWordKey; @end 

The m file for the object should look like this:

 @implementation GridPointNext +(GridPointNext *)GridPointNextWithX:(unsigned int)X GridPointY:(unsigned int)Y NextWordKey:(unsigned int)key{ GridPointNext * aPoint = [[GridPointNext alloc] initWithX:X GridPointY:Y NextWordKey:key]; [aPoint autorelease]; return aPoint; } -(id)initWithX:(unsigned int)X GridPointY:(unsigned int)Y NextWordKey:(unsigned int)key{ GridPointX = X; GridPointY = Y; nextWordKey = key; return self; } -(unsigned int)getGridX{ return GridPointX; } -(unsigned int)getGridY{ return GridPointY; } -(unsigned int)getNextWordKey{ return nextWordKey; } @end 

You will have to deal with the dealloc part. This will at least give you some tools to create your custom button and your word list algorithm around it.

+2
source share

If I understand correctly, you need to know if the pressed button is next to the previously pressed button? Use this function to check adjacency in a grid:

 bool isAdjacent(UIButton* current, UIButton* previous) { if( !previous ) return false; //Create a rectangle around the previous button (assuming all buttons are in a fixed grid) CGRect previousRect = previous.frame; CGRect adjacentRect = CGRectMake(previous.frame.origin.x - previous.frame.size.width, previous.frame.origin.y - previous.frame.size.height, previous.frame.size.width*3, previous.frame.size.height*3); return CGRectIntersectsRect(adjacentRect, previousRect); } 

And add only if they are adjacent, i.e.:

 - (void)aMethod:(id)sender { UIButton *button = (UIButton *)sender; static UIButton* previous = nil; NSString *get = (NSString *)[[button titleLabel] text]; NSString *origText = mainlabel.text; if( isAdjacent(button, previous) ) mainlabel.text = [origText stringByAppendingString:get]; previous = button; //Rest of function } 
+1
source share

If I understand correctly. Can the user only have the correct word if the selected letters sit next to each other?

Have you tried this: Save the two links. One UIButton for the previousPressedButton and one UIButtonfor lastPressedButton.

First, the user presses D. lastPressedButton will refer to D. Then the user presses O. previousPressedButton will be D. lastPressedButton will be O.

Now, get the width and height of the UIButtons and compare if lastPressedButton.frame.origin.x is less than the width or width. Also check if lastPressedButton.frame.origin.y will be less than height or -high. Now you know if they touch the previous button. Use this to decide if this is a new word or not.

I would put this in a method.

  -(BOOL)isAdjacentLetter { float buttonWidth = lastPressedButton.size.width; float buttonHeight = lastPressedButton.size.height; if(lastPressedButton.frame.origin.x>previousPressedButton.frame.origin.x+buttonWidth) return NO; if(lastPressedButton.frame.origin.y>previousPressedButton.frame.origin.y+buttonHeight) return NO; if(lastPressedButton.frame.origin.x<previousPressedButton.frame.origin.x-buttonWidth) return NO; if(lastPressedButton.frame.origin.y<previousPressedButton.frame.origin.y-buttonHeight) return NO; return YES; } 

Then whenever the button is pressed. you can use

 if ([self isAdjacentLetter]) { //still same word } else { //new word. erase } 

Or if I understand differently. Words can only be made when the letters are in a line. Such as: from left to right. Come down. Top right, etc. In this case, identify all directions. For example, the top left is 0, the top is 1, the top right is 2. right 3. right down 4. down 5. left 5. left down 6. left - 7.

By pressing the two buttons, save the directions. For example: If it is from left to right, direction = 3; Then, when another button is pressed, check the new direction. If it is 3, the word is still in one direction. if it's something else, then erase and start.

Hope this helps.

0
source share

All Articles