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

时间:2023-03-08 23:19:00
如何在ScrollView滑动的瞬间禁用拖拽手势

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

如何在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 ;
}
- (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 <= -) {
// 存储这一瞬间的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(, );
} completion:^(BOOL finished) {
// 恢复手势
scrollView.panGestureRecognizer.enabled = YES;
}];
}
} @end

关键的一步:

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

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