Просмотр исходного кода

Feature: Now animation duration can be changed (#1503)

- Now animation duration can be changed. (set 0 to disable animtion)
- Providing a new method to disable all animation(setAnimationDisabled) [Specially for Bugs in UICollectionView]
Frank 4 лет назад
Родитель
Сommit
962f012abd

+ 1 - 1
MJRefresh/Base/MJRefreshAutoFooter.m

@@ -157,7 +157,7 @@ - (void)setState:(MJRefreshState)state
             if (self.scrollView.pagingEnabled) {
                 CGPoint offset = self.scrollView.contentOffset;
                 offset.y -= self.scrollView.mj_insetB;
-                [UIView animateWithDuration:MJRefreshSlowAnimationDuration animations:^{
+                [UIView animateWithDuration:self.slowAnimationDuration animations:^{
                     self.scrollView.contentOffset = offset;
                     
                     if (self.endRefreshingAnimationBeginAction) {

+ 2 - 2
MJRefresh/Base/MJRefreshBackFooter.m

@@ -88,7 +88,7 @@ - (void)setState:(MJRefreshState)state
     if (state == MJRefreshStateNoMoreData || state == MJRefreshStateIdle) {
         // 刷新完毕
         if (MJRefreshStateRefreshing == oldState) {
-            [UIView animateWithDuration:MJRefreshSlowAnimationDuration animations:^{
+            [UIView animateWithDuration:self.slowAnimationDuration animations:^{
                 if (self.endRefreshingAnimationBeginAction) {
                     self.endRefreshingAnimationBeginAction();
                 }
@@ -114,7 +114,7 @@ - (void)setState:(MJRefreshState)state
         // 记录刷新前的数量
         self.lastRefreshCount = self.scrollView.mj_totalDataCount;
         
-        [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{
+        [UIView animateWithDuration:self.fastAnimationDuration animations:^{
             CGFloat bottom = self.mj_h + self.scrollViewOriginalInset.bottom;
             CGFloat deltaH = [self heightForContentBreakView];
             if (deltaH < 0) { // 如果内容高度小于view的高度

+ 9 - 0
MJRefresh/Base/MJRefreshComponent.h

@@ -47,6 +47,15 @@ typedef void (^MJRefreshComponentAction)(void);
     /** 父控件 */
     __weak UIScrollView *_scrollView;
 }
+
+#pragma mark - 刷新动画时间控制
+/** 快速动画时间(一般用在刷新开始的回弹动画), 默认 0.25 */
+@property (nonatomic) NSTimeInterval fastAnimationDuration;
+/** 慢速动画时间(一般用在刷新结束后的回弹动画), 默认 0.4*/
+@property (nonatomic) NSTimeInterval slowAnimationDuration;
+/** 关闭全部默认动画效果, 可以简单粗暴地解决 CollectionView 的回弹动画 bug */
+- (instancetype)setAnimationDisabled;
+
 #pragma mark - 刷新回调
 /** 正在刷新的回调 */
 @property (copy, nonatomic, nullable) MJRefreshComponentAction refreshingBlock;

+ 11 - 1
MJRefresh/Base/MJRefreshComponent.m

@@ -23,6 +23,8 @@ - (instancetype)initWithFrame:(CGRect)frame
         
         // 默认是普通状态
         self.state = MJRefreshStateIdle;
+        self.fastAnimationDuration = 0.25;
+        self.slowAnimationDuration = 0.4;
     }
     return self;
 }
@@ -142,7 +144,7 @@ - (void)setState:(MJRefreshState)state
 #pragma mark 进入刷新状态
 - (void)beginRefreshing
 {
-    [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{
+    [UIView animateWithDuration:self.fastAnimationDuration animations:^{
         self.alpha = 1.0;
     }];
     self.pullingPercent = 1.0;
@@ -237,6 +239,14 @@ - (void)executeRefreshingCallback
     })
 }
 
+#pragma mark - 刷新动画时间控制
+- (instancetype)setAnimationDisabled {
+    self.fastAnimationDuration = 0;
+    self.slowAnimationDuration = 0;
+    
+    return self;
+}
+
 #pragma mark - <<< Deprecation compatible function >>> -
 - (void)setEndRefreshingAnimateCompletionBlock:(MJRefreshComponentEndRefreshingCompletionBlock)endRefreshingAnimateCompletionBlock {
     _endRefreshingAnimationBeginAction = endRefreshingAnimateCompletionBlock;

+ 5 - 5
MJRefresh/Base/MJRefreshHeader.m

@@ -132,7 +132,7 @@ - (void)headerEndingAction {
     // 默认使用 UIViewAnimation 动画
     if (!self.isCollectionViewAnimationBug) {
         // 恢复inset和offset
-        [UIView animateWithDuration:MJRefreshSlowAnimationDuration animations:^{
+        [UIView animateWithDuration:self.slowAnimationDuration animations:^{
             self.scrollView.mj_insetT += self.insetTDelta;
             
             if (self.endRefreshingAnimationBeginAction) {
@@ -170,7 +170,7 @@ - (void)headerEndingAction {
     //CAAnimation keyPath 不支持 contentInset 用Bounds的动画代替
     CABasicAnimation *boundsAnimation = [CABasicAnimation animationWithKeyPath:@"bounds"];
     boundsAnimation.fromValue = [NSValue valueWithCGRect:CGRectOffset(self.scrollView.bounds, 0, self.insetTDelta)];
-    boundsAnimation.duration = MJRefreshSlowAnimationDuration;
+    boundsAnimation.duration = self.slowAnimationDuration;
     //在delegate里移除
     boundsAnimation.removedOnCompletion = NO;
     boundsAnimation.fillMode = kCAFillModeBoth;
@@ -188,7 +188,7 @@ - (void)headerEndingAction {
         CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
         opacityAnimation.fromValue = @(viewAlpha);
         opacityAnimation.toValue = @(0.0);
-        opacityAnimation.duration = MJRefreshSlowAnimationDuration;
+        opacityAnimation.duration = self.slowAnimationDuration;
         opacityAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
         [self.layer addAnimation:opacityAnimation forKey:@"MJRefreshHeaderRefreshing2IdleOpacity"];
 
@@ -201,7 +201,7 @@ - (void)headerRefreshingAction {
     // 默认使用 UIViewAnimation 动画
     if (!self.isCollectionViewAnimationBug) {
         MJRefreshDispatchAsyncOnMainQueue({
-            [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{
+            [UIView animateWithDuration:self.fastAnimationDuration animations:^{
                 if (self.scrollView.panGestureRecognizer.state != UIGestureRecognizerStateCancelled) {
                     CGFloat top = self.scrollViewOriginalInset.top + self.mj_h;
                     // 增加滚动区域top
@@ -229,7 +229,7 @@ - (void)headerRefreshingAction {
         bounds.origin.y = -top;
         boundsAnimation.fromValue = [NSValue valueWithCGRect:self.scrollView.bounds];
         boundsAnimation.toValue = [NSValue valueWithCGRect:bounds];
-        boundsAnimation.duration = MJRefreshFastAnimationDuration;
+        boundsAnimation.duration = self.fastAnimationDuration;
         //在delegate里移除
         boundsAnimation.removedOnCompletion = NO;
         boundsAnimation.fillMode = kCAFillModeBoth;

+ 2 - 2
MJRefresh/Base/MJRefreshTrailer.m

@@ -76,7 +76,7 @@ - (void)setState:(MJRefreshState)state {
     if (state == MJRefreshStateNoMoreData || state == MJRefreshStateIdle) {
         // 刷新完毕
         if (MJRefreshStateRefreshing == oldState) {
-            [UIView animateWithDuration:MJRefreshSlowAnimationDuration animations:^{
+            [UIView animateWithDuration:self.slowAnimationDuration animations:^{
                 if (self.endRefreshingAnimationBeginAction) {
                     self.endRefreshingAnimationBeginAction();
                 }
@@ -102,7 +102,7 @@ - (void)setState:(MJRefreshState)state {
         // 记录刷新前的数量
         self.lastRefreshCount = self.scrollView.mj_totalDataCount;
         
-        [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{
+        [UIView animateWithDuration:self.fastAnimationDuration animations:^{
             CGFloat right = self.mj_w + self.scrollViewOriginalInset.right;
             CGFloat deltaW = [self widthForContentBreakView];
             if (deltaW < 0) { // 如果内容宽度小于view的宽度

+ 3 - 3
MJRefresh/Custom/Footer/Back/MJRefreshBackNormalFooter.m

@@ -95,7 +95,7 @@ - (void)setState:(MJRefreshState)state
     if (state == MJRefreshStateIdle) {
         if (oldState == MJRefreshStateRefreshing) {
             self.arrowView.transform = CGAffineTransformMakeRotation(0.000001 - M_PI);
-            [UIView animateWithDuration:MJRefreshSlowAnimationDuration animations:^{
+            [UIView animateWithDuration:self.slowAnimationDuration animations:^{
                 self.loadingView.alpha = 0.0;
             } completion:^(BOOL finished) {
                 // 防止动画结束后,状态已经不是MJRefreshStateIdle
@@ -109,14 +109,14 @@ - (void)setState:(MJRefreshState)state
         } else {
             self.arrowView.hidden = NO;
             [self.loadingView stopAnimating];
-            [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{
+            [UIView animateWithDuration:self.fastAnimationDuration animations:^{
                 self.arrowView.transform = CGAffineTransformMakeRotation(0.000001 - M_PI);
             }];
         }
     } else if (state == MJRefreshStatePulling) {
         self.arrowView.hidden = NO;
         [self.loadingView stopAnimating];
-        [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{
+        [UIView animateWithDuration:self.fastAnimationDuration animations:^{
             self.arrowView.transform = CGAffineTransformIdentity;
         }];
     } else if (state == MJRefreshStateRefreshing) {

+ 3 - 3
MJRefresh/Custom/Header/MJRefreshNormalHeader.m

@@ -103,7 +103,7 @@ - (void)setState:(MJRefreshState)state
         if (oldState == MJRefreshStateRefreshing) {
             self.arrowView.transform = CGAffineTransformIdentity;
             
-            [UIView animateWithDuration:MJRefreshSlowAnimationDuration animations:^{
+            [UIView animateWithDuration:self.slowAnimationDuration animations:^{
                 self.loadingView.alpha = 0.0;
             } completion:^(BOOL finished) {
                 // 如果执行完动画发现不是idle状态,就直接返回,进入其他状态
@@ -116,14 +116,14 @@ - (void)setState:(MJRefreshState)state
         } else {
             [self.loadingView stopAnimating];
             self.arrowView.hidden = NO;
-            [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{
+            [UIView animateWithDuration:self.fastAnimationDuration animations:^{
                 self.arrowView.transform = CGAffineTransformIdentity;
             }];
         }
     } else if (state == MJRefreshStatePulling) {
         [self.loadingView stopAnimating];
         self.arrowView.hidden = NO;
-        [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{
+        [UIView animateWithDuration:self.fastAnimationDuration animations:^{
             self.arrowView.transform = CGAffineTransformMakeRotation(0.000001 - M_PI);
         }];
     } else if (state == MJRefreshStateRefreshing) {

+ 3 - 3
MJRefresh/Custom/Trailer/MJRefreshNormalTrailer.m

@@ -57,18 +57,18 @@ - (void)setState:(MJRefreshState)state {
     // 根据状态做事情
     if (state == MJRefreshStateIdle) {
         if (oldState == MJRefreshStateRefreshing) {
-            [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{
+            [UIView animateWithDuration:self.fastAnimationDuration animations:^{
                 self.arrowView.transform = CGAffineTransformMakeRotation(M_PI);
             } completion:^(BOOL finished) {
                 self.arrowView.transform = CGAffineTransformIdentity;
             }];
         } else {
-            [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{
+            [UIView animateWithDuration:self.fastAnimationDuration animations:^{
                 self.arrowView.transform = CGAffineTransformIdentity;
             }];
         }
     } else if (state == MJRefreshStatePulling) {
-        [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{
+        [UIView animateWithDuration:self.fastAnimationDuration animations:^{
             self.arrowView.transform = CGAffineTransformMakeRotation(M_PI);
         }];
     }

+ 0 - 2
MJRefresh/MJRefreshConst.h

@@ -33,8 +33,6 @@ UIKIT_EXTERN const CGFloat MJRefreshLabelLeftInset;
 UIKIT_EXTERN const CGFloat MJRefreshHeaderHeight;
 UIKIT_EXTERN const CGFloat MJRefreshFooterHeight;
 UIKIT_EXTERN const CGFloat MJRefreshTrailWidth;
-UIKIT_EXTERN const CGFloat MJRefreshFastAnimationDuration;
-UIKIT_EXTERN const CGFloat MJRefreshSlowAnimationDuration;
 
 UIKIT_EXTERN NSString *const MJRefreshKeyPathContentOffset;
 UIKIT_EXTERN NSString *const MJRefreshKeyPathContentSize;

+ 0 - 2
MJRefresh/MJRefreshConst.m

@@ -5,8 +5,6 @@
 const CGFloat MJRefreshHeaderHeight = 54.0;
 const CGFloat MJRefreshFooterHeight = 44.0;
 const CGFloat MJRefreshTrailWidth = 60.0;
-const CGFloat MJRefreshFastAnimationDuration = 0.25;
-const CGFloat MJRefreshSlowAnimationDuration = 0.4;
 
 NSString *const MJRefreshKeyPathContentOffset = @"contentOffset";
 NSString *const MJRefreshKeyPathContentInset = @"contentInset";

+ 8 - 7
MJRefreshExample/Classes/Second/MJCollectionViewController.m

@@ -30,7 +30,7 @@ - (void)example21
     __weak __typeof(self) weakSelf = self;
     
     // 下拉刷新
-    self.collectionView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
+    [[MJRefreshNormalHeader headerWithRefreshingBlock:^{
         // 增加5条假数据
         for (int i = 0; i<10; i++) {
             [weakSelf.colors insertObject:MJRandomColor atIndex:0];
@@ -43,12 +43,13 @@ - (void)example21
             // 结束刷新
             [weakSelf.collectionView.mj_header endRefreshing];
         });
-    }];
+    }] assignTo:self.collectionView];
     self.collectionView.mj_header.isCollectionViewAnimationBug = YES;
-    [self.collectionView.mj_header beginRefreshing];
+    // 简单粗暴版本
+//    [self.collectionView.mj_header setAnimationDisabled];
 
     // 上拉刷新
-    self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
+    [[[[MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
         // 增加5条假数据
         for (int i = 0; i<5; i++) {
             [weakSelf.colors addObject:MJRandomColor];
@@ -61,9 +62,9 @@ - (void)example21
             // 结束刷新
             [weakSelf.collectionView.mj_footer endRefreshing];
         });
-    }];
-    // 默认先隐藏footer
-    self.collectionView.mj_footer.hidden = YES;
+    }] setAnimationDisabled]
+      autoChangeTransparency:YES]
+     assignTo:self.collectionView];
 }
 
 #pragma mark - 数据相关