使用在switch语句(动画)的块时崩溃(Crash when using (animation)

2019-06-23 19:29发布

此代码(if语句与动画):

// works
if (_camOrientation == UIDeviceOrientationPortrait) {
    [UIView animateWithDuration:0.5f animations:^(void){
        [_distanceView setTransform:CGAffineTransformMakeRotation(degreesToRadian(0.0))];
    }];
} else if (_camOrientation == UIDeviceOrientationLandscapeLeft) {
    [UIView animateWithDuration:0.5f animations:^(void){
        [_distanceView setTransform:CGAffineTransformMakeRotation(degreesToRadian(90.0))];
    }];
}

这也适用(switch语句无动画):

// works
switch (_camOrientation) {
    case UIDeviceOrientationPortrait:
        [_distanceView setTransform:CGAffineTransformMakeRotation(degreesToRadian(0.0))];
        break;

    case UIDeviceOrientationLandscapeLeft:
        [_distanceView setTransform:CGAffineTransformMakeRotation(degreesToRadian(90.0))];
        break;

    default:
        break;
}

这一次崩溃(switch语句与动画):

// crashes
switch (_camOrientation) {
    case UIDeviceOrientationPortrait:
        [UIView animateWithDuration:0.5f animations:^(void){
            [_distanceView setTransform:CGAffineTransformMakeRotation(degreesToRadian(0.0))];
        }];
        break;

    case UIDeviceOrientationLandscapeLeft:
        [UIView animateWithDuration:0.5f animations:^(void){
            [_distanceView setTransform:CGAffineTransformMakeRotation(degreesToRadian(90.0))];
        }];
        break;

    default:
        break;
}

为什么我不能用动画块switch语句?!?

Answer 1:

你应该能够 :)

尝试添加{ }在你的情况是这样的:

case UIDeviceOrientationPortrait: {
    [UIView animateWithDuration:0.5f animations:^void{
        [_distanceView setTransform:CGAffineTransformMakeRotation(0.0)];
    }];
    break;
}


文章来源: Crash when using (animation) blocks in switch statements