在我的iPhone應用程序中,我有一個UIScrollView自定義內容視圖。在某些點上,一個自定義的彈出視圖會動態地添加到內容中,同時縮放動畫出現並消失。我使用基於塊的動畫與調用完成處理程序[自removeFromSuperView]如下:UI凍結動畫去除UIScrollView子視圖
- (void)dismissPopup {
CGRect rect = self.frame;
rect.origin.y += rect.size.height/2 * (pointingDown ? 1 : -1);
[UIView animateWithDuration:kZoomDuration
animations:^{
self.frame = rect;
self.transform = CGAffineTransformMakeScale(0.01, 0.01);
}
completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
晴這個工作,但現在又跟隨這個動畫的UI完全鎖定了,不再響應任何觸摸。我非常肯定這個動畫是問題的來源,因爲動畫代碼已經註釋掉了,我還沒有能夠重現它。與調試器打破,所有三個線程似乎被閒置:
Thread-1
#0 0x32fd1c98 in mach_msg_trap
#1 0x32fd3d6a in mach_msg
#2 0x34432c3e in __CFRunLoopServiceMachPort
#3 0x344324c8 in __CFRunLoopRun
#4 0x34432276 in CFRunLoopRunSpecific
#5 0x3443217e in CFRunLoopRunInMode
#6 0x3026b5f2 in GSEventRunModal
#7 0x3026b69e in GSEventRun
#8 0x31ad0122 in -[UIApplication _run]
#9 0x31ace12e in UIApplicationMain
#10 0x00002b06 in main at main.m:14
Thread-2
#0 0x32ffe330 in kevent
#1 0x330a7b74 in _dispatch_mgr_invoke
#2 0x330a75c4 in _dispatch_queue_invoke
#3 0x330a7764 in _dispatch_worker_thread2
#4 0x3304b680 in _pthread_wqthread
Thread-3
#0 0x32fd1c98 in mach_msg_trap
#1 0x32fd3d6a in mach_msg
#2 0x34432c3e in __CFRunLoopServiceMachPort
#3 0x344324c8 in __CFRunLoopRun
#4 0x34432276 in CFRunLoopRunSpecific
#5 0x3443217e in CFRunLoopRunInMode
#6 0x34b524e8 in RunWebThread
#7 0x3304b284 in _pthread_start
儀器顯示我的應用程序採取任何CPU時間,所以它是某種僵局。任何人都可以闡明這種行爲,以及如何避免它?
更新:我已經做了一些更多挖掘並使用傳統的動畫API,這個錯誤似乎也消失了。這可能是新動畫框架中的錯誤嗎?
- (void)dismissPopup {
CGRect rect = self.frame;
rect.origin.y += rect.size.height/2 * (pointingDown ? 1 : -1);
[UIView beginAnimations:@"dismissPopup" context:nil];
[UIView setAnimationDuration:kZoomDuration];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
self.frame = rect;
self.transform = CGAffineTransformMakeScale(0.01, 0.01);
[UIView commitAnimations];
}
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
[self removeFromSuperview];
}
是的,你必須做在主線程上所有的UI操作。上面的-dismissPopup方法在主線程上調用。 – 2010-09-30 13:53:02