This repository has been archived by the owner on Nov 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
FLXSmoothView.m
80 lines (65 loc) · 2.49 KB
/
FLXSmoothView.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
//
// FLXSmoothView.m
// SmoothCorners
//
// Created by Felix Lapalme on 2019-03-24.
// Copyright © 2019 Felix Lapalme. All rights reserved.
//
#import "FLXSmoothView.h"
@interface FLXSmoothView()
@property (nonatomic, assign) CGFloat flx_lastSetRadius;
@property (nonatomic, strong) CAShapeLayer *flx_smoothMask;
@end
@implementation FLXSmoothView
-(void)setFlx_smoothCorners:(BOOL)flx_smoothCorners {
_flx_smoothCorners = flx_smoothCorners;
[self flx_updateMaskIfNeeded];
}
- (void)layoutSubviews {
[super layoutSubviews];
if (self.flx_smoothCorners) {
[self flx_updateMaskIfNeeded];
}
}
- (void)flx_updateMaskIfNeeded {
if (self.flx_smoothCorners) {
if (self.flx_smoothMask) {
// If it already exists we only update it if it changed
if (!CGRectEqualToRect(CGPathGetPathBoundingBox(self.flx_smoothMask.path), self.bounds)
|| self.flx_lastSetRadius != self.layer.cornerRadius) {
[self flx_updateMaskShape];
}
} else {
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
cornerRadius:self.layer.cornerRadius];
CAShapeLayer *mask = [CAShapeLayer layer];
mask.path = maskPath.CGPath;
self.layer.mask = mask;
self.flx_smoothMask = mask;
self.flx_lastSetRadius = self.layer.cornerRadius;
[self.layer addObserver:self
forKeyPath:NSStringFromSelector(@selector(cornerRadius))
options:0
context:nil];
}
}
else if (self.flx_smoothMask) {
self.layer.mask = nil;
self.flx_smoothMask = nil;
@try {
[self.layer removeObserver:self forKeyPath:NSStringFromSelector(@selector(cornerRadius))];
}
@catch (NSException * __unused exception) {}
}
}
- (void)flx_updateMaskShape {
self.flx_smoothMask.path = [UIBezierPath bezierPathWithRoundedRect:self.bounds
cornerRadius:self.layer.cornerRadius].CGPath;
self.flx_lastSetRadius = self.layer.cornerRadius;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
if ([keyPath isEqualToString:NSStringFromSelector(@selector(cornerRadius))]) {
[self flx_updateMaskIfNeeded];
}
}
@end