diff options
author | Igor Khmurets <subzero@mapswithme.com> | 2014-05-29 18:34:05 +0400 |
---|---|---|
committer | Alex Zolotarev <alex@maps.me> | 2015-09-23 02:16:14 +0300 |
commit | 2a4938116042ec215277bbbf5f23c74f0342c3a3 (patch) | |
tree | 6b700b1261edf005f448305eab0b27426739c3e5 /iphone/Maps/Classes/ToastView.m | |
parent | 98bd8de72e230b49b03c9ddf80f696d9eb3bbe9b (diff) |
[ios] Added ToastView
Diffstat (limited to 'iphone/Maps/Classes/ToastView.m')
-rw-r--r-- | iphone/Maps/Classes/ToastView.m | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/iphone/Maps/Classes/ToastView.m b/iphone/Maps/Classes/ToastView.m new file mode 100644 index 0000000000..37e1e53301 --- /dev/null +++ b/iphone/Maps/Classes/ToastView.m @@ -0,0 +1,91 @@ + +#import "ToastView.h" +#import "UIKitCategories.h" + +@interface ToastView () + +@property (nonatomic) UILabel * messageLabel; +@property (nonatomic) NSTimer * timer; + +@end + +@implementation ToastView + + +- (id)initWithMessage:(NSString *)message +{ + CGFloat const xOffset = 18; + CGFloat const yOffset = 12; + CGSize textSize = [message sizeWithDrawSize:CGSizeMake(245 - 2 * xOffset, 1000) font:self.messageLabel.font]; + + self = [super initWithFrame:CGRectMake(0, 0, textSize.width + 2 * xOffset, textSize.height + 2 * yOffset)]; + self.backgroundColor = [UIColor colorWithColorCode:@"f2f2f2"]; + self.layer.cornerRadius = 4; + self.layer.masksToBounds = NO; + self.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin; + + [self addSubview:self.messageLabel]; + self.messageLabel.frame = CGRectMake(xOffset, yOffset - 1, textSize.width, textSize.height); + self.messageLabel.text = message; + + UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)]; + [self addGestureRecognizer:tap]; + + return self; +} + +- (UILabel *)messageLabel +{ + if (!_messageLabel) + { + _messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)]; + _messageLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:12.5]; + _messageLabel.textAlignment = NSTextAlignmentCenter; + _messageLabel.textColor = [UIColor blackColor]; + _messageLabel.numberOfLines = 0; + _messageLabel.lineBreakMode = NSLineBreakByWordWrapping; + } + return _messageLabel; +} + +- (void)tap:(UITapGestureRecognizer *)sender +{ + [self hide]; +} + +- (void)timerSel:(id)sender +{ + [self hide]; +} + +- (void)hide +{ + [UIView animateWithDuration:0.3 animations:^{ + self.alpha = 0; + } completion:^(BOOL finished){ + [self removeFromSuperview]; + }]; +} + +- (void)show +{ + UIWindow * mainWindow = [[UIApplication sharedApplication].windows firstObject]; + UIWindow * window = [[mainWindow subviews] firstObject]; + [window addSubview:self]; + + self.midX = window.width / 2; + self.maxY = window.height - 68; + self.alpha = 0; + [UIView animateWithDuration:0.3 animations:^{ + self.alpha = 1; + }]; + + self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerSel:) userInfo:nil repeats:NO]; +} + +- (void)dealloc +{ + [self.timer invalidate]; +} + +@end |