gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 10:14:14 26 4
gpt4 key购买 nike

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

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

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

最佳答案

我不会像您发布的那样使用编辑委托(delegate)。

将单元格内容放入 UIViewUIScrollView 中,然后设置要显示的 UIScrollView 背后的信息。

然后在滚动 delegate 上,scrollViewDidEndDragging,将 contentOffset 设置回 0 动画。

编辑:平移手势示例

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

注意:这是swift 2,但应该很容易翻译,我很久以前发布swift 2时就做了这个,它可以重构

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()
}
}

关于ios - 如何通过滑动 UITableViewCell 显示隐藏文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43628672/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com