ios - 使用平移移动将部分 Table View Cell 滑开

标签 ios objective-c cocoa-touch uitableview

我有一个表格,其中包含 NIB 文件中的自定义单元格。结果应该是这样的,我要捕获单元格并将其滑出屏幕。注:只是细胞的一部分。其他部分将保留。我在单元格上运行平移运动时遇到问题。 NSlog 正确记录了移动,但我无法随单元移动。我的 cell.m 看起来像这样:

#import "MIKETableViewCell.h"

static NSString *CellTableIdentifier = @"MIKETableViewCell";

@implementation MIKETableViewCell

@synthesize timeLabel = _timeLabel;
@synthesize priceLabel = _priceLabel;
@synthesize infoLabel = _infoLabel;


- (void)awakeFromNib
{
    // Initialization code
}


-(void)layoutSubviews
{
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognizer)];

    [self addGestureRecognizer:panGesture];
}


-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    self = [super initWithStyle:style reuseIdentifier:CellTableIdentifier];

    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

   // Configure the view for the selected state
}

-(void)panGestureRecognizer
{

    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognizer)];
    NSLog(@"Panned!");

    CGPoint translation = [panGesture translationInView:self];
    panGesture.view.center = CGPointMake(panGesture.view.center.x + translation.x,
                                     panGesture.view.center.y + translation.y);

}

@end

感谢您的回答!

最佳答案

您的代码无法移动您的 View ,这是不正确的。平移手势识别器给出的值从用户第一次触摸的点开始,从 0 开始,随着用户将手指从该点移动得越远,值就会不断增加。

您需要记录 View 的起始中心点并将平移值添加到该起始点以计算新的中心点,或者每次调整 View 中心时将平移值重置为零。

如果您不这样做,则每次平移值发生变化时,您都会将先前的移动量加上任何新的移动量添加到中心点。这导致变化量疯狂放大。

想象一下用户点击 (100, 100),然后从那里向下向右拖动。您被调用的翻译首先是 (+5,+5),然后是 (+10,+10),然后是 (+15,+15),然后是 (+20,+20),依此类推,直到 ( +100,+100)。这意味着用户将 View 向右拖动 100 点,向下拖动 100 点。但是,您首先将 (5,5) 添加到中心,然后是 (10,10),然后是 (15,15),依此类推。当用户的手指到达 (200,200) 时,您已经添加了比用户实际对中心位置所做的 (+100, +100) 更改。

在您的代码中,您可以通过添加一行来修复它:

-(void)panGestureRecognizer:(UIPanGestureRecognizer *)发送者

{
    CGPoint translation = [sender translationInView:self];

    //log the translation so you can see if it's working
    NSLog(@"Panned with translation point: %@", NSStringFromCGPoint(translation));

    sender.view.center = CGPointMake(sender.view.center.x + translation.x,
                                 sender.view.center.y + translation.y);

    //-------------------------------------------------------
    //---below is the line you need to add to zero out the translation.---
    [sender setTranslation: CGPointZero inView: self];
    //-------------------------------------------------------

}

关于ios - 使用平移移动将部分 Table View Cell 滑开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24249945/

相关文章:

ios - #define和enum的简单代码理解

ios - UIAlertController 未从 AppDelegate 显示

ios - 如何创建一个黑色的 UIImage?

iOS 应用程序在手动启动时崩溃/挂起,但在其他情况下不会

ios - 如何在 Swift 中对 URL 进行编码

iphone - 在一段时间内显示标签

objective-c - Objectivc C 保留层的上下文

ios - 如何快速使用来自 reverseGeocodeLocation 的数据?

objective-c - 双 UIScrollView 同步 - 不同高度

ios - 如何删除不再需要的 View Controller ?