gpt4 book ai didi

ios - 选择时向表格单元格添加滑动 View

转载 作者:可可西里 更新时间:2023-11-01 05:31:33 25 4
gpt4 key购买 nike

我有一个动态 TableView ,其中的单元格从数据库中填充。选择单元格后,用户应该可以选择其他几个选项。我知道如何在选择单元格时推送另一个 View ,但我不喜欢这种图形方式。例如,如果同一个单元格可以翻转并显示选项(然后翻转回来)可能会更好,可能会滑动。或者整个单元格可以滑出屏幕以显示选项,或者另一个 View 可以从单元格向下滑动然后向上滑动。

以下哪种解决方案最容易实现?谁能指出我正确的方向?我当然不需要代码,我是来学习的,我只需要知道要看什么。直到现在,我已经阅读了一些关于子类化 UITableViewCell 的内容,但是老实说,我还没有了解它。任何输入将不胜感激。

最佳答案

您将使用带有前景和背景 View 以及 UIPanGestureRecognizer 的 UITableViewCell 子类。此识别器将触发滑动并处理前景 View 的移动。

也就是说,您会在这里找到一个实现:https://github.com/spilliams/sparrowlike

重要的部分:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CustomCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

// Configure the cell...
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[panGestureRecognizer setDelegate:self];
[cell addGestureRecognizer:panGestureRecognizer];

return cell;
}

#pragma mark - Gesture recognizer delegate
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)panGestureRecognizer
{
CustomCell *cell = (CustomCell *)[panGestureRecognizer view];
CGPoint translation = [panGestureRecognizer translationInView:[cell superview] ];
return (fabs(translation.x) / fabs(translation.y) > 1) ? YES : NO;
}

#pragma mark - Gesture handlers

-(void)handlePan:(UIPanGestureRecognizer *)panGestureRecognizer
{
float threshold = (PAN_OPEN_X+PAN_CLOSED_X)/2.0;
float vX = 0.0;
float compare;
NSIndexPath *indexPath = [self.tableView indexPathForCell:(CustomCell *)[panGestureRecognizer view] ];
UIView *view = ((CustomCell *)panGestureRecognizer.view).frontView;

switch ([panGestureRecognizer state]) {
case UIGestureRecognizerStateBegan:
if (self.openCellIndexPath.section != indexPath.section || self.openCellIndexPath.row != indexPath.row) {
[self snapView:((CustomCell *)[self.tableView cellForRowAtIndexPath:self.openCellIndexPath]).frontView toX:PAN_CLOSED_X animated:YES];
[self setOpenCellIndexPath:nil];
[self setOpenCellLastTX:0];
}
break;
case UIGestureRecognizerStateEnded:
vX = (FAST_ANIMATION_DURATION/2.0)*[panGestureRecognizer velocityInView:self.view].x;
compare = view.transform.tx + vX;
if (compare > threshold) {
[self snapView:view toX:PAN_CLOSED_X animated:YES];
[self setOpenCellIndexPath:nil];
[self setOpenCellLastTX:0];
} else {
[self snapView:view toX:PAN_OPEN_X animated:YES];
[self setOpenCellIndexPath:[self.tableView indexPathForCell:(CustomCell *)panGestureRecognizer.view] ];
[self setOpenCellLastTX:view.transform.tx];
}
break;
case UIGestureRecognizerStateChanged:
compare = self.openCellLastTX+[panGestureRecognizer translationInView:self.view].x;
if (compare > PAN_CLOSED_X)
compare = PAN_CLOSED_X;
else if (compare < PAN_OPEN_X)
compare = PAN_OPEN_X;
[view setTransform:CGAffineTransformMakeTranslation(compare, 0)];
break;
default:
break;
}
}
-(void)snapView:(UIView *)view toX:(float)x animated:(BOOL)animated
{
if (animated) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:FAST_ANIMATION_DURATION];
}

[view setTransform:CGAffineTransformMakeTranslation(x, 0)];

if (animated) {
[UIView commitAnimations];
}
}

关于ios - 选择时向表格单元格添加滑动 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10389095/

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