-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathPopupView.m
147 lines (124 loc) · 4.8 KB
/
PopupView.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
134
135
136
137
138
139
140
141
142
143
144
145
146
//
// PopupView.m
// MSCDemo
//
// Created by iflytek on 13-6-7.
// Copyright (c) 2013年 iflytek. All rights reserved.
//
#import "PopupView.h"
#import <QuartzCore/QuartzCore.h>
#ifdef __IPHONE_6_0
# define ALIGN_CENTER NSTextAlignmentCenter
#else
# define ALIGN_CENTER UITextAlignmentCenter
#endif
@implementation PopupView
@synthesize ParentView = _parentView;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent: 0.5f];
self.layer.cornerRadius = 5.0f;
_textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 100, 10)];
_textLabel.numberOfLines = 0;
_textLabel.font = [UIFont systemFontOfSize:17];
_textLabel.textColor = [UIColor whiteColor];
_textLabel.textAlignment = ALIGN_CENTER;
_textLabel.backgroundColor = [UIColor clearColor];
_textLabel.textAlignment = ALIGN_CENTER;
[self addSubview:_textLabel];
_queueCount = 0;
}
return self;
}
- (id)initWithFrame:(CGRect)frame withParentView:(UIView*)view
{
if (view == nil) {
return nil;
}
self = [super initWithFrame:frame];
if (self) {
self.ParentView = view;
self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent: 0.5f];
self.layer.cornerRadius = 5.0f;
_textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 100, 10)];
_textLabel.numberOfLines = 0;
_textLabel.font = [UIFont systemFontOfSize:17];
// _textLabel.textColor = [UIColor whiteColor];
_textLabel.textColor = [UIColor greenColor];
_textLabel.textAlignment = ALIGN_CENTER;
// _textLabel.backgroundColor = [UIColor clearColor];
_textLabel.backgroundColor = [UIColor blackColor];
_textLabel.textAlignment = ALIGN_CENTER;
[self addSubview:_textLabel];
_queueCount = 0;
CGPoint centerPoint = CGPointMake(_parentView.center.x, self.center.y);
self.center= centerPoint;
}
return self;
}
- (void) setText:(NSString *) text
{
_textLabel.frame = CGRectMake(0, 10, 100, 10);
_queueCount ++;
self.alpha = 1.0f;
_textLabel.text = text;
[_textLabel sizeToFit];
CGRect frame = CGRectMake(5, 0, _textLabel.frame.size.width, _textLabel.frame.size.height);
_textLabel.frame = frame;
_textLabel.frame = CGRectMake(_textLabel.frame.origin.x, _textLabel.frame.origin.y+10, _textLabel.frame.size.width, _textLabel.frame.size.height);
frame = CGRectMake((_parentView.frame.size.width - frame.size.width)/2, self.frame.origin.y, _textLabel.frame.size.width+10, _textLabel.frame.size.height+20);
self.frame = frame;
CGPoint centerPoint = CGPointMake(_parentView.center.x, self.center.y);
self.center= centerPoint;
[UIView animateWithDuration:3.0
delay:0.0
options:UIViewAnimationOptionCurveEaseIn
animations:^{
self.alpha = 0;
}
completion:^(BOOL finished){
if (_queueCount == 1) {
[self removeFromSuperview];
}
_queueCount--;
}
];
}
- (void)showText:(NSString *) text
{
_textLabel.frame = CGRectMake(0, 10, 100, 10);
_queueCount ++;
self.alpha = 1.0f;
_textLabel.text = text;
[_textLabel sizeToFit];
CGRect frame = CGRectMake(5, 0, _textLabel.frame.size.width, _textLabel.frame.size.height);
_textLabel.frame = frame;
_textLabel.frame = CGRectMake(_textLabel.frame.origin.x, _textLabel.frame.origin.y+10, _textLabel.frame.size.width, _textLabel.frame.size.height);
frame = CGRectMake((_parentView.frame.size.width - frame.size.width)/2, self.frame.origin.y, _textLabel.frame.size.width+10, _textLabel.frame.size.height+20);
self.frame = frame;
CGPoint centerPoint = CGPointMake(_parentView.center.x, self.center.y);
self.center= centerPoint;
/**
addSubView 会自动判断是否已经在subviews数组里面
如果已经在数组里面,就不会再次addSubView
****/
if (_parentView != nil) {
[_parentView addSubview:self];
}
[UIView animateWithDuration:3.0
delay:0.0
options:UIViewAnimationOptionCurveEaseIn
animations:^{
self.alpha = 0;
}
completion:^(BOOL finished){
if (_queueCount == 1) {
[self removeFromSuperview];
}
_queueCount--;
}
];
}
@end