使用uiswipegesturerecognizer快速更新uitableviewcell的框架

时间:2015-11-11 18:03:17

标签: ios swift xcode6 uigesturerecognizer uipangesturerecognizer

我在我的tableviewcell中添加了UISwipeGestureRecogniser。现在我想为我的tableviewcell添加一个实时幻灯片效果。如果我目前在细胞上滑动没有任何反应。细胞应该可以刷卡。

我的代码:

var originalCenter = CGPoint()

var rightSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
rightSwipe.direction = .Right
self.addGestureRecognizer(rightSwipe)

func handleSwipes(recogniser:UISwipeGestureRecognizer) {

    let location : CGPoint = recogniser.locationInView(self)

    if (recogniser.direction == .Right) {

        if recogniser.state == .Began {

            // when the gesture begins, record the current center location
            originalCenter = center
        }

        if recogniser.state == .Changed {

            let translation = recogniser.locationInView(self)
            center = CGPointMake(originalCenter.x + translation.x, originalCenter.y)
        }

        if recogniser.state == .Ended {

        }

        println("Swipe Right")
    }
}

2 个答案:

答案 0 :(得分:0)

您可以通过继承UIPanGestureRecognizer轻松完成您想要做的事情。但不要。而不是添加手势识别器,将您的单元格转换为UIScrollView。现在,您可以设置其contentSize和边界原点,以便它只能从左向右滚动。这里的优点是您使用的内置接口类可以自动拖动。您可以使用委托方法跟踪用户在需要时执行的操作。

答案 1 :(得分:0)

我在之前的项目中实现了自定义单元格滑动...您需要为这些内容设置动画,使其看起来与在通知绘制消息滑动中工作的滑动完全相同。我可以在Objective-C中给你片段。你应该能够轻松地将其翻译成快速;)

-(void)handleLeftCellSwipe:(UISwipeGestureRecognizer *)recognizer{
     CGPoint location = [recognizer locationInView:self.localTableView];
     NSIndexPath *swipedIndexPath = [self.localTableView indexPathForRowAtPoint:location];
     UITableViewCell *swipedCell  = [self.localTableView cellForRowAtIndexPath:swipedIndexPath];
     if(swipedCell){
          [UIView animateWithDuration:0.3 delay:0.0 usingSpringWithDamping:0.7 initialSpringVelocity:1.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
          [swipedCell.contentView setFrame:CGRectMake(swipedCell.contentView.frame.origin.x - 40, swipedCell.contentView.frame.origin.y, swipedCell.contentView.frame.size.width, swipedCell.contentView.frame.size.height)];
          } completion:^(BOOL finished){
              // Do whatever you need to do after animation completes
          }];
     }
}