/
main.m
144 lines (120 loc) · 4.46 KB
/
main.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
/*
Erica Sadun, http://ericasadun.com
iPhone Developer's Cookbook, 6.x Edition
BSD License, Use at your own risk
*/
#import <UIKit/UIKit.h>
#define COOKBOOK_PURPLE_COLOR [UIColor colorWithRed:0.20392f green:0.19607f blue:0.61176f alpha:1.0f]
#define BARBUTTON(TITLE, SELECTOR) [[UIBarButtonItem alloc] initWithTitle:TITLE style:UIBarButtonItemStylePlain target:self action:SELECTOR]
@interface DragView : UIImageView
{
CGPoint previousLocation;
}
@end
@implementation DragView
- (id) initWithImage: (UIImage *) anImage
{
if (self = [super initWithImage:anImage])
{
self.userInteractionEnabled = YES;
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
self.gestureRecognizers = @[panRecognizer];
}
return self;
}
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// Promote the touched view
[self.superview bringSubviewToFront:self];
// Remember original location
previousLocation = self.center;
}
- (void) handlePan: (UIPanGestureRecognizer *) uigr
{
CGPoint translation = [uigr translationInView:self.superview];
self.center = CGPointMake(previousLocation.x + translation.x,
previousLocation.y + translation.y);
}
@end
@interface TestBedViewController : UIViewController
@end
@implementation TestBedViewController
- (CGPoint) randomFlowerPosition
{
CGFloat halfFlower = 32.0f; // half of the size of the flower
// The flower must be placed fully within the view. Inset accordingly
CGSize insetSize = CGRectInset(self.view.bounds, 2*halfFlower, 2*halfFlower).size;
// Return a random position within the inset bounds
CGFloat randomX = random() % ((int)insetSize.width) + halfFlower;
CGFloat randomY = random() % ((int)insetSize.height) + halfFlower;
return CGPointMake(randomX, randomY);
}
- (void) layoutFlowers
{
// Move every flower into a new random place
[UIView animateWithDuration:0.3f animations: ^(){
for (UIView *flowerDragger in self.view.subviews)
flowerDragger.center = [self randomFlowerPosition];}];
}
- (void) viewDidAppear:(BOOL)animated
{
[self layoutFlowers];
}
- (void) loadView
{
[super loadView];
self.view.backgroundColor = [UIColor blackColor];
NSInteger maxFlowers = 12; // number of flowers to add
NSArray *flowerArray = @[@"blueFlower.png", @"pinkFlower.png", @"orangeFlower.png"];
// Add the flowers
for (int i = 0; i < maxFlowers; i++)
{
NSString *whichFlower = [flowerArray objectAtIndex:(random() % flowerArray.count)];
DragView *flowerDragger = [[DragView alloc] initWithImage:[UIImage imageNamed:whichFlower]];
[self.view addSubview:flowerDragger];
}
// Provide a "Randomize" button
self.navigationItem.rightBarButtonItem = BARBUTTON(@"Randomize", @selector(layoutFlowers));
}
- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
// Check for any off-screen flowers and move them into place
CGFloat halfFlower = 32.0f;
CGRect targetRect = CGRectInset(self.view.bounds, halfFlower * 2, halfFlower * 2);
targetRect = CGRectOffset(targetRect, halfFlower, halfFlower);
for (UIView *flowerDragger in self.view.subviews)
if (!CGRectContainsPoint(targetRect, flowerDragger.center))
[UIView animateWithDuration:0.3f animations:
^(){flowerDragger.center = [self randomFlowerPosition];}];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
@end
#pragma mark -
#pragma mark Application Setup
@interface TestBedAppDelegate : NSObject <UIApplicationDelegate>
{
UIWindow *window;
}
@end
@implementation TestBedAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[application setStatusBarHidden:YES];
[[UINavigationBar appearance] setTintColor:COOKBOOK_PURPLE_COLOR];
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
TestBedViewController *tbvc = [[TestBedViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:tbvc];
window.rootViewController = nav;
[window makeKeyAndVisible];
return YES;
}
@end
int main(int argc, char *argv[]) {
@autoreleasepool {
int retVal = UIApplicationMain(argc, argv, nil, @"TestBedAppDelegate");
return retVal;
}
}