Circles not centering on screen


#1

Hi guys,

I’m sure this is most likely a typo somewhere, but I cannot get the circles to center on the screen. I’ve been looking through my code but can’t seem to located where the problem is coming from. Any help would be much appreciated!

HypnosisView.m

[code]//
// HypnosisView.m
// Hypnosister
//
// Created by Ben on 10/22/12.
// Copyright © 2012 Ben. All rights reserved.
//

#import “HypnosisView.h”

@implementation HypnosisView

-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self){
//all hypnosis views start with a clear background color
[self setBackgroundColor:[UIColor clearColor]];
}
return self;
}

-(void)drawRect:(CGRect)dirtyRect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect bounds = [self bounds];

//figure out center of the bounds of rectangle
CGPoint center;
center.x = bounds.origin.x + bounds.size.width /2.0;
center.y = bounds.origin.y + bounds.size.width /2.0;

//radius of the circle should be nearly as big as view
//float maxRadius = hypot(bounds.size.width, bounds.size.height) / 4.0;
float maxRadius = hypot(bounds.size.width, bounds.size.height) / 2.0;

//set line and thickness
CGContextSetLineWidth(ctx, 10);

//color
//CGContextSetRGBStrokeColor(ctx, 0.6, 0.6, 0.6, 1.0);

//[[UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1] setStroke];
[[UIColor lightGrayColor] setStroke];

//add shape to context (doesn't draw shape)
//CGContextAddArc(ctx, center.x, center.y, maxRadius, 0.0, M_PI * 2.0, YES);

//perform drawing instrution; draw current shape with current state
//CGContextStrokePath(ctx);

//draw concentric circles from the outside in
for (float currentRadius = maxRadius; currentRadius > 0; currentRadius -=20){
    //add a path to the context
    CGContextAddArc(ctx, center.x, center.y, currentRadius, 0, M_PI * 2.0, YES);
    
    //perform drawing instruction; removes path
    CGContextStrokePath(ctx);
}

//create a string
NSString *text = @"You are getting sleepy.";

//get a font to draw it in
UIFont *font = [UIFont boldSystemFontOfSize:28];

CGRect textRect;

//how big is this string when drawn in this font?
textRect.size = [text sizeWithFont:font];

//lets put that string in the center of the view
textRect.origin.x = center.x = textRect.size.width / 2.0;
textRect.origin.y = center.y - textRect.size.height / 2.0;

//set the fill color of the current context to black
[[UIColor blackColor] setFill];

//draw the string
[text drawInRect:textRect withFont:font];

}

@end
[/code]

HypnosisAppDelegate.m

[code]//
// HypnosisterAppDelegate.m
// Hypnosister
//
// Created by Ben on 10/22/12.
// Copyright © 2012 Ben. All rights reserved.
//

#import “HypnosisterAppDelegate.h”
#import “HypnosisView.h”

@implementation HypnosisterAppDelegate

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    //CGRect viewFrame = CGRectMake(160, 240, 100, 150);

    //HypnosisView *view = [[HypnosisView alloc] initWithFrame:viewFrame];
    //[view setBackgroundColor:[UIColor redColor]];

    HypnosisView *view = [[HypnosisView alloc] initWithFrame:[[self window] bounds]];
    [[self window] addSubview:view];

    //CGRect anotherFrame = CGRectMake(20, 30, 50, 50);

    //HypnosisView *anotherView = [[HypnosisView alloc] initWithFrame:anotherFrame];
    //[anotherView setBackgroundColor:[UIColor blueColor]];

    //[[self window] addSubview:anotherView];
    //[view addSubview:anotherView];

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
    }

  • (void)applicationWillResignActive:(UIApplication *)application
    {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

  • (void)applicationDidEnterBackground:(UIApplication *)application
    {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

  • (void)applicationWillEnterForeground:(UIApplication *)application
    {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

  • (void)applicationDidBecomeActive:(UIApplication *)application
    {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

  • (void)applicationWillTerminate:(UIApplication *)application
    {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }

@end
[/code]


#2

Yep, it’s a typo. You are calculating both the x and y based on the width. You need to calculate y based on the height, so in drawRect change:

//figure out center of the bounds of rectangle CGPoint center; center.x = bounds.origin.x + bounds.size.width /2.0; center.y = bounds.origin.y + bounds.size.width /2.0;

to:

//figure out center of the bounds of rectangle CGPoint center; center.x = bounds.origin.x + bounds.size.width /2.0; center.y = bounds.origin.y + bounds.size.height /2.0;