Skip to content

Commit 062b942

Browse files
committed
Implement Button class
1 parent ea0febe commit 062b942

File tree

14 files changed

+169
-101
lines changed

14 files changed

+169
-101
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SpritekitUIAccessExample.xcodeproj/xcuserdata/hiiwave.xcuserdatad/xcdebugger/

KIF

Submodule KIF added at 9a56547

SpritekitUIAccessExample.xcodeproj/project.pbxproj

+24-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
063395FC1FE90CB3001918CC /* SUESecondScene.m in Sources */ = {isa = PBXBuildFile; fileRef = 063395FB1FE90CB3001918CC /* SUESecondScene.m */; };
1313
0633960E1FEA03A9001918CC /* SpritekitUIAccessExampleUITests.m in Sources */ = {isa = PBXBuildFile; fileRef = 0633960C1FEA03A8001918CC /* SpritekitUIAccessExampleUITests.m */; };
1414
0633960F1FEA03A9001918CC /* Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 0633960D1FEA03A8001918CC /* Info.plist */; };
15+
063396191FEA2C0D001918CC /* Actions.sks in Resources */ = {isa = PBXBuildFile; fileRef = 063396181FEA2C0D001918CC /* Actions.sks */; };
16+
0633961C1FEA2C97001918CC /* MyButtonNode.m in Sources */ = {isa = PBXBuildFile; fileRef = 0633961B1FEA2C97001918CC /* MyButtonNode.m */; };
17+
063396201FEA3A8F001918CC /* MyButtonLabel.m in Sources */ = {isa = PBXBuildFile; fileRef = 0633961F1FEA3A8F001918CC /* MyButtonLabel.m */; };
1518
06666EA81FE8BB4D00D265CF /* SUEScene.m in Sources */ = {isa = PBXBuildFile; fileRef = 06666EA71FE8BB4D00D265CF /* SUEScene.m */; };
1619
06A59E1C1FE8A35100DB40CF /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 06A59E1B1FE8A35100DB40CF /* AppDelegate.m */; };
1720
06A59E1E1FE8A35100DB40CF /* SUEFirstScene.sks in Resources */ = {isa = PBXBuildFile; fileRef = 06A59E1D1FE8A35100DB40CF /* SUEFirstScene.sks */; };
18-
06A59E201FE8A35100DB40CF /* Actions.sks in Resources */ = {isa = PBXBuildFile; fileRef = 06A59E1F1FE8A35100DB40CF /* Actions.sks */; };
1921
06A59E231FE8A35100DB40CF /* SUEFirstScene.m in Sources */ = {isa = PBXBuildFile; fileRef = 06A59E221FE8A35100DB40CF /* SUEFirstScene.m */; };
2022
06A59E261FE8A35100DB40CF /* GameViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 06A59E251FE8A35100DB40CF /* GameViewController.m */; };
2123
06A59E291FE8A35100DB40CF /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 06A59E271FE8A35100DB40CF /* Main.storyboard */; };
@@ -94,13 +96,17 @@
9496
063396011FEA0058001918CC /* SpritekitUIAccessExampleUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SpritekitUIAccessExampleUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
9597
0633960C1FEA03A8001918CC /* SpritekitUIAccessExampleUITests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SpritekitUIAccessExampleUITests.m; sourceTree = "<group>"; };
9698
0633960D1FEA03A8001918CC /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
99+
063396181FEA2C0D001918CC /* Actions.sks */ = {isa = PBXFileReference; lastKnownFileType = file.sks; name = Actions.sks; path = SpritekitUIAccessExample/Actions.sks; sourceTree = SOURCE_ROOT; };
100+
0633961A1FEA2C97001918CC /* MyButtonNode.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MyButtonNode.h; sourceTree = "<group>"; };
101+
0633961B1FEA2C97001918CC /* MyButtonNode.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MyButtonNode.m; sourceTree = "<group>"; };
102+
0633961E1FEA3A8F001918CC /* MyButtonLabel.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MyButtonLabel.h; sourceTree = "<group>"; };
103+
0633961F1FEA3A8F001918CC /* MyButtonLabel.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MyButtonLabel.m; sourceTree = "<group>"; };
97104
06666EA61FE8BB4D00D265CF /* SUEScene.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SUEScene.h; sourceTree = "<group>"; };
98105
06666EA71FE8BB4D00D265CF /* SUEScene.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SUEScene.m; sourceTree = "<group>"; };
99106
06A59E171FE8A35100DB40CF /* SpritekitUIAccessExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SpritekitUIAccessExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
100107
06A59E1A1FE8A35100DB40CF /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
101108
06A59E1B1FE8A35100DB40CF /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
102109
06A59E1D1FE8A35100DB40CF /* SUEFirstScene.sks */ = {isa = PBXFileReference; lastKnownFileType = file.sks; path = SUEFirstScene.sks; sourceTree = "<group>"; };
103-
06A59E1F1FE8A35100DB40CF /* Actions.sks */ = {isa = PBXFileReference; lastKnownFileType = file.sks; path = Actions.sks; sourceTree = "<group>"; };
104110
06A59E211FE8A35100DB40CF /* SUEFirstScene.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SUEFirstScene.h; sourceTree = "<group>"; };
105111
06A59E221FE8A35100DB40CF /* SUEFirstScene.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SUEFirstScene.m; sourceTree = "<group>"; };
106112
06A59E241FE8A35100DB40CF /* GameViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GameViewController.h; sourceTree = "<group>"; };
@@ -154,6 +160,17 @@
154160
path = SpritekitUIAccessExampleUITests;
155161
sourceTree = "<group>";
156162
};
163+
0633961D1FEA3A73001918CC /* Buttons */ = {
164+
isa = PBXGroup;
165+
children = (
166+
0633961A1FEA2C97001918CC /* MyButtonNode.h */,
167+
0633961B1FEA2C97001918CC /* MyButtonNode.m */,
168+
0633961E1FEA3A8F001918CC /* MyButtonLabel.h */,
169+
0633961F1FEA3A8F001918CC /* MyButtonLabel.m */,
170+
);
171+
path = Buttons;
172+
sourceTree = "<group>";
173+
};
157174
06666EB01FE8BB5500D265CF /* Scenes */ = {
158175
isa = PBXGroup;
159176
children = (
@@ -194,15 +211,16 @@
194211
isa = PBXGroup;
195212
children = (
196213
06666EB01FE8BB5500D265CF /* Scenes */,
214+
0633961D1FEA3A73001918CC /* Buttons */,
197215
06A59E1A1FE8A35100DB40CF /* AppDelegate.h */,
198216
06A59E1B1FE8A35100DB40CF /* AppDelegate.m */,
199-
06A59E1F1FE8A35100DB40CF /* Actions.sks */,
200217
06A59E241FE8A35100DB40CF /* GameViewController.h */,
201218
06A59E251FE8A35100DB40CF /* GameViewController.m */,
202219
06A59E271FE8A35100DB40CF /* Main.storyboard */,
203220
06A59E2A1FE8A35100DB40CF /* Assets.xcassets */,
204221
06A59E2C1FE8A35100DB40CF /* LaunchScreen.storyboard */,
205222
06A59E2F1FE8A35100DB40CF /* Info.plist */,
223+
063396181FEA2C0D001918CC /* Actions.sks */,
206224
06A59E301FE8A35100DB40CF /* main.m */,
207225
);
208226
path = SpritekitUIAccessExample;
@@ -406,11 +424,11 @@
406424
buildActionMask = 2147483647;
407425
files = (
408426
06A59E291FE8A35100DB40CF /* Main.storyboard in Resources */,
427+
063396191FEA2C0D001918CC /* Actions.sks in Resources */,
409428
06A59E1E1FE8A35100DB40CF /* SUEFirstScene.sks in Resources */,
410429
063395F21FE909DF001918CC /* SUESecondScene.sks in Resources */,
411430
06A59E2B1FE8A35100DB40CF /* Assets.xcassets in Resources */,
412431
06A59E2E1FE8A35100DB40CF /* LaunchScreen.storyboard in Resources */,
413-
06A59E201FE8A35100DB40CF /* Actions.sks in Resources */,
414432
);
415433
runOnlyForDeploymentPostprocessing = 0;
416434
};
@@ -437,8 +455,10 @@
437455
buildActionMask = 2147483647;
438456
files = (
439457
063395FC1FE90CB3001918CC /* SUESecondScene.m in Sources */,
458+
0633961C1FEA2C97001918CC /* MyButtonNode.m in Sources */,
440459
06A59E231FE8A35100DB40CF /* SUEFirstScene.m in Sources */,
441460
06A59E261FE8A35100DB40CF /* GameViewController.m in Sources */,
461+
063396201FEA3A8F001918CC /* MyButtonLabel.m in Sources */,
442462
06666EA81FE8BB4D00D265CF /* SUEScene.m in Sources */,
443463
06A59E311FE8A35100DB40CF /* main.m in Sources */,
444464
06A59E1C1FE8A35100DB40CF /* AppDelegate.m in Sources */,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// MyButtonLabel.h
3+
// SpritekitUIAccessExample
4+
//
5+
// Created by hiiwave on 20/12/2017.
6+
// Copyright © 2017 pong. All rights reserved.
7+
//
8+
9+
#import <SpriteKit/SpriteKit.h>
10+
11+
@interface MyButtonLabel : SKLabelNode
12+
13+
@end
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// MyButtonLabel.m
3+
// SpritekitUIAccessExample
4+
//
5+
// Created by hiiwave on 20/12/2017.
6+
// Copyright © 2017 pong. All rights reserved.
7+
//
8+
9+
#import "MyButtonLabel.h"
10+
#import "MyButtonNode.h"
11+
12+
@implementation MyButtonLabel
13+
14+
+ (instancetype)labelNodeWithText:(NSString *)text {
15+
MyButtonLabel *nodeLabel = [super labelNodeWithText:text];
16+
nodeLabel.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
17+
nodeLabel.userInteractionEnabled = YES;
18+
return nodeLabel;
19+
}
20+
21+
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
22+
NSAssert(self.parent.class == [MyButtonNode class], @"");
23+
[self.parent touchesBegan:touches withEvent:event];
24+
}
25+
26+
@end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// MyButtonNode.h
3+
// SpritekitUIAccessExample
4+
//
5+
// Created by hiiwave on 20/12/2017.
6+
// Copyright © 2017 pong. All rights reserved.
7+
//
8+
9+
#import <SpriteKit/SpriteKit.h>
10+
11+
@protocol MyButtonNodeDelegate;
12+
13+
14+
@interface MyButtonNode : SKSpriteNode
15+
16+
@property(nonatomic, nullable) id<MyButtonNodeDelegate> delegate;
17+
@property(nonatomic, nullable) NSString *labelText;
18+
19+
@end
20+
21+
22+
@protocol MyButtonNodeDelegate<NSObject>
23+
24+
- (void)button:(MyButtonNode *_Nonnull)button touchBegan:(UITouch *_Nonnull)touch;
25+
26+
@end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// MyButtonNode.m
3+
// SpritekitUIAccessExample
4+
//
5+
// Created by hiiwave on 20/12/2017.
6+
// Copyright © 2017 pong. All rights reserved.
7+
//
8+
9+
#import "MyButtonNode.h"
10+
#import "MyButtonLabel.h"
11+
12+
13+
@implementation MyButtonNode
14+
15+
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
16+
if (self = [super initWithCoder:aDecoder]) {
17+
self.userInteractionEnabled = YES;
18+
}
19+
return self;
20+
}
21+
22+
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
23+
UITouch *touch = [touches anyObject];
24+
[_delegate button:self touchBegan:touch];
25+
[self.scene touchesBegan:touches withEvent:event];
26+
}
27+
28+
#pragma mark getter/setter
29+
30+
- (void)setLabelText:(NSString *)labelText {
31+
MyButtonLabel *nodeLabel = [MyButtonLabel labelNodeWithText:labelText];
32+
nodeLabel.fontSize = 32;
33+
nodeLabel.userInteractionEnabled = YES;
34+
[self addChild:nodeLabel];
35+
}
36+
37+
@end

SpritekitUIAccessExample/Scenes/SUEFirstScene.m

+19-76
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,14 @@ @implementation SUEFirstScene {
1818

1919
- (void)sceneDidLoad {
2020
[super sceneDidLoad];
21-
// Setup your scene here
22-
23-
// Initialize update time
24-
_lastUpdateTime = 0;
25-
26-
// Get label node from scene and store it for use later
2721
_label = (SKLabelNode *)[self childNodeWithName:@"//helloLabel"];
28-
2922
_label.alpha = 0.0;
3023
[_label runAction:[SKAction fadeInWithDuration:2.0]];
3124

32-
CGFloat w = (self.size.width + self.size.height) * 0.05;
3325

34-
// Create shape node to use during mouse interaction
35-
_spinnyNode = [SKShapeNode shapeNodeWithRectOfSize:CGSizeMake(w, w) cornerRadius:w * 0.3];
36-
_spinnyNode.lineWidth = 2.5;
37-
38-
[_spinnyNode runAction:[SKAction repeatActionForever:[SKAction rotateByAngle:M_PI duration:1]]];
39-
[_spinnyNode runAction:[SKAction sequence:@[
40-
[SKAction waitForDuration:0.5],
41-
[SKAction fadeOutWithDuration:0.5],
42-
[SKAction removeFromParent],
43-
]]];
26+
MyButtonNode *nextBtn = (MyButtonNode *)[self childNodeWithName:@"//nextBtn"];
27+
nextBtn.delegate = self;
28+
nextBtn.labelText = @"NEXT";
4429

4530
[self addTestableNodes];
4631
}
@@ -52,67 +37,16 @@ - (void)addTestableNodes {
5237
[self.testableNodes addObject:[self childNodeWithName:@"//node2"]];
5338
}
5439

55-
56-
- (void)touchDownAtPoint:(CGPoint)pos {
57-
SKShapeNode *n = [_spinnyNode copy];
58-
n.position = pos;
59-
n.strokeColor = [SKColor greenColor];
60-
[self addChild:n];
61-
}
62-
63-
- (void)touchMovedToPoint:(CGPoint)pos {
64-
SKShapeNode *n = [_spinnyNode copy];
65-
n.position = pos;
66-
n.strokeColor = [SKColor blueColor];
67-
[self addChild:n];
68-
}
69-
70-
- (void)touchUpAtPoint:(CGPoint)pos {
71-
SKShapeNode *n = [_spinnyNode copy];
72-
n.position = pos;
73-
n.strokeColor = [SKColor redColor];
74-
[self addChild:n];
75-
}
76-
7740
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
41+
// UITouch *touch = [touches anyObject];
42+
// SKNode *touchedNode = [self nodeAtPoint:[touch locationInNode:self]];
43+
// if ([touchedNode.name isEqual:@"nextBtn"]) {
44+
// NSLog(@"touchedNode.class is %@", touchedNode.class);
45+
// [self transitToSecondScene];
46+
// }
47+
7848
// Run 'Pulse' action from 'Actions.sks'
79-
UITouch *touch = [touches anyObject];
80-
SKNode *touchedNode = [self nodeAtPoint:[touch locationInNode:self]];
81-
if ([touchedNode.name isEqual:@"nextBtn"]) {
82-
[self transitToSecondScene];
83-
}
84-
8549
[_label runAction:[SKAction actionNamed:@"Pulse"] withKey:@"fadeInOut"];
86-
for (UITouch *t in touches) {[self touchDownAtPoint:[t locationInNode:self]];}
87-
}
88-
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
89-
for (UITouch *t in touches) {[self touchMovedToPoint:[t locationInNode:self]];}
90-
}
91-
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
92-
for (UITouch *t in touches) {[self touchUpAtPoint:[t locationInNode:self]];}
93-
}
94-
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
95-
for (UITouch *t in touches) {[self touchUpAtPoint:[t locationInNode:self]];}
96-
}
97-
98-
99-
- (void)update:(CFTimeInterval)currentTime {
100-
// Called before each frame is rendered
101-
102-
// Initialize _lastUpdateTime if it has not already been
103-
if (_lastUpdateTime == 0) {
104-
_lastUpdateTime = currentTime;
105-
}
106-
107-
// Calculate time since last update
108-
CGFloat dt = currentTime - _lastUpdateTime;
109-
110-
// Update entities
111-
for (GKEntity *entity in self.entities) {
112-
[entity updateWithDeltaTime:dt];
113-
}
114-
115-
_lastUpdateTime = currentTime;
11650
}
11751

11852
- (void)transitToSecondScene {
@@ -121,6 +55,15 @@ - (void)transitToSecondScene {
12155
[self.view presentScene:nextScene];
12256
}
12357

58+
#pragma mark buttons
59+
60+
- (void)button:(MyButtonNode *)button touchBegan:(UITouch *)touch {
61+
if ([button.name isEqual:@"nextBtn"]) {
62+
[self transitToSecondScene];
63+
}
64+
}
65+
66+
12467
@end
12568

12669

-309 Bytes
Binary file not shown.

SpritekitUIAccessExample/Scenes/SUEScene.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
#import <SpriteKit/SpriteKit.h>
1010
#import <GamePlayKit/GamePlayKit.h>
1111

12-
@interface SUEScene : SKScene
12+
#import "MyButtonNode.h"
13+
14+
@interface SUEScene : SKScene<MyButtonNodeDelegate>
1315

1416
@property(nonatomic) NSMutableArray *testableNodes;
1517
@property(nonatomic) NSMutableArray *accessibleElements;

SpritekitUIAccessExample/Scenes/SUEScene.m

+5
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,9 @@ - (void)initAccessibility {
5757
self.accessibilityElements = _accessibleElements;
5858
}
5959

60+
#pragma mark buttons
61+
62+
- (void)button:(MyButtonNode *)button touchBegan:(UITouch *)touch {
63+
}
64+
6065
@end

SpritekitUIAccessExample/Scenes/SUESecondScene.m

+13-2
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@ @implementation SUESecondScene
1313
- (void)sceneDidLoad {
1414
[super sceneDidLoad];
1515
[self.testableNodes addObject:[self childNodeWithName:@"//lastBtn"]];
16+
17+
MyButtonNode *lastBtn = (MyButtonNode *)[self childNodeWithName:@"//lastBtn"];
18+
lastBtn.delegate = self;
19+
lastBtn.labelText = @"LAST";
1620
}
1721

1822
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
19-
// Run 'Pulse' action from 'Actions.sks'
2023
UITouch *touch = [touches anyObject];
2124
SKNode *touchedNode = [self nodeAtPoint:[touch locationInNode:self]];
2225
if ([touchedNode.name isEqual:@"lastBtn"]) {
23-
[self transitToFirstScene];
26+
// [self transitToFirstScene];
2427
}
2528
}
2629

@@ -30,4 +33,12 @@ - (void)transitToFirstScene {
3033
[self.view presentScene:nextScene];
3134
}
3235

36+
#pragma mark buttons
37+
38+
- (void)button:(MyButtonNode *)button touchBegan:(UITouch *)touch {
39+
if ([button.name isEqual:@"lastBtn"]) {
40+
[self transitToFirstScene];
41+
}
42+
}
43+
3344
@end
-801 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)