滑动显示“额外"时的 UITableView 子视图 [英] UITableView Subview when swipe to show "extras"
问题描述
我正在尝试为我正在创建的应用程序实现一个附加"菜单.本质上,我有一个包含多种单元格类型的 UITableView,当用户在单元格上向左滑动时,我希望能够动态地向他们展示他们可以做的额外".例如,他们可以在帖子上滑动,然后看到诸如分享之类的选项,例如……等.如果您在 ios 上为 Reddit 使用了 Alien Blue 应用程序,那么这就是我想要做的......
I am trying to implement a "extras" menu for a app I am creating. Essentially I have a UITableView with multiple types of cells, when a user swipes to the left on the cell I want to be able to dynamically show them "extras" they can do. For example on a post they can swipe over and then see options like share, like ...ect. If you have used the Alien Blue app on ios for Reddit then that is what I am looking to do...
到目前为止,我的滑动识别器可以正常工作,它可以正确识别单元格类型……我只是不知道如何开始子视图编程……
So far I have the swipe recognizer working and it identifies the type of cell properly... I just don't know how to start the subview programming...
我是只是让每个单元格变大并隐藏额外内容直到滑动,还是我在移动时动态地向每个单元格添加视图...
Do I just make every cell larger and hide the extras until swipe or do I dynamically add views to each cell as I go...
感谢您的任何建议或帮助
Thank for any advice or help
如果需要,我可以提供代码......
I can provide code if needed....
A
推荐答案
我认为你是对的,至少,我在手机上是这样做的.
I think you are right, and at least, that's how I did on my cell.
唯一的区别是我没有使单元格宽度大于窗口宽度.我在常规 cell.contentview 下添加了一个带有额外内容的视图,在我的例子中它是一个删除按钮.然后当单元格检测到从右向左滑动时,它会调用一个函数来处理这个手势.
The only difference is i am not making the cell width large than window width. I add a view with the extra stuff, in my case it's a delete button, under the regular cell.contentview. then when the cell detects a swipe from right to left, it will call a function to handle the gesture.
因为我希望用户在将单元格向左拖动时看到删除按钮,并在将单元格平移足够远时显示该按钮
Since i want the user to see that delete button when drag the cell to the left and show that button entirely when they pan the cell far enough
这是我如何处理平移手势的片段,
here is a snippet for how i handle the pan gesture,
CGPoint _originalTouchPoint;
- (void)handlePan:(UIPanGestureRecognizer *)recognizer
{
switch (recognizer.state) {
case UIGestureRecognizerStateBegan:
//save the original touch point when pan starts
_originalTouchPoint = [recognizer locationInView:_tableView];
break;
case UIGestureRecognizerStateChanged:
[self handlePan:recognizer];
break;
case UIGestureRecognizerStateEnded:
[self panEnded:recognizer];
break;
default:
break;
}
}
- (void)handlePan:(UIPanGestureRecognizer *)recognizer
{
MyCell *ourCell = (MyCell *)recognizer.view;
CGPoint touchPoint = [recognizer locationInView:_tableView];
float movedDistance = (_originalTouchPoint.x - touchPoint.x);
if(movedDistance < 0)
movedDistance = 0;
float maxX = ourCell.deleteButton.frame.size.width;
float ourTranslation = MAX(-1 * ourCell.deleteButton.frame.size.width, -1 * movedDistance);
ourCell.contentView.transform = CGAffineTransformMakeTranslation(ourTranslation, 0.0f);
// i only show the button when user pan all the way though
_shouldDeleteButtonShow = movedDistance / maxX >= 1.0;
}
- (void)panEnded:(UIPanGestureRecognizer *)recognizer
{
MyCell *ourCell = (MyCell *)recognizer.view;
if (_shouldDeleteButtonShow)
{
//do whatever you want in this case
}
else
{
//move the cell back to normal
[UIView animateWithDuration:0.3
animations:^
{
ourCell.contentView.transform = CGAffineTransformIdentity;
}];
}
}
这些是我的代码,可能不像你想要的那样工作,但希望这能让你大致了解如何做到这一点
Those are my codes, maybe not working exactly like you want, but hopefully, that gave you a rough idea about how to do this
这篇关于滑动显示“额外"时的 UITableView 子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!