奇怪的UILabel和UIButton行为

时间:2014-10-28 20:05:12

标签: ios objective-c iphone uibutton

我有一个名为change shape的函数:

- (void)changeShape {

    NSLog(@"**** CHANGE SHAPE ****");

    /* Set previous shape to current shape */
    self.previousShape = self.currentShape;

    /* Transition old shape out of view */
    [UIView animateWithDuration:.4 animations:^(void){
        [self.shapeButton setCenter:CGPointMake(self.view.frame.size.width+200, self.view.frame.size.height/2)];
    } completion:^(BOOL finished){
        if (finished) {

            /* Set new current shape */
            self.currentShape = [NSString stringWithFormat:@"%@",self.shapes[(arc4random() % [self.shapes count])]];
            [self.shapeButton setImage:[UIImage imageNamed:self.currentShape] forState:UIControlStateNormal];

            /* Transition new shape into view */
            [self.shapeButton setCenter:CGPointMake(-200, self.view.frame.size.height/2)];
            [UIView animateWithDuration:.3 animations:^(void){
                [self.shapeButton setCenter:CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2)];
            } completion:^(BOOL completed){
                if (completed) {
                    [self changeScore];
                }
            }];
        }
    }];
}

- (void)changeScore {
    self.score++;
    [self.scoreLabel setText:[NSString stringWithFormat:@"%i",self.score]];
}

此功能可将UIButton移出屏幕,然后将其动画回屏幕。在最终动画结束时,我想增加分数并在屏幕上更改UILabel。出于某种原因,UIButton上的图像只会在UILabel更改后更改。有人知道为什么会这样吗?

1 个答案:

答案 0 :(得分:-1)

在应用了位置更改和新图像后调用[self.shapeButton layoutIfNeeded]