-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPKView.m
174 lines (145 loc) · 6.83 KB
/
PKView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//
// PKView.m
// 动态圆柱
//
// Created by jerold on 2019/5/27.
// Copyright © 2019 CodingFire. All rights reserved.
//
#import "PKView.h"
#define kViewWidth(View) CGRectGetWidth(View.frame)
#define kViewHeight(View) CGRectGetHeight(View.frame)
#define JERGAP 5
@interface PKView()
@property (strong, nonatomic)CAGradientLayer *leftLayer,*rightLayer;//左右两个view的动画layer
@property (nonatomic, strong) UIBezierPath *path;
@property (nonatomic, assign) float leftRate;
@property (nonatomic, strong) UILabel *leftLabel;
@property (nonatomic, strong) UILabel *rightLabel;
@end
@implementation PKView
//- (void)drawRect:(CGRect)rect {
//
//}
#pragma mark -- init
- (instancetype)initWithLeftRate:(float)leftRate frame:(CGRect)frame
{
//votedNum代表左边leftview占的比例
self = [super initWithFrame:frame];
if (self) {
[self commonInitWIthLeftRate:leftRate frame:(CGRect)frame];
}
return self;
}
- (void)commonInitWIthLeftRate:(float)leftRate frame:(CGRect)frame
{
self.frame = frame;
if (leftRate==0) {
leftRate = 0.12;
}else if (leftRate==1) {
leftRate = 0.88;
}
self.leftRate = leftRate;
float rightRate = 1 - leftRate;
self.leftLayer.frame = CGRectMake(0, 0, leftRate*self.frame.size.width+JERGAP, self.frame.size.height);
self.rightLayer.frame = CGRectMake(self.leftLayer.frame.size.width-JERGAP-2,0, rightRate*self.frame.size.width+JERGAP, self.frame.size.height);
if (self.leftLayer.superlayer || self.rightLayer.superlayer) {
[self.leftLayer removeFromSuperlayer];
[self.rightLayer removeFromSuperlayer];
}
[self.layer addSublayer:self.leftLayer];
[self.layer addSublayer:self.rightLayer];//test
//遮罩
// 添加路径关键点array
NSMutableArray *pointArray = [NSMutableArray array];
[pointArray addObject:NSStringFromCGPoint(CGPointMake(0.f, 0.f))];//1st point
[pointArray addObject:NSStringFromCGPoint(CGPointMake(kViewWidth(self.leftLayer)-2*JERGAP, 0.f))];//2nd point
[pointArray addObject:NSStringFromCGPoint(CGPointMake(kViewWidth(self.leftLayer), self.leftLayer.frame.size.height))];//3th point
[pointArray addObject:NSStringFromCGPoint(CGPointMake(0.f, kViewHeight(self.leftLayer)))];//4th point
CAShapeLayer *shapLayerLeft = [CAShapeLayer layer];
shapLayerLeft.path = [self getPathByPoints:pointArray].CGPath;
self.leftLayer.mask = shapLayerLeft;//test
NSMutableArray *pointArray1 = [NSMutableArray array];
[pointArray1 addObject:NSStringFromCGPoint(CGPointMake(0.f, 0.f))];
[pointArray1 addObject:NSStringFromCGPoint(CGPointMake(kViewWidth(self.rightLayer), 0.f))];
[pointArray1 addObject:NSStringFromCGPoint(CGPointMake(kViewWidth(self.rightLayer), self.rightLayer.frame.size.height))];
[pointArray1 addObject:NSStringFromCGPoint(CGPointMake(2*JERGAP, kViewHeight(self.rightLayer)))];
CAShapeLayer *shapLayerRight = [CAShapeLayer layer];
shapLayerRight.path = [self getPathByPoints:pointArray1].CGPath;
self.rightLayer.mask = shapLayerRight;
[self updateAnimation];
}
#pragma mark -- 动画
- (void)updateAnimation
{
//动画
[self animateWithLayerFromBounds:CGRectMake(0, 0, 0, self.leftLayer.bounds.size.height) toBounds:CGRectMake(0,0,self.leftLayer.bounds.size.width,self.leftLayer.bounds.size.height) Layer:self.leftLayer positionFrom:CGPointMake(-(self.leftLayer.bounds.size.width/2.0), self.bounds.size.height/2.0) potionTo:CGPointMake(self.leftLayer.bounds.size.width/2.0, self.bounds.size.height/2.0)];
[self animateWithLayerFromBounds:CGRectMake(0, 0, 0, self.rightLayer.frame.size.height) toBounds:CGRectMake(0,0,self.rightLayer.frame.size.width+JERGAP,self.rightLayer.frame.size.height) Layer:self.rightLayer positionFrom:CGPointMake(self.frame.size.width+(self.rightLayer.frame.size.width/2.0), self.frame.size.height/2.0) potionTo:CGPointMake(self.leftLayer.frame.size.width-2*JERGAP + self.rightLayer.frame.size.width/2.0 + JERGAP, self.frame.size.height/2.0)];
}
- (void)animateWithLayerFromBounds:(CGRect)fromBounds toBounds:(CGRect)toBounds Layer:(CALayer*)layer positionFrom:(CGPoint)fromValue potionTo:(CGPoint)toValue
{
// CABasicAnimation *aniBounds = [CABasicAnimation animationWithKeyPath:@"bounds"];
// aniBounds.fromValue = [NSValue valueWithCGRect:fromBounds];//原始bounds
// aniBounds.toValue = [NSValue valueWithCGRect:toBounds];//目标位置的bounds
//position指的是锚点(layer中心点)的初始位置,移动到目标位置
CABasicAnimation *aniPosition = [CABasicAnimation animationWithKeyPath:@"position"];
aniPosition.fromValue = [NSValue valueWithCGPoint:fromValue];
aniPosition.toValue = [NSValue valueWithCGPoint:toValue];
CAAnimationGroup *anis = [CAAnimationGroup animation];
anis.animations = @[aniPosition];
anis.duration = 2;
anis.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
anis.removedOnCompletion = NO;
anis.fillMode = kCAFillModeForwards;
[layer addAnimation:anis forKey:nil];
}
#pragma mark -- getter
- (CAGradientLayer *)leftLayer
{
if (!_leftLayer) {
CAGradientLayer *itemLayer = [CAGradientLayer layer];
//蓝色
itemLayer.colors = @[(__bridge id) [self fmut_colorWithRGB:0x79C0FF].CGColor,
(__bridge id) [self fmut_colorWithRGB:0x589AFF].CGColor];
itemLayer.locations = @[@0, @1.0];
itemLayer.startPoint = CGPointMake(0, 0);//渐变色以x轴为方向渐变
itemLayer.endPoint = CGPointMake(1.0, 0);
_leftLayer = itemLayer;
}
return _leftLayer;
}
- (CAGradientLayer *)rightLayer
{
if (!_rightLayer) {
CAGradientLayer *itemLayer = [CAGradientLayer layer];
//红色
itemLayer.colors = @[(__bridge id) [self fmut_colorWithRGB:0xFF73C9].CGColor,
(__bridge id) [self fmut_colorWithRGB:0xFF86DF].CGColor];
itemLayer.locations = @[@0, @1.0];
itemLayer.startPoint = CGPointMake(0, 0);//渐变色以x轴为方向渐变
itemLayer.endPoint = CGPointMake(1.0, 0);
_rightLayer = itemLayer;
}
return _rightLayer;
}
#pragma mark -- Tools
- (UIColor *)fmut_colorWithRGB:(uint32_t)rgbValue
{
return [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16) / 255.0f
green:((rgbValue & 0xFF00) >> 8) / 255.0f
blue:(rgbValue & 0xFF) / 255.0f
alpha:1];
}
- (UIBezierPath*)getPathByPoints:(NSArray*)points
{
UIBezierPath *path = [UIBezierPath bezierPath];
for (int i = 0; i < points.count; i++) {
CGPoint retrievedPoint = CGPointFromString([points objectAtIndex:i]);
if (i == 0) {
[path moveToPoint:retrievedPoint];
}else
[path addLineToPoint:retrievedPoint];
}
[path closePath];
return path;
}
@end