zoukankan      html  css  js  c++  java
  • 如何在ScrollView滑动的瞬间禁用拖拽手势

    如何在ScrollView滑动的瞬间禁用拖拽手势

    效果:

    在UIScrollView滑动的瞬间禁用pan手势,可以防止用户按着屏幕不放后导致出现的一些莫须有的bug.

    //
    //  ViewController.m
    //  TableViewDemo
    //
    //  Created by XianMingYou on 15/2/23.
    //  Copyright (c) 2015年 XianMingYou. All rights reserved.
    //
    
    #import "ViewController.h"
    
    @interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
    
    @property (nonatomic, strong) UITableView *tableView;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds
                                                      style:UITableViewStylePlain];
        self.tableView.delegate   = self;
        self.tableView.dataSource = self;
        [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"YouXianMing"];
     
        [self.view addSubview:self.tableView];
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return 3;
    }
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YouXianMing"];
        cell.textLabel.text   = @"YouXianMing";
        return cell;
    }
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
        
        CGFloat offsetY = scrollView.contentOffset.y;
        
        if (offsetY <= -100) {
            // 存储这一瞬间的contentOffset值
            CGPoint storePoint = scrollView.contentOffset;
            
            // 禁止用pan手势(禁用pan手势瞬间会导致contenOffset值瞬间恢复成(0, 0))
            scrollView.panGestureRecognizer.enabled = NO;
            
            // 设置此时的contentOffset值
            scrollView.contentOffset = storePoint;
            
            [UIView animateWithDuration:0.5 animations:^{
                // 动画过渡
                scrollView.contentOffset = CGPointMake(0, 0);
            } completion:^(BOOL finished) {
                // 恢复手势
                scrollView.panGestureRecognizer.enabled = YES;
            }];
        }
    }
    
    @end

    关键的一步:

    (禁用手势后,需要存储当时的contentOffset值,然后再重设,用动画过渡即可)

  • 相关阅读:
    折腾了好久的输入法显示已禁用
    文件下载上传小工具
    隐藏进程命令行参数,例如输入密码等高危操作
    创建并使用https证书
    编译lua可执行程序
    使用msys2在window下构建和使用Linux的软件
    golang遍历时修改被遍历对象
    使用shell发送邮件,方便快捷
    关于golang的time包总结
    获取Win和Linux系统启动时间,类似uptime功能,用于判断是否修改过系统时间
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/4298337.html
Copyright © 2011-2022 走看看