4

此代码有效(带有动画的 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;
}

这个崩溃(带有动画的开关语句):

// 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 语句中使用动画块?!?

4

1 回答 1

6

你应该能够 :)

尝试{ }像这样添加您的案例:

case UIDeviceOrientationPortrait: {
    [UIView animateWithDuration:0.5f animations:^void{
        [_distanceView setTransform:CGAffineTransformMakeRotation(0.0)];
    }];
    break;
}
于 2012-04-20T16:38:45.193 回答