如何通过滑动UITableViewCell来显示隐藏文本

时间:2017-04-26 08:22:39

标签: ios swift uitableview animation

我正在尝试在UITableView中的单元格左侧放置一个日期字段,当用户从左向右滑动单元格时,该字段会显示。释放后,单元格应弹回原位,隐藏日期字段。有谁知道如何做到这一点?是否在

处理
func tableView(_ tableView: UITableView, editActionsForRowAt: IndexPath) -> [UITableViewRowAction]? {

或在自定义单元格中以某种方式定义?

1 个答案:

答案 0 :(得分:4)

我不会在您发布时使用编辑委托。

将单元格内容放在UIView UIScrollView中,然后设置您希望显示的UIScrollView背后的信息。

然后在滚动delegatescrollViewDidEndDragging上,将contentOffset设置为0动画。

编辑:平移手势示例

如果您希望使用最简单的路线,请使用与您的单元格大小相同的customContentView设置您的单元格,向其中添加平移手势,然后设置您想要的设置和按钮,在下面的示例中,每侧都有一个按钮。

注意:这是快速的2,但是应该很容易翻译,我在swift 2发布的时候做了一个longgg,它可以用重构

var savedX = 0 as CGFloat
var buttonWidth = 60 as CGFloat
var open = false

func panGestureHandler(gesture: UIPanGestureRecognizer) {

    if gesture.state == .Changed {
        let translation = gesture.translationInView(tagView)

        let difference = -translation.x

        if difference > 0 && !allowScrollRight {
            return
        }

        let newConstant = savedX + difference

        tagViewCenterXConstraint.constant = newConstant

        let alpha = abs(tagViewCenterXConstraint.constant) / buttonWidth

        deleteButton.alpha = min(alpha, 1)
        followButton.alpha = min(alpha, 1)

        if let action = swipe {
            action(self)
        }
    }

    if gesture.state == .Ended {
        let translation = gesture.translationInView(self)

        let trans = fabs(translation.x)

        open = !open && trans > buttonWidth

        if open {
            if(translation.x > 0){
                resetRight(true)
            } else {
                if allowScrollRight {
                    resetLeft(true)
                }
            }
        } else {
            resetView(true){

            }
        }
    }
}

 func resetLeft (animated : Bool) {
        tagViewCenterXConstraint.constant = self.buttonWidth
        savedX = self.buttonWidth


    if animated {
        UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.7, options: [UIViewAnimationOptions.CurveEaseIn, UIViewAnimationOptions.BeginFromCurrentState], animations: { () -> Void in
            self.tagView.layoutIfNeeded()
            self.leftView.layoutIfNeeded()
            self.rightView.layoutIfNeeded()

            }, completion: { (finished) -> Void in
        })
    }
}

func resetRight (animated : Bool) {
    tagViewCenterXConstraint.constant = -self.buttonWidth
    savedX = -self.buttonWidth

    if animated {
        UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.7, options: [UIViewAnimationOptions.CurveEaseIn, UIViewAnimationOptions.BeginFromCurrentState], animations: { () -> Void in
            self.tagView.layoutIfNeeded()
            self.leftView.layoutIfNeeded()
            self.rightView.layoutIfNeeded()

            }, completion: { (finished) -> Void in
        })
    }
}

func resetView (animated : Bool, completion: () -> Void ) {
    tagViewCenterXConstraint.constant = 0
    savedX = 0
    open = false

    if animated {
        UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.7, options: [UIViewAnimationOptions.CurveEaseIn, UIViewAnimationOptions.BeginFromCurrentState], animations: { () -> Void in
            self.tagView.layoutIfNeeded()
            self.leftView.layoutIfNeeded()
            self.rightView.layoutIfNeeded()

            }, completion: { (finished) -> Void in
                completion()
        })
    } else {
        completion()
    }
}