【问题标题】:How we can perform Action on Sequence UIButtons?我们如何对序列 UIButton 执行操作?
【发布时间】:2012-06-03 11:42:43
【问题描述】:

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

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

 }}

提前感谢

【问题讨论】:

  • 您是否可以继承 UIButton 或创建一个 UIButton 类别来添加您可以在运行时设置的属性,该属性指定按钮是否是单词的一部分?这可能会对您有所帮助:stackoverflow.com/questions/5500327/…
  • 为每个按钮分配标签。并在您按下时获取标签。将标签保存在变量中,例如 name , prevTapBtn=10 然后另一个变量 nextTapBtn=11 应该是 11,比前一个抽头大一。如果是这样,那么 countinue else 删除。希望这会对你有所帮助。

标签: iphone ios algorithm uibutton uilabel


【解决方案1】:

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

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
}

【讨论】:

  • 感谢您的回复。我看过你的代码。我从你的代码中得到了什么,你通过传递 previous.frame 和另一个通过使用 previous 并检查这两个的 Intersect 来制作 previousRect 。但我的问题是在哪里检查当前和以前的邻接关系?
  • 它正在检查 -(void)aMethod 函数。如果它们相邻,则附加字母,否则不要...?
  • 感谢您的回复,但我询问的是与 isAdjacent 代码块中的上一个按钮的当前邻接。
  • 相邻的矩形是三个“网格”方块宽,三个“网格”方块高,以您上一个按钮为中心。如果您当前的按钮在这个由九个正方形组成的网格内(即:相邻),则该方法返回 true。
  • @paul thanx 帮助我。我完全实现了您的方法,但它会获取我单击 UIButtons 的每个位置的文本。它不会替换 UILabel 的文本,以防玩家不遵循我在问题中提到的 UIButton 序列。
【解决方案2】:

有趣的问题: 在这种特殊情况下,我同意 Luke 关于子类化 UIButton 的观点。这样,您可以在网格上为每个按钮提供一个 (X, Y),以及一个 (Xnext, Ynext) 列表,用于所有可能的预期下一次按下位置(如果按钮本身可用于制作多个单词)。在外部,您会将当前命中的值与预期值(Xnext、Ynext)进行比较。如果两者不匹配,这就是您要寻找的信号。

这是一个适用于所有情况的答案,向前和向后水平(如果您选择实施 backware),向上和向下垂直(如果您选择向上实施),以及任何对角线或任何其他组合可以拿出来!

这也说明了先按 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”对象。

网格对象标题应如下所示:

@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 部分。这至少为您提供了一些工具来创建自定义按钮和围绕它的单词列表算法。

【讨论】:

  • 很遗憾,我无法编辑您上面的代码,因为您没有 UIButton 对象代码。给我几分钟,我将提供几行示例代码,说明如何子类化 UIButton 类。
  • 抱歉,我发现我用的是 NSButton 而不是 UIButton。
  • 你能给我电子邮件地址吗?我将应用程序代码发送给你,我确定你的方法对我有用,但对我来说很难在我的应用程序中以正确的方式实现。谢谢
  • 如果您查看我的个人资料,将会有一个指向我网站的链接。从那里您将能够转到“联系我们”->“获取支持”链接。这是我的电子邮件地址的链接:-)
  • @trumpetlicks 再次感谢我已经在您的电子邮件地址上向您发送了完整的代码。请检查它。等待您的回复。
【解决方案3】:

如果我理解正确的话。如果选择的字母都彼此相邻,用户只能有一个正确的单词?

您是否尝试过: 保留两个参考。一个 UIButton 用于 previousPressedButton,一个 UIButton 用于 lastPressedButton。

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

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

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

    -(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,那么这个词仍然是同一个方向。如果是其他内容,请擦除并重新开始。

希望这会有所帮助。

【讨论】:

  • @Tjeerd 我实现了你的代码,但它对我不起作用,你能用我的代码编辑你的答案吗?
【解决方案4】:

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

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

希望它会有所帮助。

【讨论】:

  • 我尝试实现您的代码,但是在按钮上单击我们如何检查您在您的 code.bcz 中提到的条件,我调用该函数 - (void)aMethod:(id)sender,你能帮我以正确的方式实现你的代码吗.thanx
  • 我将标签分配给每个 uibutton ,但是在按钮上单击我们如何调用你的功能我试试这个.. [btn addTarget:self action:@selector(isAdjacent:)forControlEvents:UIControlEventTouchUpInside];但是当我在每个按钮上调用它时,单击它会杀死我的应用程序。
  • Thanx @Apurv 它的工作如我所愿..thanx 捆绑包亲爱的..我感到非常高兴..看到它对我有用..thanx
猜你喜欢
  • 2021-04-07
  • 1970-01-01
  • 2021-03-26
  • 1970-01-01
  • 1970-01-01
  • 2019-07-01
  • 2019-04-14
  • 2014-02-12
  • 1970-01-01
相关资源
最近更新 更多