質問編集履歴

2

コードの編集

2018/10/10 08:52

投稿

Shichi7
Shichi7

スコア35

test CHANGED
File without changes
test CHANGED
@@ -39,10 +39,6 @@
39
39
  override func draw(_ rect: CGRect) {
40
40
 
41
41
  super.draw(rect)
42
-
43
-
44
-
45
- print("半透明レイヤー実行")
46
42
 
47
43
 
48
44
 

1

コードを更新

2018/10/10 08:52

投稿

Shichi7
Shichi7

スコア35

test CHANGED
File without changes
test CHANGED
@@ -4,19 +4,121 @@
4
4
 
5
5
  ```
6
6
 
7
+ import UIKit
8
+
9
+
10
+
11
+ class overlayCameraVIew: UIView {
12
+
13
+
14
+
15
+ lazy var hollowPoint: CGPoint = {
16
+
17
+ return CGPoint(
18
+
19
+ x: self.bounds.width / 2.0,
20
+
21
+ y: self.bounds.height / 2.0
22
+
23
+ )
24
+
25
+ }()
26
+
27
+
28
+
29
+ override func awakeFromNib() {
30
+
31
+ super.awakeFromNib()
32
+
33
+ self.backgroundColor = UIColor.clear
34
+
35
+ }
36
+
37
+
38
+
7
- override func draw(_ rect: CGRect) {
39
+ override func draw(_ rect: CGRect) {
8
40
 
9
41
  super.draw(rect)
10
42
 
43
+
11
44
 
45
+ print("半透明レイヤー実行")
46
+
47
+
48
+
49
+ // 繰り抜きたいレイヤーを作成する(今回は例として半透明にした)
12
50
 
13
51
  let hollowTargetLayer = CALayer()
14
52
 
15
53
  hollowTargetLayer.bounds = self.bounds
16
54
 
55
+ hollowTargetLayer.position = CGPoint(
56
+
57
+ x: self.bounds.width / 2.0,
58
+
59
+ y: self.bounds.height / 2.0
60
+
61
+ )
62
+
17
63
  hollowTargetLayer.backgroundColor = UIColor.black.cgColor
18
64
 
19
65
  hollowTargetLayer.opacity = 0.7
66
+
67
+
68
+
69
+ // 四角いマスクレイヤーを作る
70
+
71
+ let maskLayer = CAShapeLayer()
72
+
73
+ maskLayer.bounds = hollowTargetLayer.bounds
74
+
75
+
76
+
77
+ // 塗りを反転させるために、pathに四角いマスクレイヤーを重ねる
78
+
79
+ let halfWidth = (self.bounds.width / 2) - 32 as CGFloat
80
+
81
+ let ovalRect = CGRect(
82
+
83
+ x: self.hollowPoint.x - halfWidth,
84
+
85
+ y: self.hollowPoint.y - halfWidth,
86
+
87
+ width: halfWidth * 2.0,
88
+
89
+ height: halfWidth * 2.0
90
+
91
+ )
92
+
93
+ // 完全な円
94
+
95
+ //let path = UIBezierPath(ovalIn: ovalRect)
96
+
97
+ // 角丸の四角
98
+
99
+ let path = UIBezierPath(roundedRect: ovalRect, cornerRadius: 24)
100
+
101
+ path.append(UIBezierPath(rect: maskLayer.bounds))
102
+
103
+
104
+
105
+ maskLayer.fillColor = UIColor.black.cgColor
106
+
107
+ maskLayer.path = path.cgPath
108
+
109
+ maskLayer.position = CGPoint(
110
+
111
+ x: hollowTargetLayer.bounds.width / 2.0,
112
+
113
+ y: hollowTargetLayer.bounds.height / 2.0
114
+
115
+ )
116
+
117
+ // マスクのルールをeven/oddに設定する
118
+
119
+ maskLayer.fillRule = kCAFillRuleEvenOdd
120
+
121
+ hollowTargetLayer.mask = maskLayer
20
122
 
21
123
 
22
124
 
@@ -26,4 +128,6 @@
26
128
 
27
129
  }
28
130
 
131
+ }
132
+
29
133
  ```