iOS之UITableViewCell左右滑动效果

本文介绍了一种在UITableView的UITableViewCell中使用滑动手势的方法。通过添加UIPanGestureRecognizer并结合代理模式,可以轻松实现左右滑动效果。文中详细展示了手势识别器的安装与处理流程,并给出了如何响应滑动方向的具体实例。
摘要由CSDN通过智能技术生成
首先在 UITableViewCell.h 中声明一个代理
@optional
- (
void )tableView:( UITableView *)tableView slideToRightWithIndexPath:( NSIndexPath *)indexPath;
- (
void )tableView:( UITableView *)tableView slideToLeftWithIndexPath:( NSIndexPath *)indexPath;
@end


.h
@property ( nonatomic , strong ) UIPanGestureRecognizer  *panGestureRecognizer;
@property ( nonatomic , weak ) UITableView  *parent;
@property ( nonatomic , strong ) NSIndexPath  *indexPath;


.m
在自定义cell中添加手势
self . panGestureRecognizer = [[ UIPanGestureRecognizer alloc ] initWithTarget : self action : @selector (panGestureRecognizerHandler:)];
        [selfaddGestureRecognizer:_panGestureRecognizer];


#pragma mark - UIGestureRecognizerDelegate
- ( BOOL )gestureRecognizerShouldBegin:( UIGestureRecognizer *)gestureRecognizer
{
   
if ( self . panGestureRecognizer == gestureRecognizer) {
       
CGPoint point = [ self . panGestureRecognizer translationInView : self ];
       
return fabs (point. x ) > fabs (point. y );
    }
else {
       
return NO ;
    }
}

#pragma mark - Event Handler
- ( void )panGestureRecognizerHandler:( UIPanGestureRecognizer *)gestureRecognizer
{
   
switch (gestureRecognizer. state ) {
       
case UIGestureRecognizerStateChanged : {
           
CGPoint point = [gestureRecognizer translationInView : self ];
           
CGFloat offset = point. x ;
           
if (offset >= kSlideWidth ) {
               
                offset =
kSlideWidth + (offset - kSlideWidth ) * kSlideWidthDelta ;
               
               
if ( _parent . delegate && [ _parent . delegate respondsToSelector : @selector (tableView:slideToRightWithIndexPath:)]) {
                   
                   
id < UITableViewCellSlideDelegate > delgate = ( id < UITableViewCellSlideDelegate >) _parent . delegate ;
                    [delgate
tableView : _parent slideToRightWithIndexPath : _indexPath ];
                   
                }
               
            }
else if (offset <= - kSlideWidth ) {
               
                offset = -
kSlideWidth + (offset + kSlideWidth ) * kSlideWidthDelta ;
               
               
if ( _parent . delegate && [ _parent . delegate respondsToSelector : @selector (tableView:slideToLeftWithIndexPath:)]) {
                   
id < UITableViewCellSlideDelegate > delgate = ( id < UITableViewCellSlideDelegate >) _parent . delegate ;
                    [delgate
tableView : _parent slideToLeftWithIndexPath : _indexPath ];
                   
                }
               
            }
           
self . contentView . center = CGPointMake ( self . center . x + offset,
                                                 
self . contentView . center . y );
           
break ;
        }
       
default :
           
break ;
    }
}



最后就可以在外部调用代理方法进行相关的操作了
在cell中签代理
cell. parent  self . table ;

#pragma mark - UITableViewCellSlideDelegate
- ( void )tableView:( UITableView *)tableView slideToRightWithIndexPath:( NSIndexPath *)indexPath
{
   
NSLog ( @" 向右滑动 " );
}

- (
void )tableView:( UITableView *)tableView slideToLeftWithIndexPath:( NSIndexPath *)indexPath
{
   
NSLog ( @" 向左滑动 " );
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值