2010-07-07 55 views
1

我一直在研究一個新的應用程序,並且真的希望實施一個刷卡以在我的應用程序中顯示更多選項菜單。我已經搜索和搜索,但似乎沒有人成功地使它工作(除了羅蘭)。我想要做的是刷單元格,並同時使用CABasicAnimation將其推到x:320,並添加一個子視圖下面,將有按鈕等。我使用willBeginEditing檢測滑動,以避免子類化。下面的代碼:滑動以顯示像Tweetie的菜單

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath { 

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

CABasicAnimation *theAnimation;   

theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"]; 

theAnimation.duration=0.0; 

theAnimation.repeatCount=0; 

theAnimation.autoreverses=NO; 

theAnimation.removedOnCompletion = NO; 

theAnimation.fillMode = kCAFillModeForwards; 

theAnimation.fromValue=[NSNumber numberWithFloat:0]; 

theAnimation.toValue=[NSNumber numberWithFloat:+320]; 

[cell.layer addAnimation:theAnimation forKey:@"animateLayer"]; 

CGRect frame = CGRectMake(0, 59 * indexPath.row, 320, 59); 

UIView *menu = [[[UIView alloc] initWithFrame:frame] autorelease]; 


NSString *path = [NSString stringWithFormat:@"%@%@", 

         [[NSBundle mainBundle] resourcePath], 

         @"/flick.wav"]; 



SystemSoundID soundID; 

NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO]; 

AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID); 

AudioServicesPlaySystemSound(soundID); 

self.menu.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"dots.png"]]; 

[self.view addSubview:menu]; 

[self.view sendSubviewToBack:menu]; 

} 



- (void)animationDidStop:(NSString*)animationID finished:(BOOL)finished context:(void  *)context 

{ 

    // Release 

    [self release]; 

} 


#pragma mark - Swipe Menu II 



- (void)scrollViewDidScroll:(UIScrollView *)tableView { 

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:nil]; 

    CABasicAnimation *theAnimation;   

    theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"]; 

    theAnimation.duration=0.2; 

theAnimation.repeatCount=0; 

theAnimation.autoreverses=NO; 

theAnimation.fromValue=[NSNumber numberWithFloat:320]; 

theAnimation.toValue=[NSNumber numberWithFloat:0] 

; 

[cell.layer addAnimation:theAnimation forKey:@"animateLayer"]; 

} 

的問題是向後滑動電池 - 我要收到菜單視圖以外的任何觸摸什麼時候做,而是因爲它是滾動型的,我不能。 ScrollViewdidScroll方法只會在單元格滾動視口後將單元格恢復到正常位置。 (如在NavigationBar或遮擋它的對象下)最後一個關鍵問題是能夠檢測菜單是否已經可見或活動,並且單元格已經離開屏幕,將單元格滑回原始位置,刪除菜單視圖,然後滑出另一個單元格並添加菜單。

我想成爲第一個在Loren身上實現這一點的人,因爲很多人都嘗試過,特別是在StackOverflow上。

我爲代碼中糟糕的格式道歉。

由於提前, 歌林

+0

當你點擊一個帖子上的+按鈕時,你的描述聽起來像是發生在臉書上的事情,該按鈕顯示下面的/評論按鈕。如果你還沒有看過http://github.com/facebook/three20,你可以看看。 – 2010-07-08 04:48:01

回答

6

我實際上做這項工作相當不錯,你可以看看這裏的項目:http://www.thermoglobalnuclearwar.com/opensource/

它還克服了特威特在那裏你可以不先刷別的兩次刷卡同一小區的問題。

如果您做出任何有建設性的更改或需要任何幫助,請告訴我!

+0

感謝您的代碼!我會看看它,看看我能用它做什麼! – 2010-07-07 22:42:07

+0

不是問題:) – 2010-07-07 22:54:11

+0

你介意幫我實施它到我的應用程序?我有的配置是不同的,我似乎無法得到它的工作。 – 2010-07-08 04:40:16

1

羅蘭的實現有一個致命的缺陷,這就是,如果你刷卡的單元格,然後滾動表,直到你刷卡不同的細胞則無法重新刷相同的細胞。我相信這是因爲tableview認爲單元格仍在編輯,直到您嘗試「編輯」另一個單元格爲止。

您可能想要調查其他方法,例如使用附加到單元格的UISwipeGestureRecognizer來檢測滑動。

我可以想出兩種方法來嘗試和檢測單元格外的水龍頭。第一個是對UIApplication進行子類化並覆蓋-sendEvent:。您可以使用它來檢測輕敲事件並關閉單元格「編輯」。這裏的主要問題是給UIApplication子類關於你的單元格的知識。第二種方法是在整個屏幕上拍攝自定義的UIView子類,並在-hitTest:withEvent:中測試觸摸是否屬於您的手機區域。如果沒有,請關閉單元格「編輯」。在這兩種情況下,都返回nil以允許事件進行到底層視圖(並且不要忘記在事件發生後刪除此覆蓋視圖)。您可能還想要測試UIEvent,以確保它在解除單元格之前包含新的觸摸。

+0

感謝您的回答並解釋它!我將研究湯姆的代碼,因爲它似乎修復了Tweetie/Twitter的問題。 – 2010-07-07 22:35:29