iOS – Subclassing UIView

Moving on to Chapter 6 in Joe Conway’s book, this chapter takes us further from previously using UIButton and UILabel to UIView. This is illustrated with the Hypnosister App, which draws concentric circles on the screen programmatically. I have a bittersweet taste towards creating artwork programatically, only because as a Photoshop wonk, I like having an excuse to use it. However, I get that it reduces file sizes and the like. It just seems like a lot of writing just to draw some circles:

– (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

CGRect wholeWindow = [window bounds];

UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:wholeWindow];

[window addSubview:scrollView];

// Make your view twice as large as the window
CGRect reallyBigRect;

reallyBigRect.origin = CGPointZero;

reallyBigRect.size.width = wholeWindow.size.width * 2;

reallyBigRect.size.height = wholeWindow.size.height * 2;

[scrollView setContentSize:reallyBigRect.size];

// Center it in the scroll view
CGPoint offset;

offset.x = wholeWindow.size.width * 0.5;

offset.y = wholeWindow.size.height * 0.5;

[scrollView setContentOffset:offset];

// Enable zooming
[scrollView setMinimumZoomScale:0.5];

[scrollView setMaximumZoomScale:5];

[scrollView setDelegate:self];

// Create the view
view = [[HypnosisView alloc] initWithFrame:reallyBigRect];

[view setBackgroundColor:[UIColor clearColor]];

[scrollView addSubview:view];

[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];

[window makeKeyAndVisible];

return YES;

}

Again, I’m struggling to fully comprehend the idea behind classes, sub-classes and the act of “subclassing”. I keep trying to relate them back to CSS classes, and I’m not sure that’s an appropriate metaphor, which may be causing more confusion than it should.

Leave a Reply

Your email address will not be published.