繁体   English   中英

ios UIView动画使用块问题

[英]ios UIView animation using blocks issue

我正在尝试使用样式将旧样式的动画转换为块。 以下代码可以正常工作:

[UIView beginAnimations:@"ResizeAndMove" context:nil];
[UIView setAnimationDuration:3];
[UIView setAnimationDelay:0];
[UIView setAnimationBeginsFromCurrentState:YES];
selected_view.frame = targetSlot.frame;
selected_view.center = targetSlot.center;
[UIView commitAnimations];

但是,这种转换将无法正常工作:

[UIView animateWithDuration:.3f
     animations:^{
         selected_view.frame = targetSlot.frame;
         selected_view.center = targetSlot.center;
     }
 ];

它说:

 2012-10-17 12:32:50.256 myapp[311:207] *** +[UIView
 animateWithDuration:animations:]: unrecognized selector sent to class
 0x21b989c 2012-10-17 12:32:50.259 myapp[311:207] *** Terminating app
 due to uncaught exception 'NSInvalidArgumentException', reason: '***
 +[UIView animateWithDuration:animations:]: unrecognized selector sent to class 0x21b989c'

我在iPad模拟器上使用ios 4.1。 它可以编译,但总是崩溃。 无法找出问题所在。 甚至来自apple dev的简单示例:

[UIView animateWithDuration:0.2
     animations:^{view.alpha = 0.0;}
     completion:^(BOOL finished){ [view removeFromSuperview]; }];

以相同的方式工作-只是崩溃并显示相同的消息(仅添加“ completion:”)。 争论到底出了什么问题? 哪一个论点呢? 我是否需要导入一些东西以增加对块的支持? 但是它可以很好地编译...我什至可以在UIKit / UIView中看到:

...
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0); // delay = 0.0, options = 0, completion = NULL
...

您最近是否升级到xcode 4.5,因为它不再支持ios 4.1。

我唯一想到的是您正在使用的变量在块中不安全使用。 为了使它们安全,只需在初始化变量时添加__block即可。 看起来确实很奇怪!

NSTimeInterval不是浮点数。 它的两倍。 这就是为什么您的原始代码起作用的原因。

安装较新版本的XCode可以解决此问题。 在安装我当前版本的XCode之前,已使用sudo删除了它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM