Loading...
Searching...
No Matches
NCFollowMeButton.m
Go to the documentation of this file.
1//
2// NCFollowMeButton.m
3// Follow Me button widget for navigation view
4//
5
6#import "NCFollowMeButton.h"
8#import "NCWidgetStyles.h"
9
10@interface NCFollowMeButton ()
11@property (nonatomic, copy) NCFollowMeButtonCallback onPressed;
12@property (nonatomic, strong) UIImageView *arrow;
13@property (nonatomic, readwrite, getter=isFollowing) BOOL following;
14@property (nonatomic, strong, nullable) NCFollowMeButtonConfig *config;
15@end
16
17@implementation NCFollowMeButton
18
19- (instancetype)initWithOnPressed:(NCFollowMeButtonCallback)onPressed {
20 return [self initWithOnPressed:onPressed config:nil];
21}
22
23- (instancetype)initWithOnPressed:(NCFollowMeButtonCallback)onPressed
24 config:(NCFollowMeButtonConfig *)config {
25 self = [super initWithFrame:CGRectZero];
26 if (self) {
27 _onPressed = [onPressed copy];
28 _following = NO;
29 _config = config;
30 [self setupUI];
31 }
32 return self;
33}
34
35- (void)setupUI {
37 self.backgroundColor = c.buttonBackgroundColor ?: kNCButtonBackgroundColor();
38 self.layer.cornerRadius = kNCBorderRadius;
39 self.layer.masksToBounds = NO;
40 self.layer.shadowOpacity = kNCShadowOpacity;
41 self.layer.shadowRadius = kNCShadowRadius;
42 self.layer.shadowOffset = kNCShadowOffset;
43 self.layer.shadowColor = kNCShadowColor().CGColor;
44
45 [self addTarget:self
46 action:@selector(followMePressed:)
47 forControlEvents:UIControlEventTouchUpInside];
48
49 self.translatesAutoresizingMaskIntoConstraints = NO;
50
51 UIImage *image = c.followMeIcon;
52 if (!image) {
53 NSBundle *bundle = [NSBundle bundleWithIdentifier:@"com.navigine"];
54 image = [UIImage imageNamed:@"navigine_follow_me" inBundle:bundle withConfiguration:nil];
55 }
56 image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
57
58 self.arrow = [[UIImageView alloc] initWithImage:image];
59 self.arrow.tintColor = c.textColor ?: kNCBaseBlackColor();
60
61 self.arrow.translatesAutoresizingMaskIntoConstraints = NO;
62 self.arrow.contentMode = UIViewContentModeScaleAspectFit;
63
64 [self addSubview:self.arrow];
65
66 CGFloat padding = 8.0f;
67
68 [NSLayoutConstraint activateConstraints:@[
69 [self.arrow.topAnchor constraintEqualToAnchor:self.topAnchor constant:padding],
70 [self.arrow.leadingAnchor constraintEqualToAnchor:self.leadingAnchor constant:padding],
71 [self.arrow.trailingAnchor constraintEqualToAnchor:self.trailingAnchor constant:-padding],
72 [self.arrow.bottomAnchor constraintEqualToAnchor:self.bottomAnchor constant:-padding]
73 ]];
74
75 [self updateAppearanceForFollowingState:NO];
76}
77
78- (void)followMePressed:(UIButton *)sender {
79 self.following = !self.isFollowing;
80 [self updateAppearanceForFollowingState:self.following];
81
82 if (self.onPressed) {
83 self.onPressed(self.isFollowing);
84 }
85}
86
87- (UIImage *)defaultFollowMeImage {
88 NSBundle *bundle = [NSBundle bundleWithIdentifier:@"com.navigine"];
89 return [UIImage imageNamed:@"navigine_follow_me" inBundle:bundle withConfiguration:nil];
90}
91
92- (void)applyConfig:(NCFollowMeButtonConfig *)config {
93 _config = config;
94 if (self.arrow) {
95 [self updateAppearanceForFollowingState:self.isFollowing];
96 }
97}
98
99- (void)updateAppearanceForFollowingState:(BOOL)isFollowing {
101 UIImage *inactiveImg = c.followMeIcon ?: [self defaultFollowMeImage];
102 UIImage *activeImg = c.followMeIconActive ?: c.followMeIcon ?: [self defaultFollowMeImage];
103 self.arrow.image = [(isFollowing ? activeImg : inactiveImg) imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
104
105 UIColor *accentColor = c.accentColor ?: kNCBaseBlueColor();
106 UIColor *textColor = c.textColor ?: kNCBaseBlackColor();
107 UIColor *bgColor = c.buttonBackgroundColor ?: kNCButtonBackgroundColor();
108
109 [UIView animateWithDuration:kNCStandardAnimationDuration
110 delay:0
111 usingSpringWithDamping:kNCSpringAnimationDamping
112 initialSpringVelocity:kNCSpringAnimationInitialVelocity
113 options:UIViewAnimationOptionCurveEaseInOut
114 animations:^{
115 if (isFollowing) {
116 self.arrow.tintColor = accentColor;
117 self.layer.borderWidth = kNCActiveBorderWidth;
118 self.layer.borderColor = kNCBorderColor().CGColor;
119 self.backgroundColor = kNCWhiteBackgroundColor();
120 } else {
121 self.arrow.tintColor = textColor;
122 self.layer.borderWidth = 0;
123 self.backgroundColor = bgColor;
124 }
125 } completion:nil];
126}
127
128@end