首先看效果
QQ20201024-140424-HD的副本.gif
關(guān)鍵點(diǎn)
自定義手勢(shì)
很明顯的需要一個(gè)自定義拖動(dòng)手勢(shì)
//拖拽手勢(shì)
UIPanGestureRecognizer *tableViewPan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureAction:)];
tableViewPan.delegate = self;
self.animationPanGes = tableViewPan;
[self addGestureRecognizer:tableViewPan];
跟隨拖拽手勢(shì)移動(dòng)視圖
這里移動(dòng)視圖并不一定要是tableView,雖然拖拽手勢(shì)是添加在tableView上面,但是可以設(shè)置為tableView的父視圖,具體自行實(shí)踐
@protocol MTVerticalAnimationTableViewDelegate <NSObject>
/**跟隨拖動(dòng)手勢(shì)移動(dòng)的View*/
- (UIView *)mt_verticalAnimationTableViewPanGestureMovingView:(MTVerticalAnimationTableView *)tableView;
@end
這里通過(guò)代理來(lái)控制移動(dòng)的視圖
處理自定義拖動(dòng)手勢(shì)與tableView的拖動(dòng)手勢(shì)沖突
- 設(shè)置tableView的拖動(dòng)手勢(shì)在自定義拖動(dòng)手勢(shì)失效之后生效
//tableView滾動(dòng)手勢(shì)依賴(lài)于自定義手勢(shì)失敗
[self.panGestureRecognizer requireGestureRecognizerToFail:tableViewPan];
- 手勢(shì)代理設(shè)置自定義手勢(shì)失效條件
//列表滾動(dòng) 只響應(yīng)tableView拖動(dòng)手勢(shì)
if (self.contentOffset.y > 0) {
return NO;
}
//contentOffset.y = 0時(shí) 1.向上拖動(dòng)甥绿,tableView拖動(dòng)手勢(shì)響應(yīng) 2.向下拖動(dòng)盛卡,自定義拖動(dòng)手勢(shì)響應(yīng)
if (self.contentOffset.y == 0 && translatedPoint.y < 0) {
return NO;
}
- 設(shè)置自定義手勢(shì)生效時(shí)的操作
/**拖動(dòng)手勢(shì)中*/
CGRect frame = movingView.frame;
frame.origin.y += translation.y;
//改變frame
movingView.frame = frame
- 設(shè)置自定義手勢(shì)結(jié)束時(shí)的操作
/**拖動(dòng)手勢(shì)結(jié)束*/
CGRect frame = movingView.frame;
frame.origin.y += translation.y;
//確定最終Y
MTAnimationTableViewState state = MTAnimationTableViewStateBottom;
if (frame.origin.y > self.centerStateOriginY && frame.origin.y <= self.bottomStateOriginY) {
frame.origin.y = self.bottomStateOriginY;
} else {
if (self.state == MTAnimationTableViewStateTop && frame.origin.y > self.topStateOriginY) {
frame.origin.y = self.centerStateOriginY;
state = MTAnimationTableViewStateCenter;
} else {
frame.origin.y = self.topStateOriginY;
state = MTAnimationTableViewStateTop;
}
}
//改變frame
movingView.frame = frame
所有代碼
MTVerticalAnimationTableView.h
文件
#import <UIKit/UIKit.h>
/*
MTAnimationTableViewStateTop
1.列表向上滑動(dòng) 正常滑動(dòng)
2.列表向下滑動(dòng) a.沒(méi)有滑動(dòng)到頂部骑素,正常滑動(dòng) b.滑動(dòng)到頂部,整體向下
MTAnimationTableViewStateCenter
1.列表向上滑動(dòng) 禁止滑動(dòng) 整體向上
2.列表向下滑動(dòng) 禁止滑動(dòng) 整體向下
MTAnimationTableViewStateBottom
*/
typedef enum : NSUInteger {
MTAnimationTableViewStateTop,
MTAnimationTableViewStateCenter,
MTAnimationTableViewStateBottom,
} MTAnimationTableViewState;
@protocol MTVerticalAnimationTableViewDelegate;
@interface MTVerticalAnimationTableView : UITableView
@property (nonatomic, weak)id<MTVerticalAnimationTableViewDelegate>animationDelegate;
/**位置枚舉*/
@property (nonatomic, assign, readonly)MTAnimationTableViewState state;
/**Top PanGestureMovingView的frame.origin.y 默認(rèn) = (安全區(qū)域高度+導(dǎo)航欄高度)*/
@property (nonatomic, assign)CGFloat topStateOriginY;
/**Center PanGestureMovingView的frame.origin.y 默認(rèn) = (安全區(qū)域高度+導(dǎo)航欄高度-topStateOriginY)/2*/
@property (nonatomic, assign)CGFloat centerStateOriginY;
/**Bottom PanGestureMovingView的frame.origin.y 默認(rèn) = (屏幕高度)*/
@property (nonatomic, assign)CGFloat bottomStateOriginY;
/**創(chuàng)建*/
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style state:(MTAnimationTableViewState)state;
/**顯示*/
- (void)showAnimationTableViewCompletion:(dispatch_block_t)completion;
/**隱藏*/
- (void)hiddenAnimationTableViewCompletion:(dispatch_block_t)completion;
@end
@protocol MTVerticalAnimationTableViewDelegate <NSObject>
/**跟隨拖動(dòng)手勢(shì)移動(dòng)的View*/
- (UIView *)mt_verticalAnimationTableViewPanGestureMovingView:(MTVerticalAnimationTableView *)tableView;
@optional
/**拖動(dòng)手勢(shì)移動(dòng)的View動(dòng)畫(huà)結(jié)束*/
- (void)mt_verticalAnimationTableViewPanGestureEnded:(MTVerticalAnimationTableView *)tableView;
@end
MTVerticalAnimationTableView.m
文件
#import "MTVerticalAnimationTableView.h"
@interface MTVerticalAnimationTableView ()<UIGestureRecognizerDelegate>
/**拖動(dòng)手勢(shì)*/
@property (nonatomic, strong)UIPanGestureRecognizer *animationPanGes;
/**位置枚舉*/
@property (nonatomic, assign, readwrite)MTAnimationTableViewState state;
@end
@implementation MTVerticalAnimationTableView
- (void)dealloc{
NSLog(@"MTAnimationTableView dealloc");
}
#pragma mark - Public
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style state:(MTAnimationTableViewState)state{
self = [super initWithFrame:frame style:style];
if (self) {
self.state = state;
//設(shè)置默認(rèn)限制
[self setupAnimationTableViewStateOriginY];
//設(shè)置頭部
[self setupSubViews:style];
}
return self;
}
/**顯示*/
- (void)showAnimationTableViewCompletion:(dispatch_block_t)completion {
/**已經(jīng)彈出不做處理*/
if (self.state != MTAnimationTableViewStateBottom) {
if (completion) {
completion();
}
return;
}
if ([self.animationDelegate respondsToSelector:@selector(mt_verticalAnimationTableViewPanGestureMovingView:)]) {
CGRect frame = [self _panGestureMovingViewFrame];
frame.origin.y = self.centerStateOriginY;
[self _animationFrame:frame changeState:MTAnimationTableViewStateCenter completion:completion];
}
}
/**隱藏*/
- (void)hiddenAnimationTableViewCompletion:(dispatch_block_t)completion {
CGRect frame = [self _panGestureMovingViewFrame];
frame.origin.y = self.bottomStateOriginY;
[self _animationFrame:frame changeState:MTAnimationTableViewStateBottom completion:completion];
}
#pragma mark - UI
/**設(shè)置默認(rèn)限制*/
- (void)setupAnimationTableViewStateOriginY {
CGFloat ret = 64;
if (@available(iOS 11.0,*)) {
CGFloat navBarHeight = 44.0f;
ret = [UIApplication sharedApplication].statusBarFrame.size.height + navBarHeight;
}
self.topStateOriginY = ret;
self.centerStateOriginY = ([UIScreen mainScreen].bounds.size.height+ret)/2;
self.bottomStateOriginY = [UIScreen mainScreen].bounds.size.height;
}
- (void)setupSubViews:(UITableViewStyle)style {
//拖拽手勢(shì)
UIPanGestureRecognizer *tableViewPan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureAction:)];
tableViewPan.delegate = self;
self.animationPanGes = tableViewPan;
[self addGestureRecognizer:tableViewPan];
//tableView滾動(dòng)手勢(shì)依賴(lài)于自定義手勢(shì)失敗
[self.panGestureRecognizer requireGestureRecognizerToFail:tableViewPan];
}
#pragma mark - Action
/**拖動(dòng)手勢(shì)*/
- (void)panGestureAction:(UIPanGestureRecognizer *)panGes {
CGPoint translation = [panGes translationInView:panGes.view];
//拖動(dòng)一直是在遞增,所以每次都要用setTranslation:inView:方法置為0
[panGes setTranslation:CGPointMake(0, 0) inView:panGes.view];
if(panGes.state ==UIGestureRecognizerStateBegan) {
} else if(panGes.state ==UIGestureRecognizerStateChanged) {
[self panGestureRecognizerChanging:translation];
} else if(panGes.state ==UIGestureRecognizerStateEnded) {
[self panGestureRecognizerDidChange:translation];
}
}
/**拖動(dòng)手勢(shì)中*/
- (void)panGestureRecognizerChanging:(CGPoint)translation {
CGRect frame = [self _panGestureMovingViewFrame];
frame.origin.y += translation.y;
//不小于最小Y
if ([self _compareCGfloat:frame.origin.y CGFloat2:self.topStateOriginY] == NSOrderedAscending) {
frame.origin.y = self.topStateOriginY;
}
//不大于最大Y
if ([self _compareCGfloat:frame.origin.y CGFloat2:self.bottomStateOriginY] == NSOrderedDescending) {
frame.origin.y = self.bottomStateOriginY;
}
//改變frame
if ([self.animationDelegate respondsToSelector:@selector(mt_verticalAnimationTableViewPanGestureMovingView:)]) {
[self.animationDelegate mt_verticalAnimationTableViewPanGestureMovingView:self].frame = frame;
}
}
/**拖動(dòng)手勢(shì)結(jié)束*/
- (void)panGestureRecognizerDidChange:(CGPoint)translation {
CGRect frame = [self _panGestureMovingViewFrame];
frame.origin.y += translation.y;
//不小于最小Y
if ([self _compareCGfloat:frame.origin.y CGFloat2:self.topStateOriginY] == NSOrderedAscending) {
frame.origin.y = self.topStateOriginY;
}
//不大于最大Y
if ([self _compareCGfloat:frame.origin.y CGFloat2:self.bottomStateOriginY] == NSOrderedDescending) {
frame.origin.y = self.bottomStateOriginY;
}
// //確定最終Y
// MTAnimationTableViewState state = MTAnimationTableViewStateBottom;
// if (frame.origin.y < self.centerStateOriginY) {
// frame.origin.y = self.topStateOriginY;
// state = MTAnimationTableViewStateTop;
// } else if(frame.origin.y >= self.centerStateOriginY && frame.origin.y < (self.bottomStateOriginY+self.centerStateOriginY)/2) {
// frame.origin.y = self.centerStateOriginY;
// state = MTAnimationTableViewStateCenter;
// } else {
// frame.origin.y = self.bottomStateOriginY;
// }
//確定最終Y
MTAnimationTableViewState state = MTAnimationTableViewStateBottom;
if ([self _compareCGfloat:frame.origin.y CGFloat2:self.centerStateOriginY] == NSOrderedDescending && [self _compareCGfloat:frame.origin.y CGFloat2:self.bottomStateOriginY] != NSOrderedDescending) {
frame.origin.y = self.bottomStateOriginY;
} else {
if (self.state == MTAnimationTableViewStateTop && [self _compareCGfloat:frame.origin.y CGFloat2:self.topStateOriginY] == NSOrderedDescending) {
frame.origin.y = self.centerStateOriginY;
state = MTAnimationTableViewStateCenter;
} else {
frame.origin.y = self.topStateOriginY;
state = MTAnimationTableViewStateTop;
}
}
//動(dòng)畫(huà)
[self _animationFrame:frame changeState:state completion:^{
//動(dòng)畫(huà)結(jié)束
if ([self.animationDelegate respondsToSelector:@selector(mt_verticalAnimationTableViewPanGestureEnded:)]) {
[self.animationDelegate mt_verticalAnimationTableViewPanGestureEnded:self];
}
}];
}
#pragma mark - Private
- (void)_animationFrame:(CGRect)frame changeState:(MTAnimationTableViewState)changeState completion:(dispatch_block_t)completion{
[UIView animateWithDuration:0.3 animations:^{
//改變frame
if ([self.animationDelegate respondsToSelector:@selector(mt_verticalAnimationTableViewPanGestureMovingView:)]) {
[self.animationDelegate mt_verticalAnimationTableViewPanGestureMovingView:self].frame = frame;
}
} completion:^(BOOL finished) {
if (finished) {
self.state = changeState;
if (completion) {
completion();
}
}
}];
}
- (CGRect)_panGestureMovingViewFrame {
if ([self.animationDelegate respondsToSelector:@selector(mt_verticalAnimationTableViewPanGestureMovingView:)]) {
UIView *moveingView = [self.animationDelegate mt_verticalAnimationTableViewPanGestureMovingView:self];
return moveingView.frame;
}
return CGRectZero;
}
/**CGFloat比較方法 解決精度丟失問(wèn)題*/
- (NSComparisonResult)_compareCGfloat:(CGFloat)float1 CGFloat2:(CGFloat)float2 {
/**精度差值 決定是否相等*/
float positiveDifference = 0.001;
float negativeDifference = -0.001;
CGFloat result = float1 - float2;
if (result < negativeDifference) {
return NSOrderedAscending;
} else if(result > positiveDifference) {
return NSOrderedDescending;
} else {
return NSOrderedSame;
}
}
#pragma mark - UIGestureRecognizerDelegate
/**處理自定義手勢(shì)生效失效*/
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
if (gestureRecognizer == self.animationPanGes && self.state == MTAnimationTableViewStateTop) {
CGPoint translatedPoint = [self.animationPanGes translationInView:self.animationPanGes.view];
//列表滾動(dòng) 只響應(yīng)tableView拖動(dòng)手勢(shì)
if (self.contentOffset.y > 0) {
return NO;
}
//contentOffset.y = 0時(shí) 1.向上拖動(dòng)闽颇,tableView拖動(dòng)手勢(shì)響應(yīng) 2.向下拖動(dòng),自定義拖動(dòng)手勢(shì)響應(yīng)
if (self.contentOffset.y == 0 && translatedPoint.y < 0) {
return NO;
}
}
return YES;
}
- (void)setTopStateOriginY:(CGFloat)topStateOriginY {
_topStateOriginY = ceil(topStateOriginY);
}
@end