Skip to content

Commit

Permalink
MOD: 下拉程序逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
CoderMikeHe committed Jul 13, 2020
1 parent d565914 commit b5629c5
Show file tree
Hide file tree
Showing 6 changed files with 310 additions and 108 deletions.
2 changes: 1 addition & 1 deletion WeChat/WeChat/Macros/MHConstant.m
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,4 @@
/// 下拉显示小程序 临界点2
CGFloat const MHPulldownAppletCriticalPoint2 = 130.0f;
/// 下拉显示小程序 临界点3
CGFloat const MHPulldownAppletCriticalPoint3 = 200.0f;
CGFloat const MHPulldownAppletCriticalPoint3 = 240.0f;
87 changes: 69 additions & 18 deletions WeChat/WeChat/View/MainFrame/BouncyBalls/MHBouncyBallsView.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#import "MHBouncyBallsView.h"
#import "MHBouncyBallsViewModel.h"
#import "YYTimer.h"

@interface MHBouncyBallsView ()
/// viewModel
@property (nonatomic, readwrite, strong) MHBouncyBallsViewModel *viewModel;
Expand All @@ -19,6 +21,16 @@ @interface MHBouncyBallsView ()
/// rightBall
@property (nonatomic, readwrite, weak) UIView *rightBall;

/// lastOffset 上一次偏移量
@property (nonatomic, readwrite, assign) CGFloat lastOffset;
/// stepValue
@property (nonatomic, readwrite, assign) CGFloat stepValue;
/// lastState
@property (nonatomic, readwrite, assign) MHRefreshState lastState;

/// Timer
@property (nonatomic, readwrite, strong) YYTimer *timer;

@end

@implementation MHBouncyBallsView
Expand All @@ -32,22 +44,35 @@ - (void)bindViewModel:(MHBouncyBallsViewModel *)viewModel {
self.viewModel = viewModel;

@weakify(self);

[[[RACObserve(viewModel, offset) skip:1] distinctUntilChanged] subscribeNext:^(NSNumber *x) {
/// Fixed bug: distinctUntilChanged 不需要,否则某些场景认为没变化 实际上变化了
RACSignal *signal = [RACObserve(self.viewModel, offsetInfo) skip:1];
[signal subscribeNext:^(NSDictionary *dictionary) {
@strongify(self);
/// 计算偏移量
CGFloat offset = x.doubleValue;

[self _handleOffset:offset];

CGFloat offset = [dictionary[@"offset"] doubleValue];
MHRefreshState state = [dictionary[@"state"] doubleValue];
BOOL animate = [dictionary[@"animate"] boolValue];

if (animate) {

if (!self.timer && !self.timer.isValid && self.lastOffset > MHPulldownAppletCriticalPoint0) {
NSTimeInterval interval = .05f;
CGFloat count = .3/interval;
self.stepValue = self.lastOffset/count;
self.timer = [YYTimer timerWithTimeInterval:interval target:self selector:@selector(_timerValueChanged:) repeats:YES];
}

} else {
/// 记录上一次数据
self.lastOffset = offset;
///
[self _handleOffset:dictionary];
}
}];

}



- (instancetype)initWithFrame:(CGRect)frame
{
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
// 初始化
Expand All @@ -62,7 +87,18 @@ - (instancetype)initWithFrame:(CGRect)frame
return self;
}
#pragma mark - 辅助方法
- (void)_handleOffset:(CGFloat)offset {
- (void)_handleOffset:(NSDictionary *)dictionary {

CGFloat offset = [dictionary[@"offset"] doubleValue];
MHRefreshState state = [dictionary[@"state"] doubleValue];

///
if (state == MHRefreshStateRefreshing) {
self.alpha = .0f;
}else {
self.alpha = 1.0f;
}

// 中间点相关
CGFloat scale = 0.0;
CGFloat alphaC = 0;
Expand All @@ -75,13 +111,16 @@ - (void)_handleOffset:(CGFloat)offset {
CGFloat translateL = 0.0;
CGFloat alphaL = 0;

if (offset > MHPulldownAppletCriticalPoint2) {
// 第四阶段 1 - 0.2
CGFloat step = 0.8 / (MHPulldownAppletCriticalPoint3 - MHPulldownAppletCriticalPoint2);
if (offset > MHPulldownAppletCriticalPoint3){
/// 超过这个 统一是 将自身隐藏
self.alpha = .0f;

} else if (offset > MHPulldownAppletCriticalPoint2) {
// 第四阶段 1 - 0
CGFloat step = 1.0 / (MHPulldownAppletCriticalPoint3 - MHPulldownAppletCriticalPoint2);
double alpha = 1 - step * (offset - MHPulldownAppletCriticalPoint2);
if (alpha < 0.2) {
alpha = 0.2;
}
alpha = MAX(.0f, alpha);

// 中间点阶段III: 保持scale 为1
alphaC = alpha;
scale = 1;
Expand Down Expand Up @@ -131,11 +170,23 @@ - (void)_handleOffset:(CGFloat)offset {
self.rightBall.transform = CGAffineTransformMakeTranslation(translateR, 0);
}

/// 定时器为
- (void)_timerValueChanged:(YYTimer *)timer
{
self.lastOffset -= self.stepValue;
if (self.lastOffset <= 0) {
[timer invalidate];
self.timer = nil;
}
CGFloat offset = MAX(0, self.lastOffset);
[self _handleOffset: @{@"offset" : @(offset), @"state": @(MHRefreshStateIdle), @"animate": @NO}];
}


#pragma mark - 初始化OrUI布局
/// 初始化
- (void)_setup{

self.alpha = .0f;
}

/// 创建子控件
Expand Down
6 changes: 5 additions & 1 deletion WeChat/WeChat/View/Other/NavigationBar/MHNavigationBar.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,9 @@ + (instancetype)navigationBar {
}



- (void)awakeFromNib {
[super awakeFromNib];

self.backgroundView.backgroundColor = self.backgroundColor = MH_MAIN_BACKGROUNDCOLOR;
}
@end
Loading

0 comments on commit b5629c5

Please sign in to comment.