2013-07-23 2 views
0

단어의 길이에 따라 임의로 단어의 문자를 할당하는 단추를 만듭니다. 행의 문자 번갈기 배열을 나타냅니다. 지금은 이러한 단추의 위치를 ​​정렬하는 것과 같습니다. 즉 1 ~ 3, 3 ~ 5 등등. 버튼 수는 고정되어 있지 않으므로 단어 길이에 따라 달라집니다. 여기 단추를 만드는 방법을 이해할 수있는 샘플 코드 조각이 있습니다.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]; 
     } 
    } 
} 

I 프레임 버튼에 대한 즉, "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과 충돌, 어떤 사람은 저를 인도하시기 바랍니다 했는데요 단어의 뒤죽박죽 배열을 셔플하는 사용자에 대한 옵션이 있기 때문에 수행해야합니다.

미리 감사드립니다.

답변

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. - 더 효과적이고 비 하드 코딩 솔루션

하자 저장 버튼의 프레임 버튼의 행을 생성하고 위치가 즉 버튼을 교환하는 것을 사용하는 것은 창조 선언 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에서 살펴보십시오.