【问题标题】:Slide part of Table View Cell away using pan Movement使用平移移动将部分表格视图单元格滑开
【发布时间】:2014-06-16 18:31:01
【问题描述】:

我有一个表格,其中包含来自 NIB 文件的自定义单元格。结果应该看起来像,我要抓住单元格并将其滑出屏幕。注意:只是单元格的一部分。其他部分会留下来。我在单元格上运行 pan Movement 时遇到问题。 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

感谢您的任何回答!

【问题讨论】:

    标签: ios objective-c cocoa-touch uitableview


    【解决方案1】:

    您的代码无法移动您的视图是不正确的。平移手势识别器为您提供从用户第一次触摸点开始的 0 值,并随着用户从该点移动他/她的手指越远而不断增加。

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

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

    假设用户在 (100, 100) 处点击,然后从那里向下和向右拖动。您被呼叫的翻译首先是 (+5,+5),然后是 (+10,+10),然后是 (+15,+15),然后是 (+20,+20),依此类推,直到 ( +100,+100)。这意味着用户将视图向右拖动 100 点,向下拖动 100 点。但是,您首先将 (5,5) 添加到中心,然后是 (10,10),然后是 (15,15),等等。当用户的手指到达 (200,200) 时,您已经添加了很多倍(+100, +100) 将用户实际所做的更改为中心位置。

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

    -(void)panGestureRecognizer:(UIPanGestureRecognizer *)sender

    {
        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];
        //-------------------------------------------------------
    
    }
    

    【讨论】:

    • 啊,我明白了,非常感谢您的出色回答,我现在明白了!谢谢!
    【解决方案2】:

    您的问题可能是您在-panGestureRecognizer 方法中重新创建panGesture。因此,您在识别器方法中使用的panGesture 与调用该方法的UIPanGestureRecognizer 不同,因此[panGesture translationInView:self] 可能不会返回正确的值。试试这个:

    -(void)panGestureRecognizer:(UIPanGestureRecognizer *)sender{
        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);
    }
    

    这样,sender 是调用该方法的UIPanGestureRecognizer,而不是您在该方法中创建的一个新的、空的。

    要完成这项工作,您还必须将-layoutSubviews 方法中的第一行更改为UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognizer:)];。请注意,唯一改变的是@selector(panGestureRecognizer:) 中添加的冒号,这是因为您的选择器方法现在具有sender 的参数。

    【讨论】:

    • 很好的答案!非常感谢你是冠军!但是有一个问题(当然)。运动不流畅,我会说它快两倍,结果是视图在屏幕上跳跃。
    猜你喜欢
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多