代码之家  ›  专栏  ›  技术社区  ›  Kolin Krewinkel

滑动以显示类似tweetie的菜单

  •  1
  • Kolin Krewinkel  · 技术社区  · 14 年前

    我一直在开发一个新的应用程序,我真的希望实现一个滑动,以显示更多的选项菜单在我的应用程序内。我已经搜索过了,但似乎没有其他人成功地使它起作用(除了洛伦)。我要做的是滑动单元格,同时使用cabasication将其推到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方法仅在单元格滚动到视区后将其动画化回其正常位置。(如中所述,在导航栏或遮蔽它的对象下)最后一个关键问题是能够检测菜单是否已可见或活动,以及单元格是否已离开屏幕,将单元格滑回其原始位置,删除菜单视图,然后将另一个单元格滑出并添加菜单。

    我想成为除了洛伦之外第一个像其他人一样实现这个的人,特别是在StackOverflow上。

    我为代码格式不好而道歉。

    事先谢谢, 科林

    2 回复  |  直到 14 年前
        1
  •  6
  •   Tom Irving    14 年前

    我做得很好,你可以在这里查看项目: http://www.thermoglobalnuclearwar.com/opensource/

    它还克服了tweetie中的问题,在tweetie中,如果你不先刷其他东西,就不能刷同一个手机两次。

    如果你有任何建设性的改变,或需要任何帮助,请告诉我!

        2
  •  1
  •   Lily Ballard    14 年前

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

    你可能想调查做其他的事情,比如使用一个连接到手机上的uiswipegesturerecognizer来检测刷卡。

    我可以想到两种方法来尝试和检测细胞外的水龙头。第一种方法是将uiapplication子类化并重写 -sendEvent: . 您可以使用它来检测tap事件并关闭单元格“编辑”。这里的主要问题是给uiapplication子类关于您的单元的知识。第二种方法是在整个屏幕和 -hitTest:withEvent: 你可以测试触摸是否属于你手机上的区域。如果没有,则取消单元格“编辑”。在这两种情况下,返回nil以允许事件继续到基础视图(并且不要忘记在事件发生后删除此覆盖视图)。您可能还需要测试uievent,以确保它包含一个新的触摸,然后再解除该单元。