更新时间:2023-12-03 21:05:28
这是我为静态表格视图制作的自定义滑动,但应该适用于动态表格视图.它将允许您滑动到设置的最大 x 位置.如果用户释放它,它将动画回到原始位置.
This is a custom swipe I had made for a static table view, however should work for a dynamic table view. It will allow you to swipe to a set max x postition. If the user releases it will animate back to the original location.
我已经调整了代码,因此它应该适用于动态单元格.但尚未测试.
I've tweaked the code so it should work with a dynamic cell. But not tested yet.
添加这个本地变量
@property (nonatomic) CGFloat changeX;
只需将 pan GR 连接到情节提要并连接到此操作.
Just connect a pan GR to the storyboard and connect to this action.
- (IBAction)dragCell:(UIPanGestureRecognizer *) gestureRecognizer
{
CGPoint swipeLocation = [gestureRecognizer locationInView:self.tableView];
NSIndexPath *swipedIndexPath = [self.tableView indexPathForRowAtPoint:swipeLocation];
UITableViewCell*swipedCell = [self.tableView cellForRowAtIndexPath:swipedIndexPath];
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
self.changeX = swipeLocation.x;
}
if (gestureRecognizer.state == UIGestureRecognizerStateChanged) {
CGFloat maxRight = 100; // change this to increase distance of hit point
CGFloat newX = swipeLocation.x - self.changeX; // relative from first touch location
if (newX <= 0) newX = 0;
if (newX >= maxRight) {
newX = maxRight;
NSLog(@"Hit the point where I'll allow delete");
}
swipedCell.frame = CGRectMake(newX, swipedCell.frame.origin.y, swipedCell.frame.size.width, swipedCell.frame.size.height);
}
if (gestureRecognizer.state == UIGestureRecognizerStateEnded || gestureRecognizer.state == UIGestureRecognizerStateCancelled) {
[UIView animateWithDuration:0.5 animations:^{
swipedCell.frame = CGRectMake(0, swipedCell.frame.origin.y, swipedCell.frame.size.width, swipedCell.frame.size.height);
}
completion:^(BOOL finished) {
//
}];
}
}