1

我一直在开发一个新应用程序,并且真的希望实现滑动以在我的应用程序中显示更多选项菜单。我已经搜索和搜索,但似乎没有其他人成功地使它工作(除了 Loren)。我想要做的是滑动单元格,同时使用 CABasicAnimation 将其推送到 x: 320,并在此下方添加一个子视图,该子视图将具有按钮等。我正在使用 willBeginEditing 来检测滑动以避免子类化. 这是代码:

- (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 上。

我为代码中的不良格式道歉。

在此先感谢,科林

4

2 回答 2

6

我实际上已经很好地完成了这项工作,您可以在这里查看该项目:http ://www.thermoglobalnuclearwar.com/opensource/

它还解决了 Tweetie 中的问题,即您不能在不先刷其他东西的情况下刷同一个单元格两次。

如果您做出任何建设性的更改,或需要任何帮助,请告诉我!

于 2010-07-07T22:11:30.090 回答
1

Loren 的实现有一个致命的缺陷,如果你在一个单元格上滑动,然后滚动表格,你不能重新滑动同一个单元格,直到你滑动另一个单元格。我相信这是因为 tableview 认为该单元格仍在被编辑,直到您尝试“编辑”另一个单元格。

您可能想要调查做其他事情,例如使用附加到单元格的 UISwipeGestureRecognizer 来检测滑动。

我也可以想到两种方法来尝试检测细胞外的水龙头。第一个是继承 UIApplication 并覆盖-sendEvent:。您可以使用它来检测点击事件并关闭单元格“编辑”。这里的主要问题是为 UIApplication 子类提供有关您的单元格的知识。第二种方法是在整个屏幕上添加一个自定义 UIView 子类,-hitTest:withEvent:您可以在其中进行测试以查看触摸是否属于您的单元格上方的区域。如果没有,请关闭单元格“编辑”。在这两种情况下,返回 nil 以允许事件继续到底层视图(并且不要忘记在事件发生后删除此覆盖视图)。您可能还想测试 UIEvent 以确保它在关闭单元格之前包含新的触摸。

于 2010-07-07T22:08:32.917 回答