This repository has been archived by the owner on Mar 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
SGInfoAlert.m
executable file
·108 lines (96 loc) · 3.65 KB
/
SGInfoAlert.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
//
// SGInfoAlert.m
//
// Created by Azure_Sagi on 3/1/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "SGInfoAlert.h"
@implementation SGInfoAlert
// 画出圆角矩形背景
static void addRoundedRectToPath(CGContextRef context, CGRect rect,
float ovalWidth,float ovalHeight)
{
float fw, fh;
if (ovalWidth == 0 || ovalHeight == 0) {
CGContextAddRect(context, rect);
return;
}
CGContextSaveGState(context);
CGContextTranslateCTM (context, CGRectGetMinX(rect),
CGRectGetMinY(rect));
CGContextScaleCTM (context, ovalWidth, ovalHeight);
fw = CGRectGetWidth (rect) / ovalWidth;
fh = CGRectGetHeight (rect) / ovalHeight;
CGContextMoveToPoint(context, fw, fh/2);
CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1);
CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1);
CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1);
CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1);
CGContextClosePath(context);
CGContextRestoreGState(context);
}
- (id)initWithFrame:(CGRect)frame bgColor:(CGColorRef)bgcolor fgColor:(CGColorRef)fgcolor info:(NSString*)info{
CGRect viewR = CGRectMake(0, 0, frame.size.width*1.2, frame.size.height*1.2);
self = [super initWithFrame:viewR];
if (self) {
self.backgroundColor = [UIColor clearColor];
bgcolor_ = bgcolor;
fgcolor_ = fgcolor;
info_ = [[NSString alloc] initWithString:info];
fontSize_ = frame.size;
}
return self;
}
- (void)drawRect:(CGRect)rect{
CGContextRef context = UIGraphicsGetCurrentContext();
// 背景0.8透明度
CGContextSetAlpha(context, .8);
addRoundedRectToPath(context, rect, 4.0f, 4.0f);
CGContextSetFillColorWithColor(context, bgcolor_);
CGContextFillPath(context);
// 文字1.0透明度
CGContextSetAlpha(context, 1.0);
//CGContextSetShadowWithColor(context, CGSizeMake(0, -1), 1, [[UIColor whiteColor] CGColor]);
CGContextSetFillColorWithColor(context, fgcolor_);
float x = (rect.size.width - fontSize_.width) / 2.0;
float y = (rect.size.height - fontSize_.height) / 2.0;
CGRect r = CGRectMake(x, y, fontSize_.width, fontSize_.height);
[info_ drawInRect:r withFont:[UIFont systemFontOfSize:kSGInfoAlert_fontSize] lineBreakMode:UILineBreakModeTailTruncation];
}
- (void)dealloc{
[info_ release];
[super dealloc];
}
// 从上层视图移除并释放
- (void)remove{
[self removeFromSuperview];
}
// 渐变消失
- (void)fadeAway{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5f];
self.alpha = .0;
[UIView commitAnimations];
[self performSelector:@selector(remove) withObject:nil afterDelay:1.5f];
}
+ (void)showInfo:(NSString *)info
bgColor:(CGColorRef)bgcolor
fgColor:(CGColorRef)fgcolor
inView:(UIView *)view
vertical:(float)height{
height = height < 0 ? 0 : height > 1 ? 1 : height;
CGSize size = [info sizeWithFont:[UIFont systemFontOfSize:kSGInfoAlert_fontSize]
constrainedToSize:kMax_ConstrainedSize];
CGRect frame = CGRectMake(0, 0, size.width, size.height);
SGInfoAlert *alert = [[SGInfoAlert alloc] initWithFrame:frame bgColor:bgcolor fgColor:fgcolor info:info];
alert.center = CGPointMake(view.center.x, view.frame.size.height*height);
alert.alpha = 0;
[view addSubview:alert];
[alert release];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.3f];
alert.alpha = 1.0;
[UIView commitAnimations];
[alert performSelector:@selector(fadeAway) withObject:nil afterDelay:1.5];
}
@end