首页 文章

我们如何对序列UIButton执行操作?

提问于
浏览
8

enter image description here

正如我的屏幕截图显示我正在进行单词匹配游戏 . 在这个游戏中,我将我的单词分配给不同位置的特定序列中的不同UIButton(我的红色箭头显示此序列)和其余的UIButtons我指定一个随机字符( AZ) . 当我点击任何UIButtons时,它的 Headers 将分配给UILabel,它位于当前部分的Fornt:我将这个UILabel文本放在UILabels文本下面,该文本不是计时器 . 当它与我的任何UILabel匹配时它的意志被删除 . 我已经实现了所有这个过程 .

但我的问题是黑线表示的问题 . 如果玩家发现第一个单词是“DOG” . 他点击顺序中的两个UIButtons,但不按顺序中的第三个 . (如黑线所示) . 这里我想要当玩家按下任何不在序列中的UIButtons然后删除之前的文本(这是“ UILabel的DO“,现在UILabel的文本只是”G“ . 这是我获取UIButtons Headers 并将其分配给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提前

4 回答

  • 2

    从左到右为每个按钮分配标签 . 所以你会有,

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

    现在保持上一个按下按钮和当前按下按钮的轨迹 . 按钮代理命中时调用以下函数:

    将以下代码写入.h文件

    #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;
         }
    }
    

    希望它会有所帮助 .

  • 2

    有趣的问题:在这个特殊情况下,我同意Luke关于UIButton的子类化 . 通过这种方式,您可以为网格上的每个按钮指定一个(X,Y),以及所有可能的预期下一个按下位置的(Xnext,Ynext)列表(如果按钮本身可用于生成多个单词) . 在外部,您将比较当前被击中的预期(Xnext,Ynext) . 如果两者不匹配,这就是您要寻找的信号 .

    这是一个答案,可以解释您的所有情况,前进和后退水平(如果您选择实施软件),向上和向下垂直(如果您选择向上执行),任何对角线,或任何其他组合,您可以出现有!

    这也解释了让我们说击中D,然后O然后试图再次按下D而不是击中G.它还会照顾到不正确的G.

    创建一个新的.m .h文件对(一个新对象)并给它命名 .

    一些用于实现自定义UIButton(h文件)的示例代码:

    @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)X Y:(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
    

    在您的.m文件中

    @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)X Y:(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
    

    您现在还需要一个“GridPointNext”对象 .

    Grid Object标头应如下所示:

    @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
    

    该对象的m文件应如下所示:

    @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
    

    你将不得不处理dealloc部分 . 这至少为您提供了一些工具来创建自定义按钮和围绕它的单词列表算法 .

  • 0

    如果我理解正确,您需要知道按下的按钮是否与先前按下的按钮相邻?使用此函数测试网格中的邻接:

    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);
    }
    

    只有在它们相邻时才附加,即:

    - (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

    如果我理解正确的话 . 如果所选的字母都是彼此相邻的,那么用户只能拥有正确的单词?

    你试过这个:保留两个引用 . 一个UIButton用于previousPressedButton,一个UIButton用于lastPressedButton .

    首先用户按下D. lastPressedButton将引用D.然后用户按下O. previousPressedButton将是D. lastPressedButton将为O.

    现在,获取UIButtons的宽度和高度,并比较lastPressedButton.frame.origin.x是否小于width或-width . 还要检查lastPressedButton.frame.origin.y是否小于高度或者是否 - . 现在您知道它是否触摸了上一个按钮 . 用它来决定它是否是一个新单词 .

    我会把它放到一个方法中 .

    -(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;
    }
    

    然后每当点击一个按钮 . 您可以使用

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

    或者,如果我理解不同 . 这些单词只能在字母排成一行时才能生成 . 例如:从左到右 . 从下到上 . 从右下角到左上角等 . 在这种情况下,确定所有方向 . 例如,左上角是0,上边是1,右上角是2.右边是3.右边是4.下边是5.左下边是6.左边是7 .

    单击两个按钮时,存储方向 . 例如:如果是从左到右,方向= 3;然后单击另一个按钮时,检查新方向 . 如果它是3,那么这个词仍然在同一个方向 . 如果它是别的东西,那么擦除并重新开始 .

    希望这可以帮助 .

相关问题