-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLayerPerformanceViewController.m
More file actions
185 lines (160 loc) · 6.96 KB
/
LayerPerformanceViewController.m
File metadata and controls
185 lines (160 loc) · 6.96 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
//
// LayerPerformanceViewController.m
// LayerPerformance
//
// Created by Nikolai Ruhe on 01.05.11.
// Copyright 2011 Savoy Software. All rights reserved.
//
#import "LayerPerformanceViewController.h"
#import <QuartzCore/QuartzCore.h>
#define DRAGGABLE_VIEWS_COUNT 30
@interface LayerPerformanceViewController()
@property (nonatomic) BOOL lightweight;
- (void)updateLinesForView:(UIView *)draggableView;
- (void)toggleMode;
@end
@implementation LayerPerformanceViewController
@synthesize window = _window;
@synthesize container = _container;
@synthesize lightweight = _lightweight;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window.rootViewController = self;
[self.window makeKeyAndVisible];
return YES;
}
- (void)toggleMode:(UIButton *)toggleButton
{
[self toggleMode];
[toggleButton setTitle:self.lightweight ? @"Core Animation" : @"Core Graphics"
forState:UIControlStateNormal];
}
- (void)loadView
{
self.container = [[[UIView alloc] init] autorelease];
self.container.bounds = (CGRect){ {0, 0}, {768, 1024} };
self.container.backgroundColor = [UIColor whiteColor];
self.container.opaque = YES;
self.container.center = (CGPoint){ 384, 512 };
self.view = [[[UIView alloc] init] autorelease];
self.view.bounds = (CGRect){ {0, 0}, {768, 1024} };
[self.view addSubview:self.container];
UIButton *toggleButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
toggleButton.frame = (CGRect){{620, 10},{130, 32}};
[toggleButton addTarget:self
action:@selector(toggleMode:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:toggleButton];
for (NSUInteger idx = 0; idx < DRAGGABLE_VIEWS_COUNT; ++idx) {
CGPoint position = { random() % 728 + 20, random() % 984 + 20};
UIView *draggableView = [[[UIView alloc] init] autorelease];
draggableView.bounds = (CGRect){{0, 0},{40, 40}};
draggableView.center = position;
CALayer *draggableLayer = draggableView.layer;
draggableLayer.cornerRadius = 20;
draggableLayer.sublayerTransform = CATransform3DMakeTranslation(20, 20, 0);
[self.container addSubview:draggableView];
UIPanGestureRecognizer *gestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self
action:@selector(didDragView:)];
[draggableView addGestureRecognizer:gestureRecognizer];
for (NSUInteger i = 0; i < idx; ++i) {
CALayer *lineLayer = [CALayer layer];
lineLayer.opacity = 0.2;
[draggableLayer addSublayer:lineLayer];
}
[self updateLinesForView:draggableView];
}
[self toggleMode:toggleButton];
}
static void setLayerToLineFromAToB(CALayer *layer, CGPoint a, CGPoint b, CGFloat lineWidth)
{
CGPoint center = { 0.5 * (a.x + b.x), 0.5 * (a.y + b.y) };
CGFloat length = sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
CGFloat angle = atan2(a.y - b.y, a.x - b.x);
layer.position = center;
layer.bounds = (CGRect) { {0, 0}, { length + lineWidth, lineWidth } };
layer.transform = CATransform3DMakeRotation(angle, 0, 0, 1);
}
- (void)toggleMode
{
[CATransaction setDisableActions:YES];
self.lightweight = ! self.lightweight;
for (UIView *draggableView in self.container.subviews) {
draggableView.backgroundColor = self.lightweight ? [UIColor orangeColor] : [UIColor blueColor];
for (CALayer *lineLayer in draggableView.layer.sublayers) {
if (self.lightweight) {
lineLayer.contents = nil;
lineLayer.backgroundColor = [UIColor orangeColor].CGColor;
lineLayer.delegate = nil;
} else {
lineLayer.transform = CATransform3DIdentity;
lineLayer.backgroundColor = NULL;
lineLayer.delegate = self;
}
}
}
[CATransaction setDisableActions:NO];
for (UIView *draggableView in self.container.subviews) {
[self updateLinesForView:draggableView];
}
}
- (void)updateLinesForView:(UIView *)draggableView
{
[CATransaction setDisableActions:YES];
NSArray *draggableViews = draggableView.superview.subviews;
NSArray *lineLayers = draggableView.layer.sublayers;
NSUInteger viewIndex = [draggableViews indexOfObject:draggableView];
CGPoint pos = draggableView.center;
for (NSUInteger i = 0; i < [lineLayers count]; ++i) {
CALayer *lineLayer = [lineLayers objectAtIndex:i];
CGPoint target = ((UIView *)[draggableViews objectAtIndex:i]).center;
target.x -= pos.x;
target.y -= pos.y;
if (self.lightweight) {
setLayerToLineFromAToB(lineLayer, CGPointZero, target, 8);
} else {
lineLayer.frame = (CGRect){ { target.x < 0 ? target.x : 0, target.y < 0 ? target.y : 0 }, { fabs(target.x), fabs(target.y) } };
[lineLayer setValue:[NSNumber numberWithBool:(target.x < 0) ^ (target.y < 0)] forKey:@"alternate"];
[lineLayer setNeedsDisplay];
}
}
for (NSUInteger i = viewIndex + 1; i < [draggableViews count]; ++i) {
UIView *otherDraggableView = [draggableViews objectAtIndex:i];
CALayer *lineLayer = [otherDraggableView.layer.sublayers objectAtIndex:viewIndex];
CGPoint target = otherDraggableView.center;
target.x = pos.x - target.x;
target.y = pos.y - target.y;
if (self.lightweight) {
setLayerToLineFromAToB(lineLayer, CGPointZero, target, 8);
} else {
lineLayer.frame = (CGRect){ { target.x < 0 ? target.x : 0, target.y < 0 ? target.y : 0 }, { fabs(target.x), fabs(target.y) } };
[lineLayer setValue:[NSNumber numberWithBool:(target.x < 0) ^ (target.y < 0)] forKey:@"alternate"];
[lineLayer setNeedsDisplay];
}
}
[CATransaction setDisableActions:NO];
}
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
if (self.lightweight)
return;
if ([[layer valueForKey:@"alternate"] boolValue]) {
CGContextMoveToPoint(ctx, layer.bounds.size.width, 0);
CGContextAddLineToPoint(ctx, 0, layer.bounds.size.height);
} else {
CGContextMoveToPoint(ctx, 0, 0);
CGContextAddLineToPoint(ctx, layer.bounds.size.width, layer.bounds.size.height);
}
CGContextSetStrokeColorWithColor(ctx, [UIColor blueColor].CGColor);
CGContextSetLineWidth(ctx, 8);
CGContextStrokePath(ctx);
}
- (void)didDragView:(UIPanGestureRecognizer *)gestureRecognizer
{
CGPoint translation = [gestureRecognizer translationInView:self.container];
[gestureRecognizer setTranslation:CGPointZero inView:self.container];
CGPoint center = gestureRecognizer.view.center;
gestureRecognizer.view.center = (CGPoint){ center.x + translation.x, center.y + translation.y };
[self updateLinesForView:gestureRecognizer.view];
}
@end