forked from typeoneerror/GKAchievementNotification
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGKAchievementHandler.m
More file actions
executable file
·137 lines (108 loc) · 3.36 KB
/
GKAchievementHandler.m
File metadata and controls
executable file
·137 lines (108 loc) · 3.36 KB
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
//
// GKAchievementHandler.m
//
// Created by Benjamin Borowski on 9/30/10.
// Copyright 2010 Typeoneerror Studios. All rights reserved.
// $Id$
//
#import <GameKit/GameKit.h>
#import "GKAchievementHandler.h"
#import "GKAchievementNotification.h"
static GKAchievementHandler *defaultHandler = nil;
#pragma mark -
@interface GKAchievementHandler(private)
- (void)displayNotification:(GKAchievementNotification *)notification;
@end
#pragma mark -
@implementation GKAchievementHandler(private)
- (void)displayNotification:(GKAchievementNotification *)notification
{
if (self.image != nil)
{
[notification setImage:self.image];
}
else
{
[notification setImage:nil];
}
UIView *_topView = [[[[UIApplication sharedApplication] keyWindow] rootViewController ] view];
[_topView addSubview:notification];
// adjust size and position of notification view
CGFloat width = 284.0f;
CGFloat middle_x = 0;
UIViewController *controller = [[[UIApplication sharedApplication] keyWindow] rootViewController];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ){
width = 445.0f;
}
else {
if (controller.interfaceOrientation == UIInterfaceOrientationLandscapeLeft || controller.interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
width = 443.0f;
}
}
if ( controller.interfaceOrientation == UIInterfaceOrientationPortrait || controller.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
middle_x = _topView.frame.size.width/2;
}
else {
middle_x = _topView.frame.size.height/2;
}
notification.frame = CGRectMake(0, -53.0, width, 52.0f);
notification.center = CGPointMake(middle_x, notification.center.y);
[notification animate];
}
@end
#pragma mark -
@implementation GKAchievementHandler
@synthesize image=_image;
#pragma mark -
+ (GKAchievementHandler *)defaultHandler
{
if (!defaultHandler) defaultHandler = [[self alloc] init];
return defaultHandler;
}
- (id)init
{
self = [super init];
if (self != nil)
{
_queue = [[NSMutableArray alloc] initWithCapacity:0];
self.image = [UIImage imageNamed:@"gk-icon.png"];
}
return self;
}
- (void)dealloc
{
[_queue release];
[_image release];
[super dealloc];
}
#pragma mark -
- (void)notifyAchievementTitle:(NSString *)title andMessage:(NSString *)message
{
GKAchievementNotification *notification = [[[GKAchievementNotification alloc] initWithTitle:title andMessage:message] autorelease];
notification.handlerDelegate = self;
[_queue addObject:notification];
if ([_queue count] && isShown == NO)
{
isShown = YES;
[self performSelectorOnMainThread:@selector(displayNotification:) withObject:notification waitUntilDone:YES];
}
}
- (void)notifyAchievement:(GKAchievementDescription *)achievement {
[self notifyAchievementTitle:achievement.title andMessage:achievement.achievedDescription];
}
#pragma mark -
#pragma mark GKAchievementHandlerDelegate implementation
- (void)didHideAchievementNotification:(GKAchievementNotification *)notification
{
if ([_queue count]) {
[_queue removeObjectAtIndex:0];
}
if ([_queue count])
{
[self displayNotification:(GKAchievementNotification *)[_queue objectAtIndex:0]];
}
else {
isShown = NO;
}
}
@end