Skip to content

Commit 4d8032c

Browse files
committed
animationStyle
1 parent b42cfd8 commit 4d8032c

14 files changed

+206
-106
lines changed

SomoDemo/SomoDemo/DataSourceTableViewController.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ - (void)viewDidAppear:(BOOL)animated{
4545
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
4646

4747
__weak typeof(self) weakSelf = self;
48-
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3. * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
48+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4. * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
4949
__strong typeof(weakSelf) strongSelf = weakSelf;
5050
[strongSelf load];
5151
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

SomoDemo/SomoDemo/Somo/Somo.h

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
//
2-
// Somo.h
3-
// SomoDemo
4-
//
5-
// Created by 向小辉 on 2017/11/25.
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+
*/
810

911
#ifndef Somo_h
1012
#define Somo_h

SomoDemo/SomoDemo/Somo/SomoDataSourceProvider.h

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
//
2-
// SomoDataSourceProvider.h
3-
// SomoDemo
4-
//
5-
// Created by 向小辉 on 2017/12/6.
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+
*/
810

911
#import <Foundation/Foundation.h>
1012
#import <UIKit/UIKit.h>

SomoDemo/SomoDemo/Somo/SomoDataSourceProvider.m

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
//
2-
// SomoDataSourceProvider.m
3-
// SomoDemo
4-
//
5-
// Created by 向小辉 on 2017/12/6.
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+
*/
810

911
#import "SomoDataSourceProvider.h"
1012
#import "UIView+SomoSkeleton.h"

SomoDemo/SomoDemo/Somo/SomoDefines.h

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
//
2-
// SomoDefines.h
3-
// SomoDemo
4-
//
5-
// Created by 向小辉 on 2017/11/25.
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+
*/
810

911
#ifndef SomoDefines_h
1012
#define SomoDefines_h

SomoDemo/SomoDemo/Somo/SomoSkeletonLayoutProtocol.h

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
//
2-
// SomoSkeletonLayoutProtocol.h
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+
*/
810

911
#ifndef SomoSkeletonLayoutProtocol_h
1012
#define SomoSkeletonLayoutProtocol_h

SomoDemo/SomoDemo/Somo/SomoView.h

+29-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1-
//
2-
// SomoView.h
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+
*/
810

911
#import <UIKit/UIKit.h>
1012

13+
typedef NS_ENUM(NSInteger,SomoAnimationStyle) {
14+
SomoAnimationStyleSolid,
15+
SomoAnimationStyleGradientHorizontal,
16+
SomoAnimationStyleGradientVertical,
17+
SomoAnimationStyleOblique
18+
};
19+
1120
/**
1221
* The essence of the Skeleton effect is the view of the placeholders.
1322
* For example, a Cell, when cells show the Skeleton effect,
@@ -16,13 +25,24 @@
1625
*/
1726
@interface SomoView : UIView
1827

19-
@property (strong, nonatomic) UIColor *somoColor;
28+
@property (strong, nonatomic) UIColor *somoColor;
29+
@property (assign, nonatomic) SomoAnimationStyle animationStyle;
2030

2131
/**
2232
* constructor
2333
*
2434
@return SomoView
2535
*/
26-
- (instancetype)initWithFrame:(CGRect)rect somoColor:(UIColor *)color;
36+
- (instancetype)initWithFrame:(CGRect)rect
37+
somoColor:(UIColor *)color;
38+
39+
/**
40+
* @style default SomoAnimationStyleSolid
41+
*
42+
@return SomoView
43+
*/
44+
- (instancetype)initWithFrame:(CGRect)rect
45+
somoColor:(UIColor *)color
46+
animationStyle:(SomoAnimationStyle)style;
2747

2848
@end

SomoDemo/SomoDemo/Somo/SomoView.m

+108-39
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
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+
*/
810

911
#import "SomoView.h"
1012
#import "SomoDefines.h"
@@ -20,61 +22,128 @@ @interface SomoView()
2022
@implementation SomoView
2123

2224
- (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];
2828
}
2929

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;
3437
}
3538
return self;
3639
}
3740

3841
- (instancetype)initWithFrame:(CGRect)frame{
3942
self = [super initWithFrame:frame];
4043
if (self) {
41-
[self setup];
44+
[self _setup];
4245
}
4346
return self;
4447
}
4548

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];
4958
}
59+
}
60+
61+
- (void)_setup{
62+
_somoColor = SomoColorFromRGBV(150);
63+
_animationStyle = SomoAnimationStyleSolid;
5064
self.backgroundColor = self.somoColor;
5165
self.layer.masksToBounds = YES;
52-
[self.layer addSublayer:self.somoLayer];
53-
54-
[self animate];
66+
[self _animate];
5567
}
5668

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+
}
64130
}
65131

66132
-(CAGradientLayer *)somoLayer{
67133
if (!_somoLayer) {
68134
_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];
78147
}
79148
return _somoLayer;
80149
}

SomoDemo/SomoDemo/Somo/UIView+SomoSkeleton.h

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
//
2-
// UIView+Somo.h
3-
// SomoDemo
4-
//
5-
// Created by 向小辉 on 2017/11/23.
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+
*/
810

911
#import <UIKit/UIKit.h>
1012

SomoDemo/SomoDemo/Somo/UIView+SomoSkeleton.m

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
//
2-
// UIView+Somo.m
3-
// SomoDemo
4-
//
5-
// Created by 向小辉 on 2017/11/23.
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+
*/
810
#import <objc/runtime.h>
911
#import "UIView+SomoSkeleton.h"
1012
#import "SomoSkeletonLayoutProtocol.h"

SomoDemo/SomoDemo/SomoListController.m

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ - (void)viewDidLoad {
1919

2020
self.tableView.tableFooterView = [UIView new];
2121
self.tableView.rowHeight = 80;
22-
2322
}
2423

2524
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

0 commit comments

Comments
 (0)