Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ToastView.m « Classes « Maps « iphone - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 70745307b2dc7c4c53d8325e2b7d0402ca2adf45 (plain)
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

#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