forked from jfro/BCAchievementNotification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BCAchievementNotificationCenter.m
403 lines (347 loc) · 12.6 KB
/
BCAchievementNotificationCenter.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//
// BCAchievementHandler.m
//
// Created by Benjamin Borowski on 9/30/10.
// Copyright 2010 Typeoneerror Studios. All rights reserved.
// $Id$
//
#import <GameKit/GameKit.h>
#import "BCAchievementNotificationCenter.h"
#import "BCAchievementNotificationView.h"
#import <QuartzCore/QuartzCore.h>
#define kBCAchievementDefaultSize CGSizeMake(284.0f, 52.0f)
#define kBCAchievementViewPadding 10.0f
#define kBCAchievementAnimeTime 0.4f
#define kBCAchievementDisplayTime 1.75f
static BCAchievementNotificationCenter *defaultHandler = nil;
#pragma mark -
@interface BCAchievementNotificationCenter(private)
- (void)displayNotification:(UIView<BCAchievementViewProtocol> *)notification;
- (void)orientationChanged:(NSNotification *)notification;
- (CGRect)startFrameForFrame:(CGRect)aFrame;
- (CGRect)endFrameForFrame:(CGRect)aFrame;
@end
#pragma mark -
@implementation BCAchievementNotificationCenter(private)
- (void)displayNotification:(UIView<BCAchievementViewProtocol> *)notification
{
if(![_containerView superview])
{
[_topView addSubview:_containerView];
}
//[_topView addSubview:notification];
notification.frame = [self startFrameForFrame:notification.frame];
[_containerView addSubview:notification];
//[notification animateIn];
// TODO: i think handler should handle animations, don't think it's the view's job to
[UIView animateWithDuration:kBCAchievementAnimeTime delay:0.0 options:0
animations:^{
notification.frame = [self endFrameForFrame:notification.frame];
}
completion:^(BOOL finished) {
[UIView animateWithDuration:kBCAchievementAnimeTime delay:kBCAchievementDisplayTime options:0
animations:^{
notification.frame = [self startFrameForFrame:notification.frame];
}
completion:^(BOOL finished) {
[_queue removeObjectAtIndex:0];
if ([_queue count])
{
[self displayNotification:(BCAchievementNotificationView *)[_queue objectAtIndex:0]];
}
else
[_containerView removeFromSuperview];
}];
}];
}
- (CGRect)rectForRect:(CGRect)rect withinRect:(CGRect)bigRect withMode:(UIViewContentMode)mode
{
CGRect result = rect;
switch (mode)
{
case UIViewContentModeCenter:
result.origin.x = CGRectGetMidX(bigRect) - (rect.size.width / 2);
result.origin.y = CGRectGetMidY(bigRect) - (rect.size.height / 2);
break;
case UIViewContentModeBottom:
result.origin.x = CGRectGetMidX(bigRect) - (rect.size.width / 2);
result.origin.y = CGRectGetMaxY(bigRect) - (rect.size.height);
break;
case UIViewContentModeBottomLeft:
result.origin.x = CGRectGetMinX(bigRect);
result.origin.y = CGRectGetMaxY(bigRect) - rect.size.height;
break;
case UIViewContentModeBottomRight:
result.origin.x = CGRectGetMaxX(bigRect) - rect.size.width;
result.origin.y = CGRectGetMaxY(bigRect) - rect.size.height;
break;
case UIViewContentModeLeft:
result.origin.x = CGRectGetMinX(bigRect);
result.origin.y = CGRectGetMidY(bigRect) - (rect.size.height / 2);
break;
case UIViewContentModeTop:
result.origin.x = CGRectGetMidX(bigRect) - (rect.size.width / 2);
result.origin.y = CGRectGetMinY(bigRect);
break;
case UIViewContentModeTopLeft:
result.origin.x = CGRectGetMinX(bigRect);
result.origin.y = CGRectGetMinY(bigRect);
break;
case UIViewContentModeTopRight:
result.origin.x = CGRectGetMaxX(bigRect) - rect.size.width;
result.origin.y = CGRectGetMinY(bigRect);
break;
case UIViewContentModeRight:
result.origin.x = CGRectGetMaxX(bigRect) - rect.size.width;
result.origin.y = CGRectGetMidY(bigRect) - (rect.size.height / 2);
break;
default:
break;
}
return result;
}
// off screen
- (CGRect)startFrameForFrame:(CGRect)aFrame
{
CGRect result = aFrame;
CGRect containerRect = [BCAchievementNotificationCenter containerRect];
result = [self rectForRect:result withinRect:containerRect withMode:self.viewDisplayMode];
result = CGRectIntegral(result);
switch (self.viewDisplayMode) {
case UIViewContentModeTop:
case UIViewContentModeTopLeft:
case UIViewContentModeTopRight:
result.origin.y -= (aFrame.size.height + kBCAchievementViewPadding);
break;
case UIViewContentModeBottom:
case UIViewContentModeBottomLeft:
case UIViewContentModeBottomRight:
result.origin.y += (aFrame.size.height + kBCAchievementViewPadding);
break;
case UIViewContentModeLeft:
result.origin.x -= aFrame.size.width;
break;
case UIViewContentModeRight:
result.origin.x += aFrame.size.width;
break;
default:
break;
}
// adjust for horizontal padding
switch (self.viewDisplayMode) {
case UIViewContentModeTopLeft:
case UIViewContentModeBottomLeft:
case UIViewContentModeLeft:
result.origin.x += kBCAchievementViewPadding;
break;
case UIViewContentModeTopRight:
case UIViewContentModeBottomRight:
case UIViewContentModeRight:
result.origin.x -= kBCAchievementViewPadding;
break;
default:
break;
}
return result;
}
// on screen
- (CGRect)endFrameForFrame:(CGRect)aFrame
{
CGRect result = aFrame;
CGRect containerRect = [BCAchievementNotificationCenter containerRect];
result = [self rectForRect:result withinRect:containerRect withMode:self.viewDisplayMode];
result = CGRectIntegral(result);
switch (self.viewDisplayMode) {
case UIViewContentModeTop:
case UIViewContentModeTopLeft:
case UIViewContentModeTopRight:
result.origin.y += kBCAchievementViewPadding; // padding from top of screen
break;
case UIViewContentModeBottom:
case UIViewContentModeBottomLeft:
case UIViewContentModeBottomRight:
result.origin.y -= kBCAchievementViewPadding;
break;
default:
break;
}
// adjust for horizontal padding
switch (self.viewDisplayMode) {
case UIViewContentModeTopLeft:
case UIViewContentModeBottomLeft:
case UIViewContentModeLeft:
result.origin.x += kBCAchievementViewPadding;
break;
case UIViewContentModeTopRight:
case UIViewContentModeBottomRight:
case UIViewContentModeRight:
result.origin.x -= kBCAchievementViewPadding;
break;
default:
break;
}
return result;
}
- (void)orientationChanged:(NSNotification *)notification
{
//[self showTitle];
UIDeviceOrientation o = [[UIApplication sharedApplication] statusBarOrientation];
CGFloat angle = 0;
switch (o) {
case UIDeviceOrientationLandscapeLeft: angle = 90; break;
case UIDeviceOrientationLandscapeRight: angle = -90; break;
case UIDeviceOrientationPortraitUpsideDown: angle = 180; break;
default: break;
}
CGRect f = [[UIScreen mainScreen] applicationFrame];
// Swap the frame height and width if necessary
if (UIDeviceOrientationIsLandscape(o)) {
CGFloat t;
t = f.size.width;
f.size.width = f.size.height;
f.size.height = t;
}
CGAffineTransform previousTransform = _containerView.layer.affineTransform;
CGAffineTransform newTransform = CGAffineTransformMakeRotation(angle * M_PI / 180.0);
//newTransform = CGAffineTransformConcat(newTransform, CGAffineTransformMakeTranslation(f.size.height, 0));
// Reset the transform so we can set the size
_containerView.layer.affineTransform = CGAffineTransformIdentity;
_containerView.frame = (CGRect){0,0,f.size};
// Revert to the previous transform for correct animation
_containerView.layer.affineTransform = previousTransform;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
// Set the new transform
_containerView.layer.affineTransform = newTransform;
// Fix the view origin
_containerView.frame = (CGRect){f.origin.x,f.origin.y,_containerView.frame.size};
[UIView commitAnimations];
}
- (void)setupDefaultFrame
{
UIDeviceOrientation o = [[UIApplication sharedApplication] statusBarOrientation];
CGFloat angle = 0;
switch (o) {
case UIDeviceOrientationLandscapeLeft: angle = 90; break;
case UIDeviceOrientationLandscapeRight: angle = -90; break;
case UIDeviceOrientationPortraitUpsideDown: angle = 180; break;
default: break;
}
CGRect f = [[UIScreen mainScreen] applicationFrame];
// Swap the frame height and width if necessary
if (UIDeviceOrientationIsLandscape(o)) {
CGFloat t;
t = f.size.width;
f.size.width = f.size.height;
f.size.height = t;
}
CGAffineTransform previousTransform = _containerView.layer.affineTransform;
CGAffineTransform newTransform = CGAffineTransformMakeRotation(angle * M_PI / 180.0);
_containerView.layer.affineTransform = CGAffineTransformIdentity;
_containerView.frame = (CGRect){0,0,f.size};
// Revert to the previous transform for correct animation
_containerView.layer.affineTransform = previousTransform;
// [UIView beginAnimations:nil context:NULL];
// [UIView setAnimationDuration:0.3];
// Set the new transform
_containerView.layer.affineTransform = newTransform;
// Fix the view origin
_containerView.frame = (CGRect){f.origin.x,f.origin.y,_containerView.frame.size};
// [UIView commitAnimations];
}
@end
#pragma mark -
@implementation BCAchievementNotificationCenter
@synthesize image;
@synthesize defaultBackgroundImage;
@synthesize viewDisplayMode;
@synthesize defaultViewSize;
@synthesize viewClass;
#pragma mark -
+ (BCAchievementNotificationCenter *)defaultCenter
{
if (!defaultHandler) defaultHandler = [[self alloc] init];
return defaultHandler;
}
- (id)init
{
if ((self = [super init]))
{
_topView = [[UIApplication sharedApplication] keyWindow];
self.viewDisplayMode = UIViewContentModeTop;
self.defaultViewSize = kBCAchievementDefaultSize;
self.viewClass = [BCAchievementNotificationView class];
_containerView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
_containerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_containerView.opaque = NO;
_containerView.backgroundColor = [UIColor clearColor];
[_containerView setUserInteractionEnabled: NO];
//defaults to YES where touches won't be passed to views below the achievementview (whose frame is = app window frame).
//Setting to NO will pass touches through.
//there's still a little delay of event passing when the achievementview appears :[
[self setupDefaultFrame];
_queue = [[NSMutableArray alloc] initWithCapacity:0];
self.image = [UIImage imageNamed:@"gk-icon.png"];
self.defaultBackgroundImage = [[UIImage imageNamed:@"gk-notification.png"] stretchableImageWithLeftCapWidth:8.0f topCapHeight:0.0f];
if (![UIDevice currentDevice].generatesDeviceOrientationNotifications) {
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
//[self setDidEnableRotationNotifications:YES];
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
//[self orientationChanged:nil]; // set it up initially?
}
return self;
}
- (void)dealloc
{
[_containerView release];
[_queue release];
[image release];
[super dealloc];
}
#pragma mark -
+ (CGRect)containerRect
{
UIDeviceOrientation o = [[UIApplication sharedApplication] statusBarOrientation];
CGRect f = [[UIScreen mainScreen] applicationFrame];
// Swap the frame height and width if necessary
if (UIDeviceOrientationIsLandscape(o)) {
CGFloat t;
t = f.size.width;
f.size.width = f.size.height;
f.size.height = t;
}
return f;
}
#pragma mark -
- (void)queueNotification:(BCAchievementNotificationView *)notification
{
[_queue addObject:notification];
if([_queue count] == 1)
[self displayNotification:notification];
}
- (void)notifyWithAchievementDescription:(GKAchievementDescription *)achievement
{
CGRect frame = CGRectMake(0, 0, self.defaultViewSize.width, self.defaultViewSize.height);
UIView<BCAchievementViewProtocol> *notification = [[[viewClass alloc] initWithFrame:frame achievementDescription:achievement] autorelease];
((UIImageView *)[notification backgroundView]).image = self.defaultBackgroundImage;
// notification.displayMode = self.viewDisplayMode;
//[notification resetFrameToStart];
[self queueNotification:notification];
}
- (void)notifyWithTitle:(NSString *)title message:(NSString *)message image:(UIImage *)anImage
{
CGRect frame = CGRectMake(0, 0, self.defaultViewSize.width, self.defaultViewSize.height);
UIView<BCAchievementViewProtocol> *notification = [[[viewClass alloc] initWithFrame:frame title:title message:message] autorelease];
((UIImageView *)[notification backgroundView]).image = self.defaultBackgroundImage;
if(anImage)
[notification setImage:anImage];
else if(self.image)
[notification setImage:self.image];
else
[notification setImage:nil];
// notification.displayMode = self.viewDisplayMode;
//[notification resetFrameToStart];
[self queueNotification:notification];
}
@end