Loading...
Searching...
No Matches
NCZoomControls.m
Go to the documentation of this file.
1//
2// NCZoomControls.m
3// Zoom controls widget for navigation view
4//
5
6#import "NCZoomControls.h"
8#import "NCWidgetStyles.h"
9
10@interface NCZoomControls ()
11@property (nonatomic, copy) NCZoomInCallback onZoomIn;
12@property (nonatomic, copy) NCZoomOutCallback onZoomOut;
13@property (nonatomic, strong, nullable) NCZoomControlsConfig *config;
14@property (nonatomic, strong) UIStackView *stackView;
15@property (nonatomic, strong, nullable) NSLayoutConstraint *stackWidthConstraint;
16@property (nonatomic, strong, nullable) NSLayoutConstraint *stackHeightConstraint;
17@end
18
19@implementation NCZoomControls
20
21- (instancetype)initWithOnZoomIn:(NCZoomInCallback)onZoomIn onZoomOut:(NCZoomOutCallback)onZoomOut {
22 return [self initWithOnZoomIn:onZoomIn onZoomOut:onZoomOut config:nil];
23}
24
25- (instancetype)initWithOnZoomIn:(NCZoomInCallback)onZoomIn
26 onZoomOut:(NCZoomOutCallback)onZoomOut
27 config:(NCZoomControlsConfig *)config {
28 self = [super init];
29 if (self) {
30 _onZoomIn = [onZoomIn copy];
31 _onZoomOut = [onZoomOut copy];
32 _config = config;
33 [self setupUI];
34 }
35 return self;
36}
37
38- (void)setupUI {
40 CGFloat width = c.buttonWidth > 0 ? c.buttonWidth : kNCStandardButtonWidth;
43 UIColor *tintColor = c.textColor ?: kNCBaseBlackColor();
44
45 UIButton *zoomOutButton = [UIButton buttonWithType:UIButtonTypeSystem];
46 UIButton *zoomInButton = [UIButton buttonWithType:UIButtonTypeSystem];
47
48 if (c.zoomInIcon) {
49 [zoomInButton setImage:[c.zoomInIcon imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
50 zoomInButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
51 } else {
52 [zoomInButton setTitle:@"+" forState:UIControlStateNormal];
53 }
54 if (c.zoomOutIcon) {
55 [zoomOutButton setImage:[c.zoomOutIcon imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
56 zoomOutButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
57 } else {
58 [zoomOutButton setTitle:@"−" forState:UIControlStateNormal];
59 }
60
61 zoomOutButton.backgroundColor = bgColor;
62 zoomInButton.backgroundColor = bgColor;
63 zoomOutButton.tintColor = tintColor;
64 zoomInButton.tintColor = tintColor;
65
66 zoomOutButton.layer.cornerRadius = kNCBorderRadius;
67 zoomOutButton.layer.maskedCorners = kCALayerMinXMaxYCorner | kCALayerMaxXMaxYCorner;
68 zoomInButton.layer.cornerRadius = kNCBorderRadius;
69 zoomInButton.layer.maskedCorners = kCALayerMinXMinYCorner | kCALayerMaxXMinYCorner;
70
71 zoomOutButton.titleLabel.font = [UIFont systemFontOfSize:kNCButtonFontSize];
72 zoomInButton.titleLabel.font = [UIFont systemFontOfSize:kNCButtonFontSize];
73
74 [zoomOutButton addTarget:self action:@selector(zoomOutPressed:) forControlEvents:UIControlEventTouchUpInside];
75 [zoomInButton addTarget:self action:@selector(zoomInPressed:) forControlEvents:UIControlEventTouchUpInside];
76
77 UIStackView *stack = [[UIStackView alloc] initWithArrangedSubviews:@[zoomInButton, zoomOutButton]];
78 stack.axis = UILayoutConstraintAxisVertical;
79 stack.distribution = UIStackViewDistributionFillEqually;
80 stack.spacing = 0;
81 stack.translatesAutoresizingMaskIntoConstraints = NO;
82 stack.layer.shadowOpacity = kNCShadowOpacity;
83 stack.layer.shadowRadius = kNCShadowRadius;
84 stack.layer.shadowOffset = kNCShadowOffset;
85 stack.layer.shadowColor = kNCShadowColor().CGColor;
86 _stackView = stack;
87
88 [self addSubview:stack];
89
90 self.translatesAutoresizingMaskIntoConstraints = NO;
91
92 [NSLayoutConstraint activateConstraints:@[
93 [stack.leadingAnchor constraintEqualToAnchor:self.leadingAnchor],
94 [stack.trailingAnchor constraintEqualToAnchor:self.trailingAnchor],
95 [stack.topAnchor constraintEqualToAnchor:self.topAnchor],
96 [stack.bottomAnchor constraintEqualToAnchor:self.bottomAnchor],
97 ]];
98 self.stackWidthConstraint = [stack.widthAnchor constraintEqualToConstant:width];
99 self.stackHeightConstraint = [stack.heightAnchor constraintEqualToConstant:height];
100 self.stackWidthConstraint.active = YES;
101 self.stackHeightConstraint.active = YES;
102}
103
104- (void)applyConfig:(NCZoomControlsConfig *)config {
105 _config = config;
106 if (!_stackView || _stackView.arrangedSubviews.count < 2) return;
107 UIButton *zoomInButton = (UIButton *)_stackView.arrangedSubviews[0];
108 UIButton *zoomOutButton = (UIButton *)_stackView.arrangedSubviews[1];
109
112 UIColor *tintColor = c.textColor ?: kNCBaseBlackColor();
113
114 if (c.zoomInIcon) {
115 [zoomInButton setImage:[c.zoomInIcon imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
116 [zoomInButton setTitle:nil forState:UIControlStateNormal];
117 } else {
118 [zoomInButton setImage:nil forState:UIControlStateNormal];
119 [zoomInButton setTitle:@"+" forState:UIControlStateNormal];
120 }
121 if (c.zoomOutIcon) {
122 [zoomOutButton setImage:[c.zoomOutIcon imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
123 [zoomOutButton setTitle:nil forState:UIControlStateNormal];
124 } else {
125 [zoomOutButton setImage:nil forState:UIControlStateNormal];
126 [zoomOutButton setTitle:@"−" forState:UIControlStateNormal];
127 }
128
129 zoomInButton.backgroundColor = bgColor;
130 zoomOutButton.backgroundColor = bgColor;
131 zoomInButton.tintColor = tintColor;
132 zoomOutButton.tintColor = tintColor;
133
134 CGFloat width = c.buttonWidth > 0 ? c.buttonWidth : kNCStandardButtonWidth;
136 if (self.stackWidthConstraint) {
137 self.stackWidthConstraint.constant = width;
138 }
139 if (self.stackHeightConstraint) {
140 self.stackHeightConstraint.constant = height;
141 }
142}
143
144- (void)zoomOutPressed:(UIButton *)sender {
145 if (self.onZoomOut) {
146 self.onZoomOut();
147 }
148}
149
150- (void)zoomInPressed:(UIButton *)sender {
151 if (self.onZoomIn) {
152 self.onZoomIn();
153 }
154}
155
156@end
157