有没有办法在iOS应用程序上随机/重新排列按钮?

时间:2015-12-24 06:51:21

标签: ios storyboard

我正在开发一个iOS应用程序。故事板上有35个按钮。无论如何,我可以使用随机生成器随机重新排列或随机播放按钮并仍保持按钮的功能吗?

5 个答案:

答案 0 :(得分:0)

这是可能的,需要一些编码:

  • 使用视图中的所有按钮创建NSMutableArray。
  • 随机化其订单。
  • 运行按钮数组,并在每次迭代时保持最后 按钮视图并使用当前按钮切换其rect 反之亦然。

为了更好地理解,这里是代码(可以使用,只需复制粘贴):

- (void)viewDidLoad {
    [super viewDidLoad];

    // Getting buttons into array
    NSMutableArray * buttonsArr = [self getButtonsArray];

    // Shuffling buttons location
    if(buttonsArr && [buttonsArr count]) {
        [self randomizeButtonsLocation:buttonsArr];
    } else {
        NSLog(@"No buttons found");
    }
}

- (void)randomizeButtonsLocation:(NSMutableArray *)buttonsArr {

    // Randomizing order in button array
    buttonsArr = [self randomizeArr:buttonsArr];
    UIButton * lastBtn = nil;

    for (UIButton * btn in buttonsArr) {
        if(!lastBtn) {
            lastBtn = btn;
        } else {

            // Put a side frames of last and current buttons
            CGRect lastBtnRect = [lastBtn frame];
            CGRect currentBtnRect = [btn frame];

            // Switch frames
            [btn setFrame:lastBtnRect];
            [lastBtn setFrame:currentBtnRect];

            // Keeping last btn for next iteration
            lastBtn = btn;
        }
    }
}

- (NSMutableArray *)getButtonsArray {

    // Getting all buttons in self.view and inserting them into a mutable array
    NSMutableArray * resultArr  = [[NSMutableArray alloc] init];
    for (UIButton * btn in [self.view subviews]) {
        if([btn isKindOfClass:[UIButton class]]) {
            [resultArr addObject:btn];
        }
    }

    return resultArr;
}

- (NSMutableArray *)randomizeArr:(NSMutableArray *)arr {
    NSUInteger count = [arr count];
    for (NSUInteger i = 0; i < count; ++i) {
        unsigned long nElements = count - i;
        unsigned long n = (arc4random() % nElements) + i;
        [arr exchangeObjectAtIndex:i withObjectAtIndex:n];
    }
    return arr;
}

答案 1 :(得分:0)

您可以将所有按钮对象添加到名为buttonsArray的NSMutableArray中,然后使用以下循环来交换按钮:

for (int i = 0; i < buttonsArray.count; i++) {
   int random1 = arc4random() % [buttonsArray count];
   int random2 = arc4random() % [buttonsArray count];
   [buttonsArray exchangeObjectAtIndex:random1 withObjectAtIndex:random2];
}

答案 2 :(得分:0)

如果每个按钮都属于下面的类,那么它很容易改变它们,

MyRandomButton {
    NSInteger uniqueId;
    NSString *title;
    ....
    ....
}

现在,您可以做的是,在初始时,准备MyRandomButton的对象数量(等于StoryBoard上的按钮数)。并将其添加到可变数组(NSMutableArray)中。

像这样,

for(NSInteger index  = 1; index <= 35; index++) {
    MyRandomButton *btnInfo = ...;
    btnInfo.uniqueId = index;
    btnInfo.title = @sometitle;
    [myButtonsArray addObject:btnInfo];
}

在下一步中,如果你已完成某些事情(并且你想要随机播放所有按钮),那么请从这个问题What's the Best Way to Shuffle an NSMutableArray?获取帮助来改组你的数组。

遍历StoryBoard上的所有按钮(在您的情况下:1到35)。

for(NSInteger index  = 1; index <= 35; index++) {
    UIButton *button = (UIButton *)[yourView viewWithTag:index];
    MyRandomButton *btnInfo = [myButtonsArray objectAtIndex:(index-1)];         
    button.tag = btnInfo.uniqueId;
    [button setTitle:btnInfo.title forState:UIControlStateNormal];
}

在上面的循环中,我们更新了按钮标题和标签。

完成!

答案 3 :(得分:0)

对于需要在SWIFT中执行此操作的人,方法如下:

func randomlyPositionAnswerButtons() {
    
    var posX = CGFloat()
    var posY_button1 = CGFloat()
    var posY_button2 = CGFloat()
    var posY_button3 = CGFloat()
    
   // if self.view.frame.size.height == 667 { /* ======== IPHONE 7, 7S, 8 ======== */
        
        posX = 24 // Set all buttons to start from 37
        
        posY_button1 = 310
        posY_button2 = 405
        posY_button3 = 500
        
  //  }
    
    let tab = [
        [1,2], [0,2], [0,1]
    ]
    
    let indexArray = [
        NSValue(cgRect:CGRect(x: posX, y: posY_button1, width: 350, height: 74)),
        NSValue(cgRect:CGRect(x: posX, y: posY_button2, width: 350, height: 74)),
        NSValue(cgRect:CGRect(x: posX, y: posY_button3, width: 350, height: 74))
        ] as NSMutableArray
    
    // Shuffle the array
    var p = UInt32()
    p = arc4random() % 3
    indexArray.exchangeObject(at: 0, withObjectAt: Int(p))
    indexArray.exchangeObject(at: tab[Int(p)][0], withObjectAt: tab[Int(p)][1])
    
    // Assign the buttons their new position
    optionButtonA.frame = (indexArray[0] as AnyObject).cgRectValue
    optionButtonB.frame = (indexArray[1] as AnyObject).cgRectValue
    optionButtonC.frame = (indexArray[2] as AnyObject).cgRectValue
    
}

答案 4 :(得分:-1)

如果只是在自动布局限制方面,重新排列固定按钮听起来很头疼。

为什么不要你:

  1. 创建并定位35&#34; generic&#34;故事板上的按钮,每个按钮都有自己的插座(属性名为button01button02等)或更好:使用outlet collection

  2. 为故事板上的每个按钮(0,1,2,3 ......)分配一个唯一标签。

  3. 为故事板上的所有按钮分配相同的@IBAction方法。

  4. 在运行时,确定并指定一个特定的角色&#34;随机地按下每个按钮(并相应地设置每个按钮的标签)。但要跟踪哪个按钮,例如 - dictionary

  5. 链接到所有按钮的操作将使用发件人的标记和上面提到的dictionary中包含的信息来确定哪个&#34;角色&#34;按钮被点击,并采取相应的行动。

  6. 有意义吗?

相关问题