這是previous question(由Michael Frederick精心解決)的後續操作。在iOS4中,使用NSRunLoop運行的NSTimer會禁用觸摸/手勢輸入
我目前正在iOS5設備和iOS4設備之間進行調試。基本上我有一個UIImage,它是動畫刷卡指示用戶刷卡繼續。動畫代碼如下:
[UIView animateWithDuration:1.0 animations:^ {
self.finger.alpha = 1.0;
}completion:^(BOOL finished) {
CGAffineTransform originalTransform = self.finger.transform;
[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationCurveEaseInOut animations:^ {
self.finger.transform = CGAffineTransformMakeTranslation(-200, 0);
self.finger.alpha = 0.0;
}completion:^(BOOL finished) {
self.finger.transform = originalTransform;
NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(repeatSwipeAnimation1) userInfo:nil repeats:YES];
self.swipeTimerForFinger1 = timer;
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:self.swipeTimerForFinger1 forMode:NSDefaultRunLoopMode];
[self.swipeTimerForFinger1 fire];
}];
}];
和定時器選擇:
-(void)repeatSwipeAnimation1 {
[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationCurveEaseIn animations:^ {
self.finger.alpha = 1.0;
}completion:^(BOOL finished) {
CGAffineTransform originalTransform = self.finger.transform;
[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationCurveEaseInOut animations:^ {
self.finger.transform = CGAffineTransformMakeTranslation(-200, 0);
self.finger.alpha = 0.0;
}completion:^(BOOL finished) {
self.finger.transform = originalTransform;
}];
}];
}
這工作完全正常。但是,在iOS4上,滑動手勢似乎被禁用;我無法在iOS5上刷卡繼續。我唯一的理論是讓計時器在主線程上運行以某種方式禁用手勢識別器(我認爲它也是在主線程上感應觸摸)。我唯一的問題是,我試圖把這個動畫的背景如下:
[UIView animateWithDuration:1.0 animations:^ {
self.finger.alpha = 1.0;
}completion:^(BOOL finished) {
CGAffineTransform originalTransform = self.finger.transform;
[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationCurveEaseInOut animations:^ {
self.finger.transform = CGAffineTransformMakeTranslation(-200, 0);
self.finger.alpha = 0.0;
}completion:^(BOOL finished) {
self.finger.transform = originalTransform;
[NSThread detachNewThreadSelector:@selector(goToNewThread) toTarget:self withObject:nil];
}];
}];
和新的線程選擇:
-(void)goToNewThread {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(repeatSwipeAnimation1) userInfo:nil repeats:YES];
self.swipeTimerForFinger1 = timer;
[[NSRunLoop currentRunLoop] addTimer:self.swipeTimerForFinger1 forMode:NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] run];
[self.swipeTimerForFinger1 fire];
[pool release];
}
動畫是到位,但再一次,它似乎禁用任何滑動手勢識別。我對SO,O'Reilly資料以及Apple文檔進行了仔細研究,並且一直無法找到解決方案。感謝您的時間。
你試過了'UIView'動畫選項'UIViewAnimationOptionAllowUserInteraction'? – NJones
NJones!謝謝!這就是訣竅!雖然,一個簡單的問題是,無論如何要在動畫中有多個選項? – Max
很高興提供幫助。是的,你可以使用多個。我已經添加了一個答案來顯示如何。 – NJones