-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAnimatedMenuScene.m
More file actions
200 lines (187 loc) · 5.54 KB
/
AnimatedMenuScene.m
File metadata and controls
200 lines (187 loc) · 5.54 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
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
//
// AnimatedMenuScene.m
// Party
//
// Created by Adnan Aftab on 8/25/15.
// Copyright (c) 2015 CX. All rights reserved.
//
#import "AnimatedMenuScene.h"
#import "MenuItemNode.h"
static NSString *SelectAnimation = @"SelectAction";
static NSString *DeselectAnimation = @"DeselectAnimation";
@interface AnimatedMenuScene()
@property (nonatomic, strong) SKFieldNode *magneticField;
@property (nonatomic, assign) CGPoint startPoint;
@property (nonatomic, assign) NSTimeInterval touchStartTime;
@property (nonatomic, assign) BOOL moving;
@property (nonatomic, strong) SKNode *selectedNode;
@property (nonatomic, strong) NSMutableArray *selectedNodes;
@end
@implementation AnimatedMenuScene
- (instancetype)initWithSize:(CGSize)size
{
self = [super initWithSize:size];
if (self)
{
}
return self;
}
- (void)didMoveToView:(SKView *)view
{
[super didMoveToView:view];
_selectedNodes = [NSMutableArray new];
[self configure];
}
- (void)configure
{
self.magneticField = [SKFieldNode radialGravityField];
self.scaleMode = SKSceneScaleModeAspectFill;
CGRect frame = self.frame;
frame.size.width = self.magneticField.minimumRadius;
frame.origin.x -= frame.size.width/2;
frame.size.height = frame.size.height;
frame.origin.y = frame.size.height - frame.size.height;
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:frame];
self.magneticField.position = CGPointMake(frame.size.width/2, frame.size.height / 2);
}
- (void)addChild:(SKNode *)node
{
CGFloat x = arc4random_uniform(self.frame.size.width - node.frame.size.width);
CGFloat y = arc4random_uniform(self.frame.size.height - node.frame.size.height);
node.position = CGPointMake(x, y);
if (node.physicsBody == nil)
{
node.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:((SKShapeNode*)node).path];
}
node.physicsBody.dynamic = YES;
node.physicsBody.affectedByGravity = NO;
node.physicsBody.allowsRotation = YES;
node.physicsBody.mass = 0.3;
node.physicsBody.friction = 0;
node.physicsBody.linearDamping = 3;
SKRange *xRange = [SKRange rangeWithLowerLimit:0 upperLimit:self.frame.size.width-node.frame.size.width];
SKRange *yRange = [SKRange rangeWithLowerLimit:0 upperLimit:self.frame.size.height-node.frame.size.height];
SKConstraint *constraint = [SKConstraint positionX:xRange Y:yRange];
node.constraints = @[constraint];
[super addChild:node];
}
- (void)setMenuNodes:(NSArray *)menuNodes
{
if (_menuNodes)
{
[self.children makeObjectsPerformSelector:@selector(removeFromParent)];
}
_selectedNode = nil;
[_selectedNodes removeAllObjects];
_menuNodes = menuNodes;
[self updateScene];
}
- (void)updateScene
{
for (NSString *nodeTitle in _menuNodes)
{
MenuItemNode *menuItem = [MenuItemNode menuNodeWithTitle:nodeTitle];
[self addChild:menuItem];
}
}
- (SKNode *)nodeAtPoint:(CGPoint)p
{
SKNode *node = [super nodeAtPoint:p];
if (![node.parent isKindOfClass:[SKScene class]] &&
![node isKindOfClass:[MenuItemNode class]] &&
node.parent != nil &&
!node.userInteractionEnabled)
{
node = node.parent;
}
return node;
}
- (void)deselectNode:(SKNode*)node
{
if (!node)
{
return;
}
[node removeActionForKey:SelectAnimation];
SKAction *action = [SKAction scaleTo:1 duration:0.2];
[node runAction:action completion:^{
[node removeAllActions];
}];
NSInteger index = [self.children indexOfObject:node];
if(index != NSNotFound)
{
[self.animatedSceneDelegate animatedMenuScene:self didDeSelectNodeAtIndex:index];
}
}
- (void)selectNode:(SKNode*)node
{
if (!_allowMultipleSelection)
{
[self deselectNode:_selectedNode];
if (_selectedNode == node)
{
_selectedNode = nil;
return;
}
_selectedNodes = nil;
}
else
{
if ([_selectedNodes containsObject:node])
{
[self deselectNode:node];
[_selectedNodes removeObject:node];
return;
}
}
SKAction *action = [SKAction scaleTo:1.3 duration:0.2];
[node runAction:action withKey:SelectAnimation];
if (_allowMultipleSelection)
{
[self.selectedNodes addObject:node];
}
else
{
_selectedNode = node;
}
NSInteger index = [self.children indexOfObject:node];
if (index != NSNotFound)
{
[self.animatedSceneDelegate animatedMenuScene:self didSelectNodeAtIndex:index];
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
self.startPoint = [touch locationInNode:self];
self.touchStartTime = touch.timestamp;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
_moving = YES;
UITouch *touch = [touches anyObject];
CGPoint prePoint = [touch previousLocationInNode:self];
CGPoint point = [touch locationInNode:self];
float dx = point.x - prePoint.x;
float dy = point.y - prePoint.y;
for (SKNode *node in self.children)
{
CGVector vector = CGVectorMake(100 * dx, 100 * dy);
[node.physicsBody applyForce:vector];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (!_moving)
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if (node)
{
[self selectNode:node];
}
}
_moving = NO;
}
@end