2013-07-23 43 views
0

我有一個詞,根據它的長度,我創建按鈕隨機分配單詞中的一個字符。我在一行中呈現混洗的字符按鈕排列。現在,我會喜歡排序這些按鈕的位置,即第一到第三,第三到第五等。請注意,按鈕的數量並不固定,它們因字長而異。這裏是示例代碼片段,用於瞭解我如何創建按鈕。隨機播放UIView中一排按鈕的位置

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

OOPS和崩潰,任何一個請指導我way.I需要完成,因爲對於用戶洗牌單詞的混亂安排的選項。

在此先感謝:)

回答

1

由於我們擁有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; }]; 
} 

請注意,交換按鈕的代碼可能會發生變化,這一切取決於字即buttons.So的數目也可放置條件等如果(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]; 
} 

希望交換的按鈕它有助於一個人。感謝和快樂編碼:)

1

我已經爲此開發了一個示例項目。

看看DragME github上的源代碼。