[英]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.