0126——动画(类似微信下拉眼睛)

效果图

首先你得画出这只眼睛,这是眼睛包括5个部分组成:

1 @property (strong, nonatomic) CAShapeLayer *eyeFirstLightLayer;
2 @property (strong, nonatomic) CAShapeLayer *eyeSecondLightLayer;
3 @property (strong, nonatomic) CAShapeLayer *eyeballLayer;
4 @property (strong, nonatomic) CAShapeLayer *topEyesocketLayer;
5 @property (strong, nonatomic) CAShapeLayer *bottomEyesocketLayer;

然后,还是通过 UIBezierPath 和 CAShapeLayer 这样的老套路来画,代码较多:

 1 - (CAShapeLayer *)eyeFirstLightLayer {
 2     if (!_eyeFirstLightLayer) {
 3         _eyeFirstLightLayer = [CAShapeLayer layer];
 4         CGPoint center = CGPointMake(CGRectGetWidth(self.frame) / 2, CGRectGetHeight(self.frame) / 2);
 5         UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center
 6                                                             radius:CGRectGetWidth(self.frame) * 0.2
 7                                                         startAngle:(230.f / 180.f) * M_PI
 8                                                           endAngle:(265.f / 180.f) * M_PI
 9                                                          clockwise:YES];
10         _eyeFirstLightLayer.borderColor = [UIColor blackColor].CGColor;
11         _eyeFirstLightLayer.lineWidth = 5.f;
12         _eyeFirstLightLayer.path = path.CGPath;
13         _eyeFirstLightLayer.fillColor = [UIColor clearColor].CGColor;
14         _eyeFirstLightLayer.strokeColor = [UIColor whiteColor].CGColor;
15     }
16     return _eyeFirstLightLayer;
17 }
18
19 - (CAShapeLayer *)eyeSecondLightLayer {
20     if (!_eyeSecondLightLayer) {
21         _eyeSecondLightLayer = [CAShapeLayer layer];
22         CGPoint center = CGPointMake(CGRectGetWidth(self.frame) / 2, CGRectGetHeight(self.frame) / 2);
23         UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center
24                                                             radius:CGRectGetWidth(self.frame) * 0.2
25                                                         startAngle:(211.f / 180.f) * M_PI
26                                                           endAngle:(220.f / 180.f) * M_PI
27                                                          clockwise:YES];
28         _eyeSecondLightLayer.borderColor = [UIColor blackColor].CGColor;
29         _eyeSecondLightLayer.lineWidth = 5.f;
30         _eyeSecondLightLayer.path = path.CGPath;
31         _eyeSecondLightLayer.fillColor = [UIColor clearColor].CGColor;
32         _eyeSecondLightLayer.strokeColor = [UIColor whiteColor].CGColor;
33
34     }
35     return _eyeSecondLightLayer;
36 }
37
38 - (CAShapeLayer *)eyeballLayer {
39     if (!_eyeballLayer) {
40         _eyeballLayer = [CAShapeLayer layer];
41         CGPoint center = CGPointMake(CGRectGetWidth(self.frame) / 2, CGRectGetHeight(self.frame) / 2);
42         UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center
43                                                             radius:CGRectGetWidth(self.frame) * 0.3
44                                                         startAngle:(0.f / 180.f) * M_PI
45                                                           endAngle:(360.f / 180.f) * M_PI
46                                                          clockwise:YES];
47         _eyeballLayer.borderColor = [UIColor blackColor].CGColor;
48         _eyeballLayer.lineWidth = 1.f;
49         _eyeballLayer.path = path.CGPath;
50         _eyeballLayer.fillColor = [UIColor clearColor].CGColor;
51         _eyeballLayer.strokeColor = [UIColor whiteColor].CGColor;
52         _eyeballLayer.anchorPoint = CGPointMake(0.5, 0.5);
53
54     }
55     return _eyeballLayer;
56 }
57
58 - (CAShapeLayer *)topEyesocketLayer {
59     if (!_topEyesocketLayer) {
60         _topEyesocketLayer = [CAShapeLayer layer];
61         CGPoint center = CGPointMake(CGRectGetWidth(self.frame) / 2, CGRectGetHeight(self.frame) / 2);
62         UIBezierPath *path = [UIBezierPath bezierPath];
63         [path moveToPoint:CGPointMake(0, CGRectGetHeight(self.frame) / 2)];
64         [path addQuadCurveToPoint:CGPointMake(CGRectGetWidth(self.frame), CGRectGetHeight(self.frame) / 2)
65                      controlPoint:CGPointMake(CGRectGetWidth(self.frame) / 2, center.y - center.y - 20)];
66         _topEyesocketLayer.borderColor = [UIColor blackColor].CGColor;
67         _topEyesocketLayer.lineWidth = 1.f;
68         _topEyesocketLayer.path = path.CGPath;
69         _topEyesocketLayer.fillColor = [UIColor clearColor].CGColor;
70         _topEyesocketLayer.strokeColor = [UIColor whiteColor].CGColor;
71     }
72     return _topEyesocketLayer;
73 }
74
75 - (CAShapeLayer *)bottomEyesocketLayer {
76     if (!_bottomEyesocketLayer) {
77         _bottomEyesocketLayer = [CAShapeLayer layer];
78         CGPoint center = CGPointMake(CGRectGetWidth(self.frame) / 2, CGRectGetHeight(self.frame) / 2);
79         UIBezierPath *path = [UIBezierPath bezierPath];
80         [path moveToPoint:CGPointMake(0, CGRectGetHeight(self.frame) / 2)];
81         [path addQuadCurveToPoint:CGPointMake(CGRectGetWidth(self.frame), CGRectGetHeight(self.frame) / 2)
82                      controlPoint:CGPointMake(CGRectGetWidth(self.frame) / 2, center.y + center.y + 20)];
83         _bottomEyesocketLayer.borderColor = [UIColor blackColor].CGColor;
84         _bottomEyesocketLayer.lineWidth = 1.f;
85         _bottomEyesocketLayer.path = path.CGPath;
86         _bottomEyesocketLayer.fillColor = [UIColor clearColor].CGColor;
87         _bottomEyesocketLayer.strokeColor = [UIColor whiteColor].CGColor;
88
89     }
90     return _bottomEyesocketLayer;
91 }

然后更改一下某些属性的值,方便稍后的动画:

1 - (void)setupAnimation {
2     self.eyeFirstLightLayer.lineWidth = 0.f;
3     self.eyeSecondLightLayer.lineWidth = 0.f;
4     self.eyeballLayer.opacity = 0.f;
5     _bottomEyesocketLayer.strokeStart = 0.5f;
6     _bottomEyesocketLayer.strokeEnd = 0.5f;
7     _topEyesocketLayer.strokeStart = 0.5f;
8     _topEyesocketLayer.strokeEnd = 0.5f;
9 }

最后根据 UIScrollView 的 contentOffset 来控制各种属性,办法较笨,但管用。

 1 - (void)animationWith:(CGFloat)y {
 2     CGFloat flag = self.frame.origin.y * 2.f - 20.f;
 3     if (y < flag) {
 4         if (self.eyeFirstLightLayer.lineWidth < 5.f) {
 5             self.eyeFirstLightLayer.lineWidth += 1.f;
 6             self.eyeSecondLightLayer.lineWidth += 1.f;
 7         }
 8     }
 9
10     if(y < flag - 20) {
11         if (self.eyeballLayer.opacity <= 1.0f) {
12             self.eyeballLayer.opacity += 0.1f;
13         }
14
15     }
16
17     if (y < flag - 40) {
18         if (self.topEyesocketLayer.strokeEnd < 1.f && self.topEyesocketLayer.strokeStart > 0.f) {
19             self.topEyesocketLayer.strokeEnd += 0.1f;
20             self.topEyesocketLayer.strokeStart -= 0.1f;
21             self.bottomEyesocketLayer.strokeEnd += 0.1f;
22             self.bottomEyesocketLayer.strokeStart -= 0.1f;
23         }
24     }
25
26     if (y > flag - 40) {
27         if (self.topEyesocketLayer.strokeEnd > 0.5f && self.topEyesocketLayer.strokeStart < 0.5f) {
28             self.topEyesocketLayer.strokeEnd -= 0.1f;
29             self.topEyesocketLayer.strokeStart += 0.1f;
30             self.bottomEyesocketLayer.strokeEnd -= 0.1f;
31             self.bottomEyesocketLayer.strokeStart += 0.1f;
32         }
33     }
34
35     if (y > flag - 20) {
36         if (self.eyeballLayer.opacity >= 0.0f) {
37             self.eyeballLayer.opacity -= 0.1f;
38         }
39     }
40
41     if (y > flag) {
42         if (self.eyeFirstLightLayer.lineWidth > 0.f) {
43             self.eyeFirstLightLayer.lineWidth -= 1.f;
44             self.eyeSecondLightLayer.lineWidth -= 1.f;
45         }
46     }
47 }

最后

总之使用 UIbezierPath 和 CAShapeLayer 可以画出你想要的任何形状,没有它做不到,只有你想不到,搞定了它们你就可以轻松定制你想要的任何控件了。

简书地址:http://www.jianshu.com/p/c5cbb5e05075

时间: 2024-11-03 20:57:00

0126——动画(类似微信下拉眼睛)的相关文章

类似微信下拉眼睛UI

public class EyeView extends FrameLayout { private Paint paint; private Bitmap bitmap; public EyeView(Context context) { super(context); init(); } public EyeView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public EyeView(C

PullDownListView高仿微信下拉眼睛出现动画

PullDownListView高仿微信下拉眼睛出现动画,动画自定义PullDownListView,pullDownListView.setOnPullHeightChangeListener设置下拉高度监听,onTopHeightChange()onBottomHeightChange(),onRefreshing()其中提供者三个方法时刻刷新页面特效 查看项目完整源码:http://www.itlanbao.com/code/20150911/10000/100524.html 引入布局:

超炫 PullDownListView高仿微信下拉眼睛出现动画

PullDownListView高仿微信下拉眼睛出现动画,动画自定义PullDownListView, pullDownListView.setOnPullHeightChangeListener设置下拉高度监听,onTopHeightChange() onBottomHeightChange(),onRefreshing()其中提供者三个方法时刻刷新页面特效 运行效果: 完整源码下载地址:http://www.itlanbao.com/code/20150911/10000/100524.ht

弹性ScrollView,和下啦刷新的效果类似 实现下拉弹回和上拉弹回

今天做了一个弹性ScrollView,和下啦刷新的效果类似,我想这个很多需求都用的这种效果 其实这是一个自定义的scrollView,上代码,这是我写在一个公共的组件包里的 package com.pb.soft.widget; import android.content.Context; import android.graphics.Rect; import android.util.AttributeSet; import android.view.MotionEvent; import

CSS3+Jquery实现带动画效果的下拉选择框

CSS3+JQuery实现带动画效果的下拉选择框. 元素结构为: 1 <div class="box"> 2 <p>this is the first li</p> 3 <div id="blank"></div> 4 <ul> 5 <li class="selected">this is the first li</li> 6 <li >

Android开发仿微信下拉关闭图片11

图片会跟随手指移动,只有是下滑时才会退出查看页面,其他情况会复位,直接当做ImageView使用即可,setViewCall方法是在下滑完成后要执行的操作,上,左,右,可自行扩展 onTouchEvent 监听手指坐标,GestureDetector 监听滑动的惯性,ViewHelper设置图片位移动画 public class FriendCircleView extends android.support.v7.widget.AppCompatImageView implements Ges

快速下拉刷新动画

-(void)setupTableview{ //添加下拉的动画图片 //设置下拉刷新回调 [self.tableView addGifHeaderWithRefreshingTarget:self refreshingAction:@selector(loadNewData)]; //设置普通状态的动画图片 NSMutableArray *idleImages = [NSMutableArray array]; for (NSUInteger i = 1; i<=60; ++i) { // U

微信浏览器下拉黑边的终极解决方案---wScroollFix

开始 由于在开发微信页面的时候下拉黑边的情况很常见,有时候会比较影响体验,因此开发了wScrollFix库,用于隐藏微信下拉的黑边问题npm地址github地址 什么是黑边 相信很多在微信上面做网页开发的同学都会发现,当页面被划到顶部或者底部的时候,如果继续滑动,页面会被整个进行下滑,露出上面或者下面的黑底,黑底上面标明了当前访问页面的域名信息,黑边的作用也很明显,由于在微信内浏览器并没有像传统浏览器地址栏那样子的设计,为了在一定程度上面防止钓鱼网站,所以通过下拉显示域名信息,如下图Wechat

自个儿写Android的下拉刷新/上拉加载控件 (续)

本文算是对之前的一篇博文<自个儿写Android的下拉刷新/上拉加载控件>的续章,如果有兴趣了解更多的朋友可以先看一看之前的这篇博客. 事实上之所以会有之前的那篇博文的出现,是起因于前段时间自己在写一个练手的App时很快就遇到这种需求.其实我们可以发现类似这样下拉刷新.上拉加载的功能正在变得越来越普遍,可以说如今基本上绝大多数的应用里面都会使用到.当然,随着Android的发展,已经有不少现成的可以实现这种需求的"轮子"供我们使用了. 但转过头想一下想,既然本来就是自己练手