0

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

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

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

提前致谢 :)

4

2 回答 2

1

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

在 github上查看DragME源代码。

于 2013-07-23T12:31:43.670 回答
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; }];
}

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

PS——更有效且非硬编码的解决方案

让我们在创建一行按钮时存储按钮的框架并使用它来交换按钮位置,即在创建时将我们的按钮框架分配给声明的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];
}

希望它可以帮助一些人。谢谢和快乐的编码:)

于 2013-07-24T10:25:45.130 回答