我有一個詞,根據它的長度,我創建按鈕隨機分配單詞中的一個字符。我在一行中呈現混洗的字符按鈕排列。現在,我會喜歡排序這些按鈕的位置,即第一到第三,第三到第五等。請注意,按鈕的數量並不固定,它們因字長而異。這裏是示例代碼片段,用於瞭解我如何創建按鈕。隨機播放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需要完成,因爲對於用戶洗牌單詞的混亂安排的選項。
在此先感謝:)