【问题标题】:Shuffle the positions of a row of buttons in a UIView随机排列 UIView 中一行按钮的位置
【发布时间】:2013-07-22 11:57:20
【问题描述】:

我有一个单词,根据它的长度,我创建了随机分配单词中的字符的按钮。我将随机排列的字符按钮排列成一行。现在我想对这些按钮的位置进行排序,即1到3,3到5等等。请注意按钮的数量不是固定的,它们取决于字长。这里是示例代码sn-p,以了解我如何创建按钮。

-(void)buttonsCreation:(int)noOfButtons
{
    NSMutableArray *charactersArray = [NSMutableArray array];
    self.wordButtons = [[NSMutableArray alloc]initWithCapacity:30];

    BOOL record = NO;
    int randomNumber;
    self.jumbledWord = [jumbledWord stringByReplacingOccurrencesOfString:@" " withString:@""];

    for (int i=0; [charactersArray count] < jumbledWord.length; i++) //Loop for generate different random values
    {
        randomNumber = arc4random() % jumbledWord.length;//generating random number
        if(i==0)
        {
            [charactersArray addObject:[NSNumber numberWithInt:randomNumber]];
        }
        else
        {
            for (int j=0; j<= [charactersArray count]-1; j++)
            {
                if (randomNumber ==[[charactersArray objectAtIndex:j] intValue])
                    record = YES;
            }
            if (record == YES)
                record = NO;
            else
                [charactersArray addObject:[NSNumber numberWithInt:randomNumber]];
        }
    }

    int arrayValue;
    float x;
    float y;

    if ([jumbledWord length]>11)
    {
        int remainingWordslength = [jumbledWord length]-11;
        x = ((475/2) - (38.0 *(11/2.0))) + 8;
        y = ((475/2) - (38.0 *(remainingWordslength/2.0))) + 8;

    }
    else
    {
        x = ((475/2) - (38.0 *([jumbledWord length]/2.0))) + 8;
    }

    for(int i=0;i<[jumbledWord length];i++)
    {
        CGFloat translation = 0.0;

        if(i>=0 && i<11)
        {
            arrayValue = [[charactersArray objectAtIndex:i] intValue];
            UIButton *characterButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            characterButton.frame = CGRectMake(x,160.0, 35.0, 35.0);
            x = x + 38;

            if (jumbledWord.length>=1 && jumbledWord.length<5)
            {
                translation = (self.view.frame.size.width - characterButton.frame.size.width)+65.0;
            }
            if (jumbledWord.length>=5 && jumbledWord.length<11)
            {
                translation = (self.view.frame.size.width - characterButton.frame.size.width)+160.0;
            }
            characterButton.transform = CGAffineTransformMakeTranslation(translation, 0);
            [UIView animateWithDuration:1.30f
                                  delay:0.7
                                options:UIViewAnimationOptionTransitionFlipFromRight
                             animations:^{
                                 characterButton.transform = CGAffineTransformIdentity;
                             } completion:^(BOOL finished) {
                             }];
            [wordButtons addObject:characterButton];
            NSString *characterValue = [NSString stringWithFormat:@"%c",[jumbledWord characterAtIndex:arrayValue]];
            [characterButton setTitle:characterValue forState:UIControlStateNormal];
            [characterButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
            if (arrayValue==0)
            {
                arrayValue = 100;
            }
            [characterButton setTag:arrayValue*100];
            [self.view addSubview:characterButton];
        }
        else if(i>=11 && i<20)
        {
            arrayValue = [[charactersArray objectAtIndex:i] intValue];
            UIButton *characterButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            characterButton.frame = CGRectMake(y,200.0, 35.0, 35.0);
            y = y + 38;

            if (jumbledWord.length>=11 && jumbledWord.length<15)
            {
                translation = (self.view.frame.size.width - characterButton.frame.size.width)+70.0;
            }
            if (jumbledWord.length>=15 && jumbledWord.length<=20)
            {
                translation = (self.view.frame.size.width - characterButton.frame.size.width)+150.0;
            }
            characterButton.transform = CGAffineTransformMakeTranslation(translation, 0);
            [UIView animateWithDuration:1.30f
                                  delay:0.7
                                options:UIViewAnimationOptionTransitionFlipFromRight
                             animations:^{
                                 characterButton.transform = CGAffineTransformIdentity;
                             } completion:^(BOOL finished) {
                             }];

            [wordButtons addObject:characterButton];
            NSString *characterValue = [NSString stringWithFormat:@"%c",[jumbledWord characterAtIndex:arrayValue]];
            [characterButton setTitle:characterValue forState:UIControlStateNormal];
            [characterButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
            if (arrayValue==0)
            {
                arrayValue = 100;
            }
            [characterButton setTag:arrayValue*100];
            [self.view addSubview:characterButton];
        }
    }
}

我确实承认理想的选择是分配框架,即按钮的“x”位置,但我该怎么做。另一个选择是交换索引位置,我也尝试过,即:

-(void)shuffleButtons
{
    for (UIButton *characterButton in wordButtons)
    {
        id sortObject1 = [self.wordButtons objectAtIndex:0];
        id sortObject1 = [self.wordButtons objectAtIndex:1];
        id sortObject1 = [self.wordButtons objectAtIndex:2];
        [self.wordButtons removeObjectAtIndex:0];
        [self.wordButtons removeObjectAtIndex:1];
        [self.wordButtons insertObject:characterButton atIndex:1];
        [self.wordButtons insertObject:characterButton atIndex:0];
        [self.wordButtons removeObjectAtIndex:2];
        [self.wordButtons insertObject:characterButton atIndex:1];
    }
}

哎呀,它崩溃了,请任何人指导我一个方法。我需要完成,因为用户可以选择打乱单词的混乱排列。

提前致谢:)

【问题讨论】:

    标签: iphone uibutton shuffle


    【解决方案1】:

    我已经为此开发了一个示例项目。

    在 github 上查看 DragME 源代码。

    【讨论】:

      【解决方案2】:

      由于我们在 wordButtons 数组中拥有所有按钮,我们可以利用这些索引位置并通过一点动画来交换它们的位置;),即

      - (void)exchangeButtons
      {
         UIButton *firstButton = self.wordButtons[0];
         UIButton *lastButton = self.wordButtons[self.wordButtons.count - 1];
         UIButton *secondButton = self.wordButtons[1];
         UIButton *thirdButton = self.wordButtons[2];
         [self exchangeButton:firstButton withAnother:lastButton];
         [self exchangeButton:secondButton withAnother:thirdButton];
      }
      
      //Animate buttons while interchanging positions
      - (void)exchangeButton:(UIButton *)firstButton withAnother:(UIButton *)secondButton
      {
          CGRect firstButtonFrame = firstButton.frame; [UIView animateWithDuration:1.0f animations:^{ firstButton.frame = secondButton.frame; secondButton.frame = firstButtonFrame; }];
      }
      

      请注意,交换按钮代码可能会有所不同,这完全取决于单词的长度,即按钮的数量。因此您可以设置 if(wordButtons.count >= 6) 或 >=10 等,然后再交换按钮索引。

      P.S. -- 更有效且非硬编码的解决方案

      让我们在创建一行按钮时存储按钮的框架并使用它来交换按钮位置,即在创建时将我们的按钮框架分配给声明的 CGRect 对象,即

      CGRect originalFrameArray[16]; --> **Global variable**
      originalFrameArray[i] = characterButton.frame;
      

      现在让我们创建 2 个数组,一个用于打乱单词并存储字符按钮索引,另一个用于修改按钮数组,然后开始交换按钮并带有一点动画:)

      - (void)exchangeButtons
      {
          NSMutableArray *charactersArray = [NSMutableArray array];
          NSMutableArray *copyOfButtons = [NSMutableArray arrayWithArray:wordButtons];
      
          BOOL record = NO;
          int randomNumber;
      
          for (int i=0; [charactersArray count] < jumbledWord.length; i++) //Loop for generate different random values
          {
              randomNumber = arc4random() % jumbledWord.length;//generating random number
              if(i==0)
              {
                  [charactersArray addObject:[NSNumber numberWithInt:randomNumber]];
              }
              else
              {
                  for (int j=0; j<= [charactersArray count]-1; j++)
                  {
                      if (randomNumber ==[[charactersArray objectAtIndex:j] intValue])
                          record = YES;
                  }
                  if (record == YES)
                      record = NO;
                  else
                      [charactersArray addObject:[NSNumber numberWithInt:randomNumber]];
              }
          }
      
          int arrayValue;
      
          for(int i=0;i<[jumbledWord length];i++)
          {
              arrayValue = [[charactersArray objectAtIndex:i] intValue];
              [wordButtons replaceObjectAtIndex:arrayValue withObject:[copyOfButtons objectAtIndex:i]];
          }
          for(int i=0; i < [jumbledWord length]; i++)
          {
              UIButton *characterButton = [wordButtons objectAtIndex:i];
              [self shiftButtonToFrame:characterButton :originalFrameArray[i]];
          }
      }
      
      //Animate the buttons while they exchange positions
      - (void) shiftButtonToFrame:(UIButton *) characterButton :(CGRect) frame
      {
          [UIView beginAnimations:nil context:NULL];
          [UIView setAnimationBeginsFromCurrentState:YES];
          [UIView setAnimationDuration:1.0];
          [characterButton setFrame:frame];
          [UIView commitAnimations];
      }
      

      希望它对某人有所帮助。谢谢,编码愉快:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-14
        • 1970-01-01
        • 2020-09-17
        • 1970-01-01
        相关资源
        最近更新 更多