zoukankan      html  css  js  c++  java
  • iOS之UITableViewCell左右滑动效果

    首先在 UITableViewCell.h 中声明一个代理

    @protocol UITableViewCellSlideDelegate <UITableViewDelegate>
    
    @optional
    - (void)tableView:(UITableView *)tableView slideToRightWithIndexPath:(NSIndexPath *)indexPath;
    - (void)tableView:(UITableView *)tableView slideToLeftWithIndexPath:(NSIndexPath *)indexPath;
    @end

    然后在 UITableViewCell.m 中 创建一个手势 UIPanGestureRecognizer,因为我们左右滑动的时候必须依赖拖动手势,然后在 UIPanGestureRecognizer 的代理方法中进行判断到底是左滑还是右滑

    static const CGFloat kSlideWidth = 80;
    static const CGFloat kSlideWidthDelta = 0.08;
    
    #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;
        }
    }

    最后就可以在外部调用代理方法进行相关的操作了

    #pragma mark - UITableViewCellSlideDelegate
    - (void)tableView:(UITableView *)tableView slideToRightWithIndexPath:(NSIndexPath *)indexPath
    {
        NSLog(@"向右滑动");
    }
    
    - (void)tableView:(UITableView *)tableView slideToLeftWithIndexPath:(NSIndexPath *)indexPath
    {
        NSLog(@"向左滑动");
    }
  • 相关阅读:
    vscode 前端好用插件汇总
    IE8和IE9下textarea滚动选中的问题
    javascript实现数字整数位每三位一个逗号分隔
    简单枚举(算法竞赛入门经典)
    拓扑排序(算法竞赛入门经典)
    七桥问题--欧拉(算法竞赛入门经典)
    走迷宫问题 算法竞赛入门经典
    ZOJ1008
    ZOJ1163
    HDU 1069 Monkey and Banana
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/4946876.html
Copyright © 2011-2022 走看看