-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathSWBSmallCloseButton.m
116 lines (89 loc) · 3 KB
/
SWBSmallCloseButton.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
//
// SWBSmallCloseButton.m
// SWBuaWeb
//
// Created by clowwindy on 11-6-10.
// Copyright 2011年 __MyCompanyName__. All rights reserved.
//
#import "SWBSmallCloseButton.h"
@implementation SWBSmallCloseButton
@synthesize radius;
-(void) loadDefaults {
radius = 7;
highlightView = [[SWBSmallCloseButtonHighlightView alloc] init];
highlightView.backgroundColor = [UIColor clearColor];
highlightView.userInteractionEnabled = NO;
highlightView.frame = self.bounds;
highlightView.center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
highlightView.hidden = YES;
[self addSubview:highlightView];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self loadDefaults];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
//
[self loadDefaults];
}
return self;
}
#define kButtonBGColor 0.5
#define kButtonFGColor 1
#define kButtonLineWidth 1.5
#define kButtonShadowColor 0.0
#define kButtonHighlightedScale 1.5
-(void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat height = self.bounds.size.height;
CGFloat width = self.bounds.size.width;
CGFloat radiusToDraw;
if (self.highlighted) {
radiusToDraw = radius * 1.0;
} else {
radiusToDraw = radius;
}
//draw circle
CGContextAddArc(context, width / 2, height / 2, radiusToDraw, 0, 2 * M_PI, 0);
CGFloat bgColorComponents[4] = {
kButtonBGColor, kButtonBGColor, kButtonBGColor, 1.0};
CGContextSetFillColor(context, bgColorComponents);
CGContextFillPath(context);
// draw x
CGFloat centerX = width / 2;
CGFloat centerY = height / 2;
CGFloat delta = radiusToDraw / 2.2;
CGContextMoveToPoint(context, centerX - delta, centerY - delta);
CGContextAddLineToPoint(context, centerX + delta, centerY + delta);
CGContextMoveToPoint(context, centerX - delta, centerY + delta);
CGContextAddLineToPoint(context, centerX + delta, centerY - delta);
CGFloat fgColorComponents[4] = {
kButtonFGColor, kButtonFGColor, kButtonFGColor, 1.0};
CGContextSetStrokeColor(context, fgColorComponents);
CGContextSetLineWidth(context, kButtonLineWidth);
CGContextStrokePath(context);
}
-(void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
[self setNeedsDisplay];
if (highlighted) {
// self.transform = CGAffineTransformMakeScale(kButtonHighlightedScale, kButtonHighlightedScale);
highlightView.hidden = NO;
highlightView.alpha = 1;
} else {
[UIView beginAnimations:@"buttonHighlighted" context:NULL];
[UIView setAnimationDuration:0.6];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
highlightView.alpha = 0;
[UIView commitAnimations];
}
}
@end