Skip to content

Commit bf5bbc6

Browse files
committed
update UI section
1 parent 2276f82 commit bf5bbc6

File tree

8 files changed

+216
-6
lines changed

8 files changed

+216
-6
lines changed

UITheory/UITheory.xcodeproj/project.pbxproj

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
/* Begin PBXBuildFile section */
1010
17438E121D50294500550770 /* ViewLayer.m in Sources */ = {isa = PBXBuildFile; fileRef = 17438E111D50294500550770 /* ViewLayer.m */; };
11+
1754FE191D51D10700C8CA2B /* CGViewDIY.m in Sources */ = {isa = PBXBuildFile; fileRef = 1754FE181D51D10700C8CA2B /* CGViewDIY.m */; };
12+
1754FE1C1D51D39100C8CA2B /* Test4UI.m in Sources */ = {isa = PBXBuildFile; fileRef = 1754FE1B1D51D39100C8CA2B /* Test4UI.m */; };
13+
1754FE1D1D51D63100C8CA2B /* Test4UI.m in Sources */ = {isa = PBXBuildFile; fileRef = 1754FE1B1D51D39100C8CA2B /* Test4UI.m */; };
14+
1754FE1E1D51D63200C8CA2B /* Test4UI.m in Sources */ = {isa = PBXBuildFile; fileRef = 1754FE1B1D51D39100C8CA2B /* Test4UI.m */; };
15+
1754FE201D51E05100C8CA2B /* CGViewREADME.md in Sources */ = {isa = PBXBuildFile; fileRef = 1754FE1F1D51E05100C8CA2B /* CGViewREADME.md */; };
1116
17E34A211D4F1BCB00489103 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 17E34A201D4F1BCB00489103 /* main.m */; };
1217
17E34A241D4F1BCB00489103 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 17E34A231D4F1BCB00489103 /* AppDelegate.m */; };
1318
17E34A271D4F1BCB00489103 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 17E34A261D4F1BCB00489103 /* ViewController.m */; };
@@ -39,6 +44,11 @@
3944
/* Begin PBXFileReference section */
4045
17438E101D50294500550770 /* ViewLayer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ViewLayer.h; sourceTree = "<group>"; };
4146
17438E111D50294500550770 /* ViewLayer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ViewLayer.m; sourceTree = "<group>"; };
47+
1754FE171D51D10700C8CA2B /* CGViewDIY.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CGViewDIY.h; sourceTree = "<group>"; };
48+
1754FE181D51D10700C8CA2B /* CGViewDIY.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CGViewDIY.m; sourceTree = "<group>"; };
49+
1754FE1A1D51D39100C8CA2B /* Test4UI.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Test4UI.h; sourceTree = "<group>"; };
50+
1754FE1B1D51D39100C8CA2B /* Test4UI.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Test4UI.m; sourceTree = "<group>"; };
51+
1754FE1F1D51E05100C8CA2B /* CGViewREADME.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = CGViewREADME.md; sourceTree = "<group>"; };
4252
17E34A1C1D4F1BCB00489103 /* UITheory.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = UITheory.app; sourceTree = BUILT_PRODUCTS_DIR; };
4353
17E34A201D4F1BCB00489103 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
4454
17E34A221D4F1BCB00489103 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
@@ -91,6 +101,16 @@
91101
path = TableView;
92102
sourceTree = "<group>";
93103
};
104+
1754FE161D51D0EA00C8CA2B /* DIY */ = {
105+
isa = PBXGroup;
106+
children = (
107+
1754FE171D51D10700C8CA2B /* CGViewDIY.h */,
108+
1754FE181D51D10700C8CA2B /* CGViewDIY.m */,
109+
1754FE1F1D51E05100C8CA2B /* CGViewREADME.md */,
110+
);
111+
path = DIY;
112+
sourceTree = "<group>";
113+
};
94114
17E34A131D4F1BCB00489103 = {
95115
isa = PBXGroup;
96116
children = (
@@ -114,16 +134,19 @@
114134
17E34A1E1D4F1BCB00489103 /* UITheory */ = {
115135
isa = PBXGroup;
116136
children = (
117-
173E93DD1D508F1A00F5762F /* TableView */,
118-
17E34A521D4F227F00489103 /* View */,
119137
17E34A221D4F1BCB00489103 /* AppDelegate.h */,
120138
17E34A231D4F1BCB00489103 /* AppDelegate.m */,
121139
17E34A251D4F1BCB00489103 /* ViewController.h */,
122140
17E34A261D4F1BCB00489103 /* ViewController.m */,
141+
1754FE1A1D51D39100C8CA2B /* Test4UI.h */,
142+
1754FE1B1D51D39100C8CA2B /* Test4UI.m */,
143+
17E34A301D4F1BCB00489103 /* Info.plist */,
123144
17E34A2B1D4F1BCB00489103 /* Assets.xcassets */,
145+
1754FE161D51D0EA00C8CA2B /* DIY */,
124146
17E34A2D1D4F1BCB00489103 /* LaunchScreen.storyboard */,
125-
17E34A301D4F1BCB00489103 /* Info.plist */,
126147
17E34A1F1D4F1BCB00489103 /* Supporting Files */,
148+
173E93DD1D508F1A00F5762F /* TableView */,
149+
17E34A521D4F227F00489103 /* View */,
127150
);
128151
path = UITheory;
129152
sourceTree = "<group>";
@@ -299,7 +322,10 @@
299322
17E34A5D1D4F364600489103 /* ViewREADME.md in Sources */,
300323
17438E121D50294500550770 /* ViewLayer.m in Sources */,
301324
17E34A271D4F1BCB00489103 /* ViewController.m in Sources */,
325+
1754FE191D51D10700C8CA2B /* CGViewDIY.m in Sources */,
302326
17E34A241D4F1BCB00489103 /* AppDelegate.m in Sources */,
327+
1754FE1C1D51D39100C8CA2B /* Test4UI.m in Sources */,
328+
1754FE201D51E05100C8CA2B /* CGViewREADME.md in Sources */,
303329
17E34A211D4F1BCB00489103 /* main.m in Sources */,
304330
);
305331
runOnlyForDeploymentPostprocessing = 0;
@@ -308,6 +334,7 @@
308334
isa = PBXSourcesBuildPhase;
309335
buildActionMask = 2147483647;
310336
files = (
337+
1754FE1D1D51D63100C8CA2B /* Test4UI.m in Sources */,
311338
17E34A3A1D4F1BCB00489103 /* UITheoryTests.m in Sources */,
312339
);
313340
runOnlyForDeploymentPostprocessing = 0;
@@ -316,6 +343,7 @@
316343
isa = PBXSourcesBuildPhase;
317344
buildActionMask = 2147483647;
318345
files = (
346+
1754FE1E1D51D63200C8CA2B /* Test4UI.m in Sources */,
319347
17E34A451D4F1BCC00489103 /* UITheoryUITests.m in Sources */,
320348
);
321349
runOnlyForDeploymentPostprocessing = 0;

UITheory/UITheory.xcodeproj/project.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UITheory/UITheory/DIY/CGViewDIY.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
CGViewDIY.h
3+
4+
Reference(for more effects):
5+
http://www.cocoachina.com/industry/20140115/7703.html
6+
7+
Created by coder4869 on 8/3/16.
8+
Copyright © 2016 coder4869. All rights reserved.
9+
*/
10+
11+
#import <UIKit/UIKit.h>
12+
13+
@interface CGViewDIY : UIView
14+
15+
@end

UITheory/UITheory/DIY/CGViewDIY.m

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
CGViewDIY.m
3+
4+
Created by coder4869 on 8/3/16.
5+
Copyright © 2016 coder4869. All rights reserved.
6+
*/
7+
8+
#import "CGViewDIY.h"
9+
10+
@implementation CGViewDIY
11+
12+
// Only override drawRect: if you perform custom drawing.
13+
// An empty implementation adversely affects performance during animation.
14+
- (void)drawRect:(CGRect)rect {
15+
16+
CGContextRef con = UIGraphicsGetCurrentContext();
17+
CGContextSaveGState(con);
18+
19+
// 从箭头杆子上裁掉一个三角形,使用清除混合模式
20+
CGContextMoveToPoint(con, 45, 100);
21+
CGContextAddLineToPoint(con, 50, 90);
22+
CGContextAddLineToPoint(con, 55, 100);
23+
CGContextClosePath(con);
24+
CGContextAddRect(con, CGContextGetClipBoundingBox(con));
25+
// 使用奇偶规则,裁剪区域为矩形减去三角形区域
26+
CGContextEOClip(con);
27+
28+
// 绘制一个黑色的垂直黑色线,作为箭头的杆子
29+
CGContextMoveToPoint(con, 50, 100);
30+
CGContextAddLineToPoint(con, 50, 20);
31+
CGContextSetLineWidth(con, 10);
32+
CGContextStrokePath(con);
33+
34+
// 使用路径的描边版本替换图形上下文的路径
35+
CGContextReplacePathWithStrokedPath(con);
36+
37+
// 对路径的描边版本实施裁剪
38+
CGContextClip(con);
39+
// 绘制渐变
40+
CGFloat locs[3] = { 0.0, 0.5, 1.0 };
41+
CGFloat colors[12] = {
42+
0.3,0.3,0.3,0.8, // 开始颜色,透明灰
43+
0.0,0.0,0.0,1.0, // 中间颜色,黑色
44+
0.3,0.3,0.3,0.8 // 末尾颜色,透明灰
45+
};
46+
CGColorSpaceRef sp = CGColorSpaceCreateDeviceGray();
47+
CGGradientRef grad = CGGradientCreateWithColorComponents (sp, colors, locs, 3);
48+
CGContextDrawLinearGradient(con, grad, CGPointMake(89,0), CGPointMake(111,0), 0);
49+
CGColorSpaceRelease(sp);
50+
CGGradientRelease(grad);
51+
CGContextRestoreGState(con); // 完成裁剪
52+
53+
// 绘制一个红色三角形箭头
54+
CGContextSetFillColorWithColor(con, [[UIColor redColor] CGColor]);
55+
CGContextMoveToPoint(con, 30, 20);
56+
CGContextAddLineToPoint(con, 50, 0);
57+
CGContextAddLineToPoint(con, 70, 20);
58+
CGContextFillPath(con);
59+
60+
//绘制左上和右下为圆角的矩形
61+
CGContextRef ctx = UIGraphicsGetCurrentContext();
62+
CGContextSetStrokeColorWithColor(ctx, [UIColor blackColor].CGColor);
63+
CGContextSetLineWidth(ctx, 3);
64+
UIBezierPath *path;
65+
path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 110, 80, 70) byRoundingCorners:(UIRectCornerTopLeft |UIRectCornerTopRight)cornerRadii:CGSizeMake(10, 10)];
66+
[path stroke];
67+
}
68+
69+
@end

UITheory/UITheory/DIY/CGViewREADME.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
6. Core Graphics
2+
6.1 Core Graphics上下文属性设置
3+
图形上下文的相关属性设置会决定绘图的行为与外观。绘图的一般过程为:先设定好图形上下文参数,然后绘图。例如:先画一根红线,接着画一根蓝线。绘制流程为:将上下文的线条颜色属性设定为为红色,然后画红线;接着设置上下文的线条颜色属性为蓝色,再画蓝线。画线时,线条颜色是整个上下文的属性。用UIKit方法或Core Graphics函数都一样。
4+
图形上下文在不同时刻的状态不一样,这些状态有专门的状态栈存储。状态入栈由函数CGContextSaveGState控制,查找栈订状态用CGContextRestoreGState函数。
5+
一般绘图模式是:
6+
[1].在绘图之前调用CGContextSaveGState函数保存当前状态,
7+
[2].根据需要设置某些上下文状态,然后绘图。
8+
[3].最后调用CGContextRestoreGState函数将当前状态恢复到绘图之前的状态。
9+
CGContextSaveGState函数和CGContextRestoreGState函数必须成对出现,否则会出现奇怪的异常。示例代码如下:
10+
- (void)drawRect:(CGRect)rect {
11+
CGContextRef ctx = UIGraphicsGetCurrentContext();
12+
CGContextSaveGState(ctx);
13+
{
14+
// 绘图代码
15+
}
16+
CGContextRestoreGState(ctx);
17+
}
18+
并非每次修改上下文状态之前都这样做,因为上下文属性的设置未必会跟之前有冲突。可以在不调用保存和恢复函数的情况下先设置线条颜色为红色,然后再设置为蓝色。若要使状态的设置可撤销,参见后续示例。
19+
常用属性和对应修改函数如下(与Core Graphics一样, UIKit也通过这些函数操纵上下文状态):
20+
[1].线条的宽度和线条的虚线样式:CGContextSetLineWidth、CGContextSetLineDash
21+
[2].线帽和线条联接点样式:CGContextSetLineCap、CGContextSetLineJoin、CGContextSetMiterLimit
22+
[3].线条颜色和线条模式CGContextSetRGBStrokeColor、CGContextSetGrayStrokeColor、CGContextSetStrokeColorWithColor、CGContextSetStrokePattern
23+
[4].填充颜色和模式:CGContextSetFillColorWithColor,CGContextSetRGBFillColor,CGContextSetGrayFillColor,CGContextSetFillPattern
24+
[5].阴影:CGContextSetShadow、CGContextSetShadowWithColor
25+
[6].混合模式:CGContextSetBlendMode(决定当前绘制的图形与已经存在的图形如何被合成)
26+
[7].整体透明度:CGContextSetAlpha(个别颜色也具有alpha成分)
27+
[8].文本属性:CGContextSetTextDrawingMode、CGContextSetCharacterSpacing 、CGContextSelectFont、CGContextSetFont、CGContextSetFontSize
28+
[9].反锯齿和字体平滑设置:CGContextSetShouldAntialias、CGContextSetShouldSmoothFonts
29+
[10].裁剪区域:在裁剪区域外绘图不会被实际的画出来。
30+
[11].变换(或称为“CTM“,意为当前变换矩阵):改变随后指定的绘图命令中的点如何被映射到画布的物理空间。
31+
6.2 路径与绘图
32+
通过编写移动虚拟画笔的代码描画一段路径,这样的路径并不构成一个图形。绘制路径意味着对路径描边或填充该路径,也或者两者都做。常用路径描画命令:
33+
[1].定位当前点:CGContextMoveToPoint
34+
[2].描画一条线:CGContextAddLineToPoint、CGContextAddLines
35+
[3].描画一个矩形:CGContextAddRect、CGContextAddRects
36+
[4].描画一个椭圆或圆形:CGContextAddEllipseInRect
37+
[5].描画一段圆弧:CGContextAddArcToPoint、CGContextAddArc
38+
[6].通过一到两个控制点描画一段贝赛尔曲线:CGContextAddQuadCurveToPoint、CGContextAddCurveToPoint
39+
[7].关闭当前路径:CGContextClosePath 这将从路径的终点到起点追加一条线。若要填充一段路径,就不需要使用该命令,因为该命令会被自动调用。
40+
[8].描边或填充当前路径CGContextStrokePath、CGContextFillPath、CGContextEOFillPath、CGContextDrawPath。对当前路径描边或填充会清除掉路径。要用一条命令完成描边和填充任务,可以使用CGContextDrawPath命令。如果只用CGContextStrokePath对路径描边,路径就会被清除掉,就不能再对它进行填充了。
41+
[9].用一条命令可以创建路径并描边路径或填充路径的函数:CGContextStrokeLineSegments、CGContextStrokeRect、CGContextStrokeRectWithWidth、CGContextFillRect、CGContextFillRects、CGContextStrokeEllipseInRect、CGContextFillEllipseInRect。
42+
[10].并非每条独立路径都要用CGContextBeginPath函数指定
43+
[11].CGContextClearRect函数的功能是擦除一个区域(矩形内的所有已存在的绘图;并对该区域执行裁剪)。结果像是打了一个贯穿所有已存在绘图的孔。其行为依赖于上下文是透明与否。如果图片上下文是透明的(UIGraphicsBeginImageContextWithOptions第二个参数为NO),那么CGContextClearRect函数执行擦除后的颜色为透明,反之则为黑色。
44+
[12].当在一个视图中直接绘图(使用drawRect:或drawLayer:inContext:方法),如果视图的背景颜色为nil或颜色哪怕有一点点透明度,那么CGContextClearRect的矩形区域将会显示为透明的,打出的孔将穿过视图包括它的背景颜色。如果背景颜色完全不透明,那么CGContextClearRect函数的结果将会是黑色。这是因为视图的背景颜色决定了是否视图的图形上下文是透明的还是不透明的。

UITheory/UITheory/Test4UI.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
Test4UI.h
3+
4+
Created by coder4869 on 8/3/16.
5+
Copyright © 2016 coder4869. All rights reserved.
6+
*/
7+
8+
#import <UIKit/UIKit.h>
9+
#import <Foundation/Foundation.h>
10+
11+
@interface Test4UI : NSObject
12+
13+
-(void)test4CGViewDIY:(UIView*)parent;
14+
15+
-(void)test4ViewProperty:(UIView*)parent;
16+
17+
@end

UITheory/UITheory/Test4UI.m

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Test4UI.m
3+
4+
Created by coder4869 on 8/3/16.
5+
Copyright © 2016 coder4869. All rights reserved.
6+
*/
7+
8+
#import "Test4UI.h"
9+
10+
#import "CGViewDIY.h"
11+
#import "ViewProperty.h"
12+
13+
@implementation Test4UI
14+
15+
-(void)test4CGViewDIY:(UIView*)parent {
16+
CGViewDIY * diy = [[CGViewDIY alloc] initWithFrame:CGRectMake(0, 70, 120, 200)];
17+
[diy setBackgroundColor:[UIColor whiteColor]];
18+
[parent addSubview:diy];
19+
}
20+
21+
-(void)test4ViewProperty:(UIView*)parent {
22+
ViewProperty *vp = [[ViewProperty alloc] initWithFrame:CGRectMake(120, 70, 200, 200)];
23+
[vp demo4FrameBoundsCenter:NO]; //参数为是否隐藏超出边界的子视图
24+
[parent addSubview:vp];
25+
}
26+
27+
@end

UITheory/UITheory/ViewController.m

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#import "ViewController.h"
99

10+
#import "Test4UI.h"
1011
#import "ViewProperty.h"
1112

1213
@interface ViewController ()
@@ -19,9 +20,11 @@ - (void)viewDidLoad {
1920
[super viewDidLoad];
2021
// Do any additional setup after loading the view, typically from a nib.
2122

22-
ViewProperty *vp = [[ViewProperty alloc] initWithFrame:CGRectMake(100, 70, 200, 200)];
23-
[vp demo4FrameBoundsCenter:NO]; //参数为是否隐藏超出边界的子视图
24-
[self.view addSubview:vp];
23+
[self.view setBackgroundColor:[UIColor whiteColor]];
24+
25+
Test4UI *testUI = [[Test4UI alloc] init];
26+
[testUI test4CGViewDIY:self.view];
27+
[testUI test4ViewProperty:self.view];
2528
}
2629

2730

0 commit comments

Comments
 (0)