This repository has been archived by the owner on Nov 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
GKAchievementNotification.m
220 lines (187 loc) · 6.45 KB
/
GKAchievementNotification.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//
// GKAchievementNotification.m
//
// Created by Benjamin Borowski on 9/30/10.
// Copyright 2010 Typeoneerror Studios. All rights reserved.
// $Id$
//
#import <GameKit/GameKit.h>
#import "GKAchievementNotification.h"
#pragma mark -
@interface GKAchievementNotification(private)
- (void)animationInDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context;
- (void)animationOutDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context;
- (void)delegateCallback:(SEL)selector withObject:(id)object;
@end
#pragma mark -
@implementation GKAchievementNotification(private)
- (void)animationInDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
[self delegateCallback:@selector(didShowAchievementNotification:) withObject:self];
[self performSelector:@selector(animateOut) withObject:nil afterDelay:kGKAchievementDisplayTime];
}
- (void)animationOutDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
[self delegateCallback:@selector(didHideAchievementNotification:) withObject:self];
[self removeFromSuperview];
}
- (void)delegateCallback:(SEL)selector withObject:(id)object
{
if (self.handlerDelegate)
{
if ([self.handlerDelegate respondsToSelector:selector])
{
[self.handlerDelegate performSelector:selector withObject:object];
}
}
}
@end
#pragma mark -
@implementation GKAchievementNotification
@synthesize achievement=_achievement;
@synthesize background=_background;
@synthesize handlerDelegate=_handlerDelegate;
@synthesize detailLabel=_detailLabel;
@synthesize logo=_logo;
@synthesize message=_message;
@synthesize title=_title;
@synthesize textLabel=_textLabel;
#pragma mark -
- (id)initWithAchievementDescription:(GKAchievementDescription *)achievement
{
CGRect frame = kGKAchievementDefaultSize;
self.achievement = achievement;
if ((self = [self initWithFrame:frame]))
{
}
return self;
}
- (id)initWithTitle:(NSString *)title andMessage:(NSString *)message
{
CGRect frame = kGKAchievementDefaultSize;
self.title = title;
self.message = message;
if ((self = [self initWithFrame:frame]))
{
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame]))
{
// create the GK background
UIImage *backgroundStretch = [[UIImage imageNamed:@"gk-notification.png"] stretchableImageWithLeftCapWidth:8.0f topCapHeight:0.0f];
UIImageView *tBackground = [[UIImageView alloc] initWithFrame:frame];
tBackground.autoresizingMask = UIViewAutoresizingFlexibleWidth;
tBackground.image = backgroundStretch;
self.background = tBackground;
self.opaque = NO;
[tBackground release];
[self addSubview:self.background];
CGRect r1 = kGKAchievementText1;
CGRect r2 = kGKAchievementText2;
// create the text label
UILabel *tTextLabel = [[UILabel alloc] initWithFrame:r1];
tTextLabel.textAlignment = NSTextAlignmentCenter;
tTextLabel.backgroundColor = [UIColor clearColor];
tTextLabel.textColor = [UIColor whiteColor];
tTextLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:15.0f];
tTextLabel.text = NSLocalizedString(@"Achievement Unlocked", @"Achievemnt Unlocked Message");
self.textLabel = tTextLabel;
[tTextLabel release];
// detail label
UILabel *tDetailLabel = [[UILabel alloc] initWithFrame:r2];
tDetailLabel.textAlignment = NSTextAlignmentCenter;
tDetailLabel.adjustsFontSizeToFitWidth = YES;
tDetailLabel.minimumScaleFactor = 10.0f/15.0f;
tDetailLabel.backgroundColor = [UIColor clearColor];
tDetailLabel.textColor = [UIColor whiteColor];
tDetailLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:11.0f];
self.detailLabel = tDetailLabel;
[tDetailLabel release];
if (self.achievement)
{
self.textLabel.text = self.achievement.title;
self.detailLabel.text = self.achievement.achievedDescription;
}
else
{
if (self.title)
{
self.textLabel.text = self.title;
}
if (self.message)
{
self.detailLabel.text = self.message;
}
}
[self addSubview:self.textLabel];
[self addSubview:self.detailLabel];
}
return self;
}
- (void)dealloc
{
NSLog(@"dealloc: GKAchievementNotification");
self.handlerDelegate = nil;
self.logo = nil;
[_achievement release];
[_background release];
[_detailLabel release];
[_logo release];
[_message release];
[_textLabel release];
[_title release];
[super dealloc];
}
#pragma mark -
- (void)animateIn
{
[self delegateCallback:@selector(willShowAchievementNotification:) withObject:self];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:kGKAchievementAnimeTime];
[UIView setAnimationDelegate:self];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDidStopSelector:@selector(animationInDidStop:finished:context:)];
self.frame = kGKAchievementFrameEnd;
[UIView commitAnimations];
}
- (void)animateOut
{
[self delegateCallback:@selector(willHideAchievementNotification:) withObject:self];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:kGKAchievementAnimeTime];
[UIView setAnimationDelegate:self];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDidStopSelector:@selector(animationOutDidStop:finished:context:)];
self.frame = kGKAchievementFrameStart;
[UIView commitAnimations];
}
- (void)setImage:(UIImage *)image
{
if (image)
{
if (!self.logo)
{
UIImageView *tLogo = [[UIImageView alloc] initWithFrame:CGRectMake(7.0f, 6.0f, 34.0f, 34.0f)];
tLogo.contentMode = UIViewContentModeCenter;
self.logo = tLogo;
[tLogo release];
[self addSubview:self.logo];
}
self.logo.image = image;
self.textLabel.frame = kGKAchievementText1WLogo;
self.detailLabel.frame = kGKAchievementText2WLogo;
}
else
{
if (self.logo)
{
[self.logo removeFromSuperview];
}
self.textLabel.frame = kGKAchievementText1;
self.detailLabel.frame = kGKAchievementText2;
}
}
@end