Objective c 如何使自定义按钮沿x轴移动

Objective c 如何使自定义按钮沿x轴移动,objective-c,Objective C,为什么当我尝试设置自定义杯子按钮的动画时,它们会沿着x轴和y轴移动,即使我已经固定了y轴?我使用上面的数组抓取一个随机按钮并将其设置为randomCup1,依此类推。然后,我想使用randomCup1将\u cupButton1移动到该位置,其余位置也一样 当我运行代码时,杯子会移动,但移动非常奇怪。任何提示都将不胜感激 (void)shuffleCups{ _numberOfTimesShuffled++; if(_numberOfTimesShuffled > 4){

为什么当我尝试设置自定义杯子按钮的动画时,它们会沿着x轴和y轴移动,即使我已经固定了y轴?我使用上面的数组抓取一个随机按钮并将其设置为
randomCup1
,依此类推。然后,我想使用
randomCup1
\u 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];
                         }];
    }
}

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

此外,您的版本中存在逻辑错误。
\u cupButtonX
randomCupX
都引用了相同的按钮。这意味着您可以移动一个按钮,然后在以后与另一个按钮一起使用它作为参考。例如,如果将
\u cupButton1
分配给
\u randomCup2
\u cupButton2
分配给
\u randomCup1
。首先,
\u cupButton1
将被移动到
\u cupButton2
所在的位置。然后,
\u cupButton2
将保持在同一个位置,因为那就是
\u 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
        ];
    }
}
-(无效)shuffleCups
{
_numberoftimeshuffled++;
如果(_numberOfTimesShuffled>4){
_cupButton1.canBeClicked=true;
_cupButton2.canBeClicked=true;
_cupButton3.canBeClicked=true;
返回;
}
否则{
//您可以使用缩写@[]创建数组
//(围绕它的可变副本使其可变)
NSMutableArray*CupbuttonArray=[@[
_杯形按钮1,
_按钮2,
_杯形按钮3
]可变拷贝];
[UIView animateWithDuration:1.0
延迟:0.1
选项:UIViewAnimationOptionBeginFromCurrentState
动画:^{
//存储要移动到的点而不是参考点
//按钮本身
CGPoint newPoints[3];
//使用NSUTEGER而不是int,因为这是NSArray需要的
//用作其索引类型
对于(整数i=0;i<3;i++){
NSUTEGER randomIndex=arc4random()%cupButtonsArray.count;
//你可以用括号来代替nsarray
//objectAtIndex的定义:
newPoints[i]=[cupButtonsArray[randomIndex]center];
[cupButtonsArray removeObjectAtIndex:randomIndex];
}
//可以在一个动画块中设置多个更改的动画
[_cupButton1设置中心:新点[0]];
[_cupButton2设置中心:新点[1]];
[_cupButton3设置中心:新点[2]];
}
完成日期:无
];
}
}

这不是一个很大的改变,但是当应用到整个代码库时,清理少量代码会产生很大的影响。

哇,是的,这很有效,谢谢!因此,如果我想让杯子准确地移动到随机杯子所在的位置,我还需要制作randomCup.center.x?或者我需要做什么,这样我的杯子就会移动到新的随机位置?我的逻辑似乎正确吗?谢谢你的帮助。你可以做很多事情来清理你的代码,但是逻辑似乎是合理的。我会用一个清理过的代码更新我上面的答案,这样你就可以知道清理过的代码是什么样子了。我带了一点我的个人风格,但你明白了。此外,我确实想到了在我的编辑中描述的原始代码的逻辑问题。运行您所做的,它工作完美,令人印象深刻。请您解释一下如何设置[u cupButton1 setCenter:newPoints[0];当它通常要求两个参数时,只使用一个点。还有[cupButtonsArray[randomIndex]center]正在做什么。我认为这是抓住奖杯的位置,并将其储存在新的积分中。