1
- //
2
- // SomoView.m
3
- // SomoDemo
4
- //
5
- // Created by 向小辉 on 2017/11/24.
6
- // Copyright © 2017年 KINX. All rights reserved.
7
- //
1
+ /* *
2
+ * Copyright (c) 2016-present, K.
3
+ * All rights reserved.
4
+ *
5
+ * e-mail:xorshine@icloud.com
6
+ * github:https://github.com/xorshine
7
+ *
8
+ * This source code is licensed under the MIT license.
9
+ */
8
10
9
11
#import " SomoView.h"
10
12
#import " SomoDefines.h"
@@ -20,61 +22,128 @@ @interface SomoView()
20
22
@implementation SomoView
21
23
22
24
- (instancetype )initWithFrame : (CGRect )rect somoColor : (UIColor *)color {
23
- if (self = [super initWithFrame: rect]) {
24
- self.somoColor = color;
25
- [self setup ];
26
- }
27
- return self;
25
+ return [[[self class ] alloc ] initWithFrame: rect
26
+ somoColor: color
27
+ animationStyle: SomoAnimationStyleSolid];
28
28
}
29
29
30
- - (instancetype )init {
31
- self = [super init ];
32
- if (self) {
33
- [self setup ];
30
+ - (instancetype )initWithFrame : (CGRect )rect
31
+ somoColor : (UIColor *)color
32
+ animationStyle : (SomoAnimationStyle)style {
33
+ if (self = [super initWithFrame: rect]) {
34
+ [self _setup ];
35
+ _somoColor = color;
36
+ _animationStyle = style;
34
37
}
35
38
return self;
36
39
}
37
40
38
41
- (instancetype )initWithFrame : (CGRect )frame {
39
42
self = [super initWithFrame: frame];
40
43
if (self) {
41
- [self setup ];
44
+ [self _setup ];
42
45
}
43
46
return self;
44
47
}
45
48
46
- - (void )setup {
47
- if (!self.somoColor ) {
48
- self.somoColor = SomoColorFromRGBV (200 );
49
+ - (void )layoutSubviews {
50
+ [super layoutSubviews ];
51
+ }
52
+
53
+ - (void )setAnimationStyle : (SomoAnimationStyle)animationStyle {
54
+ if (_animationStyle != animationStyle) {
55
+ _animationStyle = animationStyle;
56
+ [self .somoLayer removeFromSuperlayer ];
57
+ [self _animate ];
49
58
}
59
+ }
60
+
61
+ - (void )_setup {
62
+ _somoColor = SomoColorFromRGBV (150 );
63
+ _animationStyle = SomoAnimationStyleSolid;
50
64
self.backgroundColor = self.somoColor ;
51
65
self.layer .masksToBounds = YES ;
52
- [self .layer addSublayer: self .somoLayer];
53
-
54
- [self animate ];
66
+ [self _animate ];
55
67
}
56
68
57
- - (void )animate {
58
- CABasicAnimation * basic = [CABasicAnimation animationWithKeyPath: @" position" ];
59
- basic.fromValue = [NSValue valueWithCGPoint: CGPointMake (-kShadowWidth /2 ., self .bounds.size.height/2 .)];
60
- basic.toValue = [NSValue valueWithCGPoint: CGPointMake (self .bounds.size.width+kShadowWidth /2 ., self .bounds.size.height/2 .)];
61
- basic.duration = 1.5 ;
62
- basic.repeatCount = INFINITY;
63
- [self .somoLayer addAnimation: basic forKey: basic.keyPath];
69
+ - (void )_animate {
70
+ CGSize size = self.bounds .size ;
71
+ switch (self.animationStyle ) {
72
+ case SomoAnimationStyleSolid:{
73
+ CABasicAnimation * basic = [CABasicAnimation animationWithKeyPath: @" opacity" ];
74
+ basic.fromValue = @1 .;
75
+ basic.toValue = @0.5 ;
76
+ basic.duration = 2 .;
77
+ basic.repeatCount = INFINITY;
78
+ basic.autoreverses = YES ;
79
+ basic.removedOnCompletion = NO ;
80
+ [self .layer addAnimation: basic forKey: basic.keyPath];
81
+ break ;
82
+ }
83
+ case SomoAnimationStyleGradientHorizontal:{
84
+ CABasicAnimation * basic = [CABasicAnimation animationWithKeyPath: @" position" ];
85
+ basic.fromValue = [NSValue valueWithCGPoint: CGPointMake (-kShadowWidth /2 ., size.height/2 .)];
86
+ basic.toValue = [NSValue valueWithCGPoint: CGPointMake (size.width+kShadowWidth /2 ., size.height/2 .)];
87
+ basic.duration = 1.5 ;
88
+ basic.repeatCount = INFINITY;
89
+ basic.removedOnCompletion = NO ;
90
+ self.somoLayer .frame = CGRectMake (0 , 0 , kShadowWidth , size.height );
91
+ self.somoLayer .startPoint = CGPointMake (0 , 0.5 );
92
+ self.somoLayer .endPoint = CGPointMake (1 , 0.5 );
93
+ [self .somoLayer addAnimation: basic forKey: basic.keyPath];
94
+ [self .layer addSublayer: self .somoLayer];
95
+ break ;
96
+ }
97
+ case SomoAnimationStyleGradientVertical:{
98
+ CGFloat height = size.height /2 . > 40 . ? : 40 .;
99
+ CABasicAnimation * basic = [CABasicAnimation animationWithKeyPath: @" position" ];
100
+ basic.fromValue = [NSValue valueWithCGPoint: CGPointMake (size.width/2 ., -height)];
101
+ basic.toValue = [NSValue valueWithCGPoint: CGPointMake (size.width/2 ., size.height+height)];
102
+ basic.duration = 1.5 ;
103
+ basic.repeatCount = INFINITY;
104
+ basic.removedOnCompletion = NO ;
105
+ self.somoLayer .frame = CGRectMake (0 ,0 ,size.width ,height);
106
+ self.somoLayer .startPoint = CGPointMake (0.5 , 0 );
107
+ self.somoLayer .endPoint = CGPointMake (0.5 , 1 );
108
+ [self .somoLayer addAnimation: basic forKey: basic.keyPath];
109
+ [self .layer addSublayer: self .somoLayer];
110
+ break ;
111
+ }
112
+ case SomoAnimationStyleOblique:{
113
+ CABasicAnimation * basic = [CABasicAnimation animationWithKeyPath: @" position" ];
114
+ basic.fromValue = [NSValue valueWithCGPoint: CGPointMake (-kShadowWidth , size.height/2 .)];
115
+ basic.toValue = [NSValue valueWithCGPoint: CGPointMake (size.width+kShadowWidth , size.height/2 .)];
116
+ basic.duration = 1.5 ;
117
+ basic.repeatCount = INFINITY;
118
+ basic.removedOnCompletion = NO ;
119
+ self.somoLayer .affineTransform = CGAffineTransformMakeRotation (0.4 );
120
+ self.somoLayer .frame = CGRectMake (0 , 0 , kShadowWidth , size.height +200 );
121
+ self.somoLayer .startPoint = CGPointMake (0 , 0.5 );
122
+ self.somoLayer .endPoint = CGPointMake (1 , 0.5 );
123
+ [self .somoLayer addAnimation: basic forKey: basic.keyPath];
124
+ [self .layer addSublayer: self .somoLayer];
125
+ break ;
126
+ }
127
+ default :
128
+ break ;
129
+ }
64
130
}
65
131
66
132
-(CAGradientLayer *)somoLayer {
67
133
if (!_somoLayer) {
68
134
_somoLayer = [CAGradientLayer layer ];
69
- _somoLayer.frame = CGRectMake (0 , 0 , kShadowWidth , self.bounds .size .height );
70
- _somoLayer.colors = @[(id )[self .somoColor colorWithAlphaComponent: 0.1 ].CGColor ,
71
- (id )[[UIColor whiteColor ] colorWithAlphaComponent: 0.25 ].CGColor ,
72
- (id )[[UIColor whiteColor ] colorWithAlphaComponent: 0.4 ].CGColor ,
73
- (id )[[UIColor whiteColor ] colorWithAlphaComponent: 0.25 ].CGColor ,
74
- (id )[self .somoColor colorWithAlphaComponent: 0.1 ].CGColor ];
75
- _somoLayer.startPoint = CGPointMake (0 , 0.5 );
76
- _somoLayer.endPoint = CGPointMake (1 , 0.5 );
77
-
135
+ UIColor * color = [UIColor whiteColor ];
136
+ _somoLayer.colors = @[(id )[color colorWithAlphaComponent: 0.03 ].CGColor ,
137
+ (id )[color colorWithAlphaComponent: 0.09 ].CGColor ,
138
+ (id )[color colorWithAlphaComponent: 0.15 ].CGColor ,
139
+ (id )[color colorWithAlphaComponent: 0.21 ].CGColor ,
140
+ (id )[color colorWithAlphaComponent: 0.27 ].CGColor ,
141
+ (id )[color colorWithAlphaComponent: 0.30 ].CGColor ,
142
+ (id )[color colorWithAlphaComponent: 0.27 ].CGColor ,
143
+ (id )[color colorWithAlphaComponent: 0.21 ].CGColor ,
144
+ (id )[color colorWithAlphaComponent: 0.15 ].CGColor ,
145
+ (id )[color colorWithAlphaComponent: 0.09 ].CGColor ,
146
+ (id )[color colorWithAlphaComponent: 0.03 ].CGColor ];
78
147
}
79
148
return _somoLayer;
80
149
}
0 commit comments