/
MasterViewController.m
139 lines (104 loc) · 3.37 KB
/
MasterViewController.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
//
// MasterViewController.m
// SpriteAnimationDemo
//
// Created by Rafal Sroka on 11-11-17.
// Copyright (c) 2011 Rafal Sroka. All rights reserved.
//
#import "MasterViewController.h"
#import "UIImage+Sprite.h"
@implementation MasterViewController
- (void)dealloc
{
[settingsView release];
[locationLabel release];
[lengthLabel release];
[customRangeSwitch release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
[self setTitle:NSLocalizedString(@"Sprite Animation", @"")];
[self switchValueChanged:customRangeSwitch];
}
- (void)viewDidUnload
{
[imageView release];
imageView = nil;
[settingsView release];
settingsView = nil;
[locationLabel release];
locationLabel = nil;
[lengthLabel release];
lengthLabel = nil;
[customRangeSwitch release];
customRangeSwitch = nil;
[super viewDidUnload];
}
- (IBAction)buttonAction:(id)sender
{
if ([imageView isAnimating])
{
[imageView stopAnimating];
[((UIButton *)sender) setTitle:NSLocalizedString(@"Start animation!", @"")
forState:UIControlStateNormal];
return;
}
//
// This cool sprite sheet can be found at http://gushh.net/blog/free-game-sprites-explosion-3/
// I added numbers to this image to make testing and debuging easier.
//
UIImage *spriteSheet = [UIImage imageNamed:@"explosion_4_39_128_debug"];
if ([customRangeSwitch isOn])
{
NSRange range = NSMakeRange([locationLabel.text intValue],
[lengthLabel.text intValue]);
NSArray *arrayWithSprites = [spriteSheet spritesWithSpriteSheetImage:spriteSheet
inRange:range
spriteSize:CGSizeMake(128, 128)];
[imageView setAnimationImages:arrayWithSprites];
}
else
{
NSArray *arrayWithSprites = [spriteSheet spritesWithSpriteSheetImage:spriteSheet
spriteSize:CGSizeMake(128, 128)];
[imageView setAnimationImages:arrayWithSprites];
}
NSLog(@"Sprite images: %i", [imageView.animationImages count]);
float animationDuration = [imageView.animationImages count] * 0.100; // 100ms per frame
[imageView setAnimationRepeatCount:0];
[imageView setAnimationDuration:animationDuration];
[imageView startAnimating];
[((UIButton *)sender) setTitle:NSLocalizedString(@"Stop animation!", @"")
forState:UIControlStateNormal];
}
- (IBAction)locationValueChanged:(id)sender
{
locationLabel.text = [NSString stringWithFormat:@"%.0f", ((UIStepper *)sender).value];
}
- (IBAction)lengthValueChanged:(id)sender
{
lengthLabel.text = [NSString stringWithFormat:@"%.0f", ((UIStepper *)sender).value];
}
- (IBAction)switchValueChanged:(id)sender
{
UISwitch *sw = (UISwitch *)sender;
[settingsView setUserInteractionEnabled:[sw isOn]];
[UIView beginAnimations:nil context:nil];
if ([sw isOn])
{
[settingsView setAlpha:1.0];
}
else
{
[settingsView setAlpha:0.2];
}
[UIView commitAnimations];
}
@end