forked from cruffenach/CRToast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainViewController.m
133 lines (103 loc) · 5.68 KB
/
MainViewController.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//
// MainViewController.m
// CRNotificationDemo
//
// Created by Cezary Wojcik on 11/15/13.
// Copyright (c) 2013 Cezary Wojcik. All rights reserved.
//
#import "MainViewController.h"
#import "CRToast.h"
@interface MainViewController ()
@property (weak, readonly) NSDictionary *options;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet UISegmentedControl *segFromStyle;
@property (weak, nonatomic) IBOutlet UISegmentedControl *segToStyle;
@property (weak, nonatomic) IBOutlet UISlider *sliderDuration;
@property (weak, nonatomic) IBOutlet UILabel *lblDuration;
@property (weak, nonatomic) IBOutlet UISwitch *showImageSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *coverNavBarSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *springPhysicsSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *slideOverSwitch;
@property (weak, nonatomic) IBOutlet UISegmentedControl *segAlignment;
@property (weak, nonatomic) IBOutlet UITextField *txtNotificationMessage;
@property (weak, nonatomic) IBOutlet UIButton *showNotificationButton;
@property (assign, nonatomic) NSTextAlignment textAlignment;
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.automaticallyAdjustsScrollViewInsets = NO;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
self.scrollView.contentSize = CGSizeMake(CGRectGetWidth(self.contentView.frame),
CGRectGetMaxY(self.showNotificationButton.frame));
self.title = @"CRToast";
[self updateDurationLabel];
UIFont *font = [UIFont boldSystemFontOfSize:10];
[self.segFromStyle setTitleTextAttributes:@{NSFontAttributeName : font}
forState:UIControlStateNormal];
[self.segToStyle setTitleTextAttributes:@{NSFontAttributeName : font}
forState:UIControlStateNormal];
}
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
self.scrollView.contentInset = UIEdgeInsetsMake([self.topLayoutGuide length],
0,
[self.bottomLayoutGuide length],
0);
self.scrollView.contentSize = CGSizeMake(CGRectGetWidth(self.contentView.frame),
CGRectGetMaxY(self.showNotificationButton.frame));
}
- (void)updateDurationLabel {
self.lblDuration.text = [NSString stringWithFormat:@"%f seconds", self.sliderDuration.value];
}
- (IBAction)sliderDurationChanged:(UISlider *)sender {
[self updateDurationLabel];
}
# pragma mark - Show Notification
- (IBAction)btnShowNotificationPressed:(UIButton *)sender {
[CRToastManager showNotificationWithOptions:[self options]
completionBlock:^{
NSLog(@"Completed");
}];
}
#pragma mark - Notifications
- (void)keyboardWillShow:(NSNotification*)notification {
self.scrollView.contentInset = UIEdgeInsetsMake([self.topLayoutGuide length],
0,
CGRectGetHeight([notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]),
0);
self.scrollView.scrollIndicatorInsets = self.scrollView.contentInset;
self.scrollView.contentSize = CGSizeMake(CGRectGetWidth(self.contentView.frame),
CGRectGetMaxY(self.showNotificationButton.frame));
}
#pragma mark - Overrides
- (NSDictionary*)options {
NSMutableDictionary *options = [@{kCRToastNotificationTypeKey : self.coverNavBarSwitch.on ? @(CRToastTypeNavigationBar) : @(CRToastTypeStatusBar),
kCRToastNotificationPresentationTypeKey : self.slideOverSwitch.on ? @(CRToastPresentationTypeCover) : @(CRToastPresentationTypePush),
kCRToastTextKey : self.txtNotificationMessage.text,
kCRToastTimeIntervalKey : @(self.sliderDuration.value),
kCRToastTextAlignmentKey : @(self.textAlignment),
kCRToastTimeIntervalKey : @(self.sliderDuration.value),
kCRToastAnimationInStyleKey : @(self.segFromStyle.selectedSegmentIndex),
kCRToastAnimationOutStyleKey : @(self.segToStyle.selectedSegmentIndex)} mutableCopy];
if (self.showImageSwitch.on) {
options[kCRToastImageKey] = [UIImage imageNamed:@"alert_icon.png"];
}
if (self.springPhysicsSwitch.on) {
options[kCRToastAnimationTypeKey] = @(CRToastAnimationTypeSpring);
options[kCRToastAnimationInTimeIntervalKey] = @(0.5);
options[kCRToastAnimationOutTimeIntervalKey] = @(0.5);
}
return [NSDictionary dictionaryWithDictionary:options];
}
- (NSTextAlignment)textAlignment {
NSInteger selectedSegment = self.segAlignment.selectedSegmentIndex;
return selectedSegment == 0 ? NSTextAlignmentLeft :
selectedSegment == 1 ? NSTextAlignmentCenter :
NSTextAlignmentRight;
}
@end