最近項目中需要做視頻列表和視頻詳情頁面,本以為很快就能寫完,沒想到這里邊還有不少坑。
視頻列表頁面cell上添加button,button顯示視頻首幀圖片,點擊button進(jìn)入視頻詳情頁面并播放視頻。列表頁似乎沒什么問題,但是測試之后發(fā)現(xiàn)有問題:1、長按button后tableview不能滑動,2、快速點擊button時沒有點擊效果。
一、解決長按button后tableview不能滑動
自定義tableview,先設(shè)置canCancelContentTouches
屬性為YES,不過它默認(rèn)就是YES,所以一般情況下不用再去設(shè)置,然后重寫自定義tableview的
- (BOOL)touchesShouldCancelInContentView:(UIView *)view
方法,直接返回YES即可。
說明:canCancelContentTouches
字面意思是“可以取消內(nèi)容觸摸”,設(shè)置為YES之后,如果用戶點擊到一個控件然后手指發(fā)生了移動,這時系統(tǒng)會調(diào)用
- (BOOL)touchesShouldCancelInContentView:(UIView *)view
判斷是否取消控件的點擊事件,轉(zhuǎn)而讓scrollview響應(yīng)滑動事件。
二、解決快速點擊button時沒有點擊效果
設(shè)置delaysContentTouches
為NO,并且在自定義的tableview中找到UITableViewWrapperView,也將它的delaysContentTouches
設(shè)置為NO。
可以使用以下方法找到UITableViewWrapperView:
for (id view in self.subviews) {
if ([NSStringFromClass([view class]) isEqualToString:@"UITableViewWrapperView"]) {
if([view isKindOfClass:[UIScrollView class]]) {
UIScrollView *scroll = (UIScrollView *)view;
scroll.delaysContentTouches = NO;
}
break;
}
}
說明:delaysContentTouches
默認(rèn)也是YES,用戶點擊scrollview時系統(tǒng)會等待一下,監(jiān)聽用戶手指是否滑動,如果滑動則讓scrollview響應(yīng)滑動事件,否則讓控件響應(yīng)點擊事件。
由于UITableView上還有個子控件UITableViewWrapperView,所以要把這兩個scrollview的delaysContentTouches
都設(shè)置為NO才行。
關(guān)于delaysContentTouches
與canCancelContentTouches
屬性的說明,可以參考《UIScrollView的delaysContentTouches與canCencelContentTouches屬性》
以上就是tableview上按鈕與滑動手勢沖突的解決辦法,下面說一下花樣滑動,先看下效果圖:
做之前查了不少博客,基本都是imageview放在self.view上,往上滑動tableview時改變imageview的Y坐標(biāo)值。
這種方法有一定的局限性,如果用戶滑動imageview是沒有效果的,因為imageview在self.view上放著,滑動事件不會自動傳給tableview,想要傳遞的話還得重寫父視圖的hitTest方法,并且之后的處理也非常麻煩。
所以我就把imageview放在了tableview上,往上滑動時不需要處理,讓它跟隨tableview一直滑動就行了,往下滑動到達(dá)臨界點時,把它從tableview上取下,放到self.view上。代碼如下:
#import "ViewController.h"
@interface ViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIImageView *topImg;
@property (nonatomic, assign) NSInteger rowNum;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.rowNum = 0;
[self setUI];
}
- (void)setUI{
[self.view addSubview:self.tableView];
[self.tableView addSubview:self.topImg];
self.tableView.frame = self.view.bounds;
self.topImg.frame = CGRectMake(0, -200, [UIScreen mainScreen].bounds.size.width, 200);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.rowNum = 20;
[self.tableView reloadData];
});
}
#pragma mark - tableView delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.rowNum;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell.textLabel.text = [NSString stringWithFormat:@"這是第%td行",indexPath.row];
return cell;
}
#pragma mark - scrollview delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat offsetY = scrollView.contentOffset.y;
if (offsetY >= -200) {
CGRect frame = CGRectMake(0, -200, self.topImg.bounds.size.width, self.topImg.bounds.size.height);
self.topImg.frame = frame;
} else {
CGRect frame = CGRectMake(0, offsetY, self.topImg.bounds.size.width, self.topImg.bounds.size.height);
self.topImg.frame = frame;
}
}
#pragma mark - actions
- (void)tap {
NSLog(@"點擊圖片");
}
#pragma mark - lazy load
- (UIImageView *)topImg {
if (!_topImg) {
_topImg = [UIImageView new];
_topImg.contentMode = UIViewContentModeScaleAspectFill;
_topImg.layer.masksToBounds = YES;
_topImg.image = [UIImage imageNamed:@"videoimg"];
_topImg.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
[_topImg addGestureRecognizer:tap];
}
return _topImg;
}
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [UITableView new];
_tableView.dataSource = self;
_tableView.delegate = self;
_tableView.rowHeight = 50;
_tableView.delaysContentTouches = NO;
// 內(nèi)容偏移量
_tableView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);
// 滾動條偏移量
_tableView.scrollIndicatorInsets = UIEdgeInsetsMake(200, 0, 0, 0);
}
return _tableView;
}
@end