Skip to content

Commit f786daf

Browse files
committed
add magnification label
1 parent f1a9b7f commit f786daf

File tree

5 files changed

+62
-15
lines changed

5 files changed

+62
-15
lines changed

iMandelbrot.xcodeproj/project.pbxproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@
399399
buildSettings = {
400400
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
401401
ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage;
402-
GCC_OPTIMIZATION_LEVEL = 0;
402+
GCC_OPTIMIZATION_LEVEL = fast;
403403
GCC_PRECOMPILE_PREFIX_HEADER = YES;
404404
GCC_PREFIX_HEADER = "iMandelbrot/iMandelbrot-Prefix.pch";
405405
INFOPLIST_FILE = "iMandelbrot/iMandelbrot-Info.plist";

iMandelbrot/USKFractalView.h

+9
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,17 @@
88

99
#import <UIKit/UIKit.h>
1010

11+
@protocol USKFractalViewDelegate <NSObject>
12+
13+
- (void)updateMagnificationLabel;
14+
15+
@end
16+
1117
@interface USKFractalView : UIImageView
1218

19+
@property id delegate;
20+
@property (readonly) double currentMagnification;
21+
1322
- (void)drawMandelbrotSet;
1423
- (void)drawMandelbrotSetInComplexRect:(CGRect)cRect;
1524

iMandelbrot/USKFractalView.m

+29-7
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,31 @@ @implementation USKFractalView {
1414
CGRect currentCRect;
1515
}
1616

17+
@synthesize delegate;
18+
@synthesize currentMagnification;
19+
1720
- (id)initWithFrame:(CGRect)frame
1821
{
1922
self = [super initWithFrame:frame];
2023
if (self) {
2124
// Initialization code
2225
self.contentMode = UIViewContentModeScaleAspectFit;
2326
self.userInteractionEnabled = YES;
27+
28+
width = frame.size.width;
29+
height = frame.size.height;
30+
31+
// if ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] &&
32+
// ([UIScreen mainScreen].scale == 2.0)) {
33+
// // Retina display
34+
// width = frame.size.width * 2.0;
35+
// height = frame.size.height * 2.0;
36+
// } else {
37+
// // non-Retina display
38+
// width = frame.size.width;
39+
// height = frame.size.height;
40+
// }
41+
2442
[self drawMandelbrotSet];
2543
}
2644
return self;
@@ -29,6 +47,8 @@ - (id)initWithFrame:(CGRect)frame
2947
- (void)drawMandelbrotSet
3048
{
3149
CGRect complexRect = CGRectMake(-2.0, -2.0, 4.0, 4.0);
50+
currentMagnification = 1.0;
51+
[delegate updateMagnificationLabel];
3252
[self drawMandelbrotSetInComplexRect:complexRect];
3353
}
3454

@@ -41,9 +61,6 @@ - (void)drawMandelbrotSetInComplexRect:(CGRect)cRect
4161
NSUInteger iteration;
4262
double _Complex z0 = 0 + 0 * I;
4363

44-
width = 1536;
45-
height = width;
46-
4764
size_t bytePerPixel = sizeof(unsigned char) * 3;
4865
size_t bitsPerPixel, bytesPerRow;
4966
unsigned char *imageData = calloc(width * height, sizeof(unsigned char) * bytePerPixel);
@@ -113,10 +130,15 @@ - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
113130
CGPoint complexP = CGPointMake(currentCRect.origin.x + currentCRect.size.width * relativeP.x,
114131
currentCRect.origin.y + currentCRect.size.height * relativeP.y);
115132
CGSize newSize = CGSizeMake(currentCRect.size.width / magnification, currentCRect.size.height / magnification);
116-
[self drawMandelbrotSetInComplexRect:CGRectMake(complexP.x - newSize.width / 2.0,
117-
complexP.y - newSize.height / 2.0,
118-
newSize.width,
119-
newSize.height)];
133+
134+
[UIView animateWithDuration:0.5 animations:^{
135+
[self drawMandelbrotSetInComplexRect:CGRectMake(complexP.x - newSize.width / 2.0,
136+
complexP.y - newSize.height / 2.0,
137+
newSize.width,
138+
newSize.height)];
139+
}];
140+
currentMagnification *= magnification;
141+
[delegate updateMagnificationLabel];
120142
}
121143

122144

iMandelbrot/USKViewController.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
//
88

99
#import <UIKit/UIKit.h>
10+
#import "USKFractalView.h"
1011

11-
@interface USKViewController : UIViewController
12+
@interface USKViewController : UIViewController <USKFractalViewDelegate>
1213

1314
@end

iMandelbrot/USKViewController.m

+21-6
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
//
88

99
#import "USKViewController.h"
10-
#import "USKFractalView.h"
1110

12-
@interface USKViewController ()
11+
@interface USKViewController () {
12+
USKFractalView *fractalView;
13+
UILabel *magnificationLabel;
14+
}
1315

1416
@end
1517

@@ -20,16 +22,24 @@ - (void)viewDidLoad
2022
[super viewDidLoad];
2123
// Do any additional setup after loading the view, typically from a nib.
2224

23-
USKFractalView *iv = [[USKFractalView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.width)];
24-
iv.center = self.view.center;
25-
[self.view addSubview:iv];
25+
fractalView = [[USKFractalView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.width)];
26+
fractalView.delegate = self;
27+
fractalView.center = self.view.center;
28+
[self.view addSubview:fractalView];
2629
self.view.backgroundColor = [UIColor whiteColor];
2730

2831
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeSystem];
29-
[backButton addTarget:iv action:@selector(drawMandelbrotSet) forControlEvents:UIControlEventTouchUpInside];
32+
[backButton addTarget:fractalView action:@selector(drawMandelbrotSet) forControlEvents:UIControlEventTouchUpInside];
3033
backButton.frame = CGRectMake(20, [[UIScreen mainScreen] bounds].size.height - 108, [[UIScreen mainScreen] bounds].size.width - 40, 88);
3134
[backButton setTitle:@"Back" forState:UIControlStateNormal];
3235
[self.view addSubview:backButton];
36+
37+
magnificationLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, [[UIScreen mainScreen] bounds].size.width - 40, 88)];
38+
magnificationLabel.textAlignment = NSTextAlignmentRight;
39+
magnificationLabel.adjustsFontSizeToFitWidth = YES;
40+
magnificationLabel.font = [UIFont fontWithName:@"Helvetica" size:88];
41+
[self updateMagnificationLabel];
42+
[self.view addSubview:magnificationLabel];
3343
}
3444

3545
- (void)didReceiveMemoryWarning
@@ -38,4 +48,9 @@ - (void)didReceiveMemoryWarning
3848
// Dispose of any resources that can be recreated.
3949
}
4050

51+
- (void)updateMagnificationLabel
52+
{
53+
magnificationLabel.text = [NSString stringWithFormat:@"x%.1f", fractalView.currentMagnification];
54+
}
55+
4156
@end

0 commit comments

Comments
 (0)