iphone - 我们如何对 Sequence UIButtons 执行操作?

标签 iphone ios algorithm uibutton uilabel

enter image description here

正如我的屏幕截图所示,我正在研究单词匹配游戏。在这个游戏中,我将我的单词分配给不同位置上的特定序列的不同 UIButton(我的红色箭头显示了这个序列),其余的 UIButtons 我分配了一个随机字符(A-Z)。当我单击任何 UIButtons 时,其标题将分配给位于当前部分 Fornt 中的 UILabel:我将此 UILabel 文本保存到 UILabels 文本下方,该文本位于计时器的形式中。当它与我的任何匹配时UILabels 将被删除。我已经实现了所有这些过程。

但我的问题是由黑线显示的问题。如果玩家找到第一个单词“DOG”。他单击序列中的两个 UIButton,但不按序列中的第三个按钮。(如黑线所示)。所以在这里我希望当玩家按下不在序列中的任何 UIButton 时,然后删除之前的文本(这是“DO") 的 UILabel,现在 UILabel 的文本只有 "G"。 这是我获取 UIButtons 标题并为其分配 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=@"";
  }

 }}

提前致谢

最佳答案

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

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

现在跟踪上一个按下的按钮和当前按下的按钮。 当您的按钮委托(delegate)点击时调用以下函数:

将以下代码写入您的 .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;
     }
}

希望对您有所帮助。

关于iphone - 我们如何对 Sequence UIButtons 执行操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10795952/

相关文章:

iphone - & 符号在 Objective-C 中是什么意思?

ios - PhotoKit:fetchAssetCollectionsWithLocalIdentifiers 失败

ios - 此崩溃报告告诉我为什么我的应用无法启动?

java - Java 中的高级递归

iphone - 在循环中创建的 UIButton 不可点击

如果连接不可用,iOS 服务请求的重试机制

iphone - 可以限制 UIScrollView 中的缩放/平移区域吗?

ios - UITableView 设置为静态单元格。是否可以以编程方式隐藏某些单元格?

c++ - 磁盘调度程序 SCAN 算法错误

C++11 std::reverse() 的便捷包装器