如何使我的自定义按钮沿x轴移动

时间:2014-03-28 23:00:34

标签: objective-c

为什么当我尝试为自定义杯子按钮设置动画时,即使我将y轴固定,它们也会沿着x轴和y轴移动?我正在使用上面的数组来抓取一个随机按钮并将其设置为randomCup1,依此类推。然后,我想使用randomCup1_cupButton1移至该位置,其余部分则相同。

当我运行代码时,杯子会随机移动,但它们的移动非常奇怪。任何提示将不胜感激!

(void)shuffleCups{
    _numberOfTimesShuffled++;
    if(_numberOfTimesShuffled > 4){
        _cupButton1.canBeClicked = true;
        _cupButton2.canBeClicked = true;
        _cupButton3.canBeClicked = true;
        return;
    }

    else{
        NSMutableArray *cupButtonsArray = [[NSMutableArray alloc] init];
        [cupButtonsArray addObject:_cupButton1];
        [cupButtonsArray addObject:_cupButton2];
        [cupButtonsArray addObject:_cupButton3];

        int random1 =  arc4random() % [cupButtonsArray count];
        CupButton *randomCup1 = [cupButtonsArray objectAtIndex:random1];
        [cupButtonsArray removeObjectAtIndex:random1];

        NSLog(@"This is random 1 %d", random1);

        int random2 = arc4random() % [cupButtonsArray count];
        CupButton *randomCup2 = [cupButtonsArray objectAtIndex:random2];
        [cupButtonsArray removeObjectAtIndex:random2];

        NSLog(@"This is random 2 %d", random2);

        CupButton *randomCup3 = [cupButtonsArray objectAtIndex:0];

        [UIView animateWithDuration:1.0
                              delay:0.1
                            options:UIViewAnimationOptionBeginFromCurrentState
                         animations:^{
                             [_cupButton1 setCenter:CGPointMake(randomCup2.frame.origin.x, _cupButton1.frame.origin.y)];

                         }completion:^(BOOL finshed){
                             //[self shuffleCups];
                         }];

        [UIView animateWithDuration:1.0
                              delay:0.1
                            options:UIViewAnimationOptionBeginFromCurrentState
                         animations:^{
                             [_cupButton2 setCenter:CGPointMake(randomCup3.frame.origin.x, _cupButton1.frame.origin.y)];

                         }completion:^(BOOL finshed){
                             //[self shuffleCups];
                         }];

        [UIView animateWithDuration:1.0
                              delay:0.1
                            options:UIViewAnimationOptionBeginFromCurrentState
                         animations:^{
                             [_cupButton3 setCenter:CGPointMake(randomCup1.frame.origin.x, _cupButton1.frame.origin.y)];

                         }completion:^(BOOL finshed){
                             [self shuffleCups];
                         }];
    }
}

1 个答案:

答案 0 :(得分:0)

您正在将按钮中心的y值设置为视图左上角的原点y值。如果您不希望它垂直移动,您应该将y值设置为_cupButton1.center.y

此外,您的版本中存在逻辑错误。 _cupButtonXrandomCupX都引用了相同的按钮。这意味着您可以移动一个按钮,然后使用另一个按钮进行参考。例如,如果将_cupButton1分配给_randomCup2,并将_cupButton2分配给_randomCup1。拳头_cupButton1将移至_cupButton2所在的位置。然后_cupButton2将保留在同一位置,因为那是_cupButton1现在的位置。

以下是代码的清洁程序(至少在我看来),它没有逻辑错误:

- (void)shuffleCups
{
    _numberOfTimesShuffled++;
    if (_numberOfTimesShuffled > 4) {
        _cupButton1.canBeClicked = true;
        _cupButton2.canBeClicked = true;
        _cupButton3.canBeClicked = true;
        return;
    }
    else {
        // You can create arrays with the short-hand @[]
        // (the mutable copy surrounding it makes it mutable)
        NSMutableArray *cupButtonsArray = [@[
            _cupButton1,
            _cupButton2,
            _cupButton3
        ] mutableCopy];

        [UIView animateWithDuration:1.0
            delay:0.1
            options:UIViewAnimationOptionBeginFromCurrentState
            animations:^{
                // Storing the points to move to instead of a reference
                // to the buttons themselves
                CGPoint newPoints[3];
                // Use NSUInteger instead of int because that is what NSArray
                // uses as its index type
                for (NSUInteger i = 0; i < 3; i++) {
                    NSUInteger randomIndex = arc4random() % cupButtonsArray.count;
                    // You can use brackets as a short-hand on NSArrays instead
                    // of objectAtIndex:
                    newPoints[i] = [cupButtonsArray[randomIndex] center];
                    [cupButtonsArray removeObjectAtIndex:randomIndex];
                }

                // You can animate multiple changes all in one animtation block
                [_cupButton1 setCenter:newPoints[0]];
                [_cupButton2 setCenter:newPoints[1]];
                [_cupButton3 setCenter:newPoints[2]];
             }
             completion:nil
        ];
    }
}

不是一个巨大的变化,但是当应用于整个代码库时,清理一小段代码会产生很大的影响。