This repository has been archived by the owner on Apr 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
ABVolumeHUDIconView.m
102 lines (83 loc) · 3.06 KB
/
ABVolumeHUDIconView.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
//
// ABVolumeHUDIconView.m
// Ultrasound
//
// Created by Ayden Panhuyzen on 8/28/18.
// Copyright © 2018 Ayden Panhuyzen. All rights reserved.
//
#import "ABVolumeHUDIconView.h"
#import "ABVolumeHUDIconVolumeGlyph.h"
#import "ABVolumeHUDIconRingerGlyph.h"
@implementation ABVolumeHUDIconView {
UIView<ABVolumeHUDIconGlyphProviding> *glyphView;
NSLayoutConstraint *squareHeightConstraint;
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) [self setupForDisplay];
return self;
}
- (void)setupForDisplay {
self.userInteractionEnabled = YES;
NSLayoutConstraint *heightConstraint = [self.heightAnchor constraintEqualToConstant:22];
heightConstraint.priority = UILayoutPriorityDefaultLow;
heightConstraint.active = YES;
[self.widthAnchor constraintEqualToConstant:26].active = YES;
squareHeightConstraint = [self.heightAnchor constraintEqualToAnchor:self.widthAnchor];
self.backgroundColor = [UIColor whiteColor];
}
- (void)setupGlyphView:(UIView<ABVolumeHUDIconGlyphProviding> *)_glyphView {
if (glyphView) [glyphView removeFromSuperview];
glyphView = _glyphView;
glyphView.translatesAutoresizingMaskIntoConstraints = NO;
[self updateVolume];
self.maskView = glyphView;
}
- (void)layoutSubviews {
[super layoutSubviews];
glyphView.frame = CGRectMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds), 0, 0);
}
- (void)setVolume:(CGFloat)volume {
_volume = volume;
[self updateVolume];
}
- (void)updateVolume {
[glyphView setVolume:_modeInfo ? [_modeInfo iconVolumeForRealVolume:_volume] : _volume];
}
- (void)setMustBeSquare:(BOOL)mustBeSquare {
_mustBeSquare = mustBeSquare;
squareHeightConstraint.active = mustBeSquare;
}
- (void)setModeInfo:(ABVolumeHUDVolumeModeInfo *)modeInfo {
if (_modeInfo && _modeInfo.mode == modeInfo.mode) return;
_modeInfo = modeInfo;
[self setupGlyphView:modeInfo.iconGlyphProvider];
}
- (void)setIsTouchedDown:(BOOL)isTouchedDown {
if (_isTouchedDown == isTouchedDown) return;
_isTouchedDown = isTouchedDown;
CGFloat newScale = isTouchedDown ? 0.85 : 1;
[UIView animateWithDuration:0.2 delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:1 options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState animations:^{
self.transform = CGAffineTransformMakeScale(newScale, newScale);
} completion:nil];
}
- (void)handleTap {
if (![self canHandleTouches]) return;
[self.delegate tappedIconView:self];
}
- (BOOL)canHandleTouches {
return self.delegate && [self.delegate respondsToSelector:@selector(tappedIconView:)];
}
// MARK: - Touch Handling
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
self.isTouchedDown = [self canHandleTouches];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
self.isTouchedDown = NO;
[self isTouchedDown];
[self handleTap];
}
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
self.isTouchedDown = NO;
}
@end