我有一个词,根据它的长度,我创建了从字随机分配字符的按钮。我正在连续呈现洗牌的字符按钮排列。现在我想对这些按钮的位置进行排序,即。第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];
}
}
OOPS并且它崩溃了,任何人都请指导我。我需要完成,因为有一个选项让用户可以混淆一个单词的混乱排列。
提前致谢:)
答案 0 :(得分:1)
我已经为此开发了一个示例项目。
在github上查看DragME来源。
答案 1 :(得分: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&gt; = 6)或&gt; = 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];
}
希望它有所帮助。感谢和快乐的编码:)