使用switch语句(动画)块时发生崩溃 [英] Crash when using (animation) blocks in switch statements

查看:385
本文介绍了使用switch语句(动画)块时发生崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这code工作(if语句与动画):

This code works (if-statement with animations):

// 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语句无动画):

This also works (switch-statement without animations):

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

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

    default:
        break;
}

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

This one crashes (switch-statement with animation):

// 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语句?!?

Why can't I use animation blocks in a switch statement?!?

推荐答案

您应该能够:)

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

Try adding { } around your cases like this :

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

这篇关于使用switch语句(动画)块时发生崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆