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值,然后再重设,用动画过渡即可)

  • 相关阅读:
    VysorPro助手
    Play 2D games on Pixel running Android Nougat (N7.1.2) with Daydream View VR headset
    Play 2D games on Nexus 6P running Android N7.1.1 with Daydream View VR headset
    Native SBS for Android
    ADB和Fastboot最新版的谷歌官方下载链接
    How do I install Daydream on my phone?
    Daydream Controller手柄数据的解析
    蓝牙BLE传输性能及延迟分析
    VR(虚拟现实)开发资源汇总
    Android(Java)控制GPIO的方法及耗时分析
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/4298337.html
Copyright © 2011-2022 走看看