果冻效果下拉刷新控件

这个下拉控件是在Elastic view animation using UIBezierPath这篇博客上看到的,觉得效果还不错,自己也就OC简单实现了一下(原作者是用Swift写的),控件效果如下:


这是代码地址:https://github.com/Yuzeyang/GCLoadingAnimation/tree/master/GCLoadingAnimationTwo
这个控件的动画效果分为两个部分:
1.下拉的果冻效果
2.下拉进度圆圈的显示及旋转

0x00 下拉果冻状态实现思路

对于下拉的状态,我将其分为三种,然后在初始化的时候,将状态设置为正常状态

typedef NS_ENUM(NSInteger, GCLoadingState) {

   GCLoadingStateNormal, // 正常状态

   GCLoadingStateLoading, // 加载中状态

   GCLoadingStateCancelled // 取消加载状态

};

并且在初始化的时候,绘制曲线的初始样式

- (void)drawOriginPath {

   UIBezierPath *path = [UIBezierPath bezierPath];
   [path moveToPoint:CGPointMake(0, 0)];
   [path addLineToPoint:CGPointMake(0, kGCLoadingViewMinHeight)];
   [path addLineToPoint:CGPointMake(CGRectGetWidth(self.associatedScrollView.frame), kGCLoadingViewMinHeight)];
   [path addLineToPoint:CGPointMake(CGRectGetWidth(self.associatedScrollView.frame), 0)];
   
   self.loadLayer.path = path.CGPath;
}

那在手指拖动的过程中,我们该如果实现果冻拉伸的效果呢?
我们需要一个辅助视图centerHelperView,这个辅助视图是加在下面这条线的中间的,如图的小黑点:


在下拉时我们就根据这个centerHelperView的变化来不断的绘制我们的曲线,所以我们用到了CADisplayLink,这个应该在写动画的时候用的也是比较多了,是根据屏幕的刷新频率将内容绘制到屏幕的定时器,当我们将定时器加到runLoop里时,我们需要注意在设置mode时,如果将mode设置为NSDefaultRunLoopMode,那么在滑动的时候,定时器会暂停,直到停止滑动才会继续工作,所以我们需要将mode设置为NSRunLoopCommonModes,这样能保证定时器在滑动的过程中也能正常工作

- (CADisplayLink *)displayLink {

   if (!_displayLink) {
       _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkAction:)];

       [_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
       _displayLink.paused = YES;
   }
   return _displayLink;
}

然后在取出centerHelperView的原点,来不断绘制果冻的曲线

- (void)displayLinkAction:(CADisplayLink *)displayLink {

   CALayer *centerHelperViewLayer = (CALayer *)[self.centerHelperView.layer presentationLayer];
   CGRect centerHelperViewRect = [[centerHelperViewLayer valueForKey:@"frame"] CGRectValue];
   [self drawLoadLayerWithCenter:centerHelperViewRect.origin];
}

既然我们能够获取到centerHelperView在不同时间里的位置,那么我们就可以根据它来绘制我们的曲线


我们可以看到左右两边都是直线,调用- addLineToPoint方法即可,重要的是底下这条线,我们获取到centerHelperView的位置后,暂且用c来表示,我们在绘制曲线时,需要用到controlPoint1controlPoint2,那我们就把底下的线分为三段,并且以c为中心店,左边取出l3l2l1,右边取出r3r2r1,曲线分为三条:

UIBezierPath *path = [UIBezierPath bezierPath];

[path moveToPoint:CGPointMake(0, 0)];

[path addLineToPoint:l3];

[path addCurveToPoint:l1 controlPoint1:l3 controlPoint2:l2];
[path addCurveToPoint:r1 controlPoint1:l1 controlPoint2:c];
[path addCurveToPoint:r3 controlPoint1:r1 controlPoint2:r2];
[path addLineToPoint:CGPointMake(CGRectGetWidth(self.associatedScrollView.frame), 0)];

果冻的曲线我们就完成了,然后我们就要开始对手势的状态来进行处理在写控件调试的时候,你可以通过给目标视图添加UIPanGestureRecognizer,调用- translationInView:来获取到手指在屏幕上拖动时位置的变化,但是下拉刷新控件一般都是加在ScrollView上的,ScrollView自己是有一个只读的UIPanGestureRecognizer属性,所以我们不必自己再添加一个,我们只需要观察UIPanGestureRecognizerstate即可

[self.associatedScrollView addObserver:self forKeyPath:@"panGestureRecognizer.state" options:NSKeyValueObservingOptionNew context:nil];

在取消拖动时,我们根据ScrollViewcontentOffset来判断,是否是取消加载还是加载

0x01 下拉进度圆圈的显示及旋转

进度圆圈的显示主要是依赖于下拉的进度,然后改变progress
,圆圈随之绘制就好

if (contentOffset.y < 0) {

   self.progress = MAX(0.0, MIN(fabs(contentOffset.y/kGCPullMaxDistance), 1.0));
} else {
   self.progress = 0;
}
- (void)drawRect:(CGRect)rect {

   UIBezierPath *circlePath = [UIBezierPath bezierPath];
   [circlePath moveToPoint:CGPointMake(0, - kGCLoadingCircleRadius)];
   [circlePath addArcWithCenter:CGPointMake(0, 0) radius:kGCLoadingCircleRadius startAngle:-M_PI/2 endAngle:((M_PI*17/9)*self.progess - M_PI/2) clockwise:YES];
   

   self.circleLayer.path = circlePath.CGPath;
}

progress达到1之后,就开始旋转动画了,这个我们使用CABasicAnimation即可

CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];

rotationAnimation.toValue = @(M_PI*2);
rotationAnimation.beginTime = CACurrentMediaTime();
rotationAnimation.duration = 1.0;
rotationAnimation.fillMode = kCAFillModeForwards;
rotationAnimation.repeatCount = HUGE_VALF;
[self.circleLayer addAnimation:rotationAnimation forKey:nil];

ok,大功告成~

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,932评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,267评论 4 61
  • 夜 静得有些凉 思绪甩不掉的惆怅 莫名的逆流成河的悲伤 仿佛间生命中曾经所有的快乐已消逝不见 不知从何时起开始不敢...
    丫丫小贝阅读 379评论 0 1
  • 新人,写的不好,勿喷!!坐标广州市天河区,去年底刚入手一套二手小户型,从看房到交房中间历时8个月左右的时间(正常只...
    黑夜里的那盏灯阅读 763评论 0 1
  • 听说你的城市很冷 担心你独自走在漆黑的夜 如果不是你我不会懂 为何有的人,在灯火阑珊时会哭 也是因为你,我才承认 ...
    陪妳阅读 1,122评论 0 1