-
Notifications
You must be signed in to change notification settings - Fork 0
/
WallpaperView.m
125 lines (102 loc) · 3.94 KB
/
WallpaperView.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
//
// WallpaperView.m
// W
//
// Created by liaa on 10/29/14.
// Copyright (c) 2014 kidliaa. All rights reserved.
//
#import "WallpaperView.h"
@implementation WallpaperView {
// 配置值
int floorCount; //从里到外层数总和
float baseHeight; //最里层(最低层)的高度
// 算出值
float differenceHeight;
int horizontalPiceCount;
float averWidth;
CGPoint commonAnchorPoint;
}
#pragma mark -
#pragma mark initialization
-(instancetype)initWithFrame:(CGRect)frame andColor:(UIColor *)color
{
self = [super initWithFrame:frame];
if (self) {
self.color = color;
[self setupSizeInfoAccordingToScreenSize];
[self uisetup];
}
return self;
}
-(void)setupSizeInfoAccordingToScreenSize
{
float viewWidth = self.frame.size.width;
float viewHeight = self.frame.size.height;
// 配置值
floorCount = 8;
baseHeight = viewHeight * 0.6;
// 算出值
horizontalPiceCount = floorCount * 2 - 1;
averWidth = viewWidth / horizontalPiceCount;
differenceHeight = viewHeight - baseHeight;
commonAnchorPoint = CGPointMake(viewWidth/2, viewHeight);
}
#pragma mark -
#pragma mark uisetup
- (void) uiuColor
{
UIColor *finalColor = self.color;
for (int i = 0; i < floorCount; i++) {
float normalizedValueBaseOnNestedLevel = i / (floorCount - 1.0); //根据层级得到一个标准化的值(0~1)用来计算不同层级的 1)颜色值 2)高度值
const CGFloat *compoments = CGColorGetComponents(finalColor.CGColor);
CGFloat r = 0.0 + compoments[0] * normalizedValueBaseOnNestedLevel;
CGFloat g = 0.0 + compoments[1] * normalizedValueBaseOnNestedLevel;
CGFloat b = 0.0 + compoments[2] * normalizedValueBaseOnNestedLevel;
CALayer *layer = self.layer.sublayers[(floorCount-1)-i];
layer.backgroundColor = [UIColor colorWithRed:r green:g blue:b alpha:1].CGColor;
}
}
- (void) uiuLayersFrame
{
for (int i = 0; i < floorCount; i++) {
float normalizedValueBaseOnNestedLevel = i / (floorCount - 1.0); //根据层级得到一个标准化的值(0~1)用来计算不同层级的 1)颜色值 2)高度值
float width = averWidth * (1 + i * 2);
float height = baseHeight + differenceHeight * normalizedValueBaseOnNestedLevel;
CALayer *layer = self.layer.sublayers[(floorCount-1)-i];
layer.bounds = CGRectMake(0, 0, width, height);
layer.position = commonAnchorPoint;
layer.anchorPoint = CGPointMake(0.5, 1);
}
}
- (void) uisetup {
UIColor *finalColor = self.color;
// 绘制背景 Layers
for (int i = 0; i < floorCount; i++) {
float normalizedValueBaseOnNestedLevel = i / (floorCount - 1.0); //根据层级得到一个标准化的值(0~1)用来计算不同层级的 1)颜色值 2)高度值
float width = averWidth * (1 + i * 2);
float height = baseHeight + differenceHeight * normalizedValueBaseOnNestedLevel;
const CGFloat *compoments = CGColorGetComponents(finalColor.CGColor);
CGFloat r = 0.0 + compoments[0] * normalizedValueBaseOnNestedLevel;
CGFloat g = 0.0 + compoments[1] * normalizedValueBaseOnNestedLevel;
CGFloat b = 0.0 + compoments[2] * normalizedValueBaseOnNestedLevel;
CALayer *layer;
if (self.layer.sublayers[i] != nil) {
layer = self.layer.sublayers[i];
} else {
layer = [CALayer layer];
layer.position = commonAnchorPoint;
layer.anchorPoint = CGPointMake(0.5, 1);
}
layer.bounds = CGRectMake(0, 0, width, height);
layer.backgroundColor = [UIColor colorWithRed:r green:g blue:b alpha:1].CGColor;
[self.layer insertSublayer:layer atIndex:0];
}
}
#pragma mark -
#pragma mark handle rotation
-(void)layoutSubviews
{
[self setupSizeInfoAccordingToScreenSize];
[self uiuLayersFrame];
}
@end