Drawing the circle


#1

I am hoping someone can shed some light on what I may be doing wrong. In the book example, on page 102, their circle is positioned nicely about an inch below the carrier, time, etc…

When my circle appears, the top of the circle is at the top of the screen, going right through the clock. I went back and checked my code against theirs and it looks likeI am doing everything just as they are.

BNRAppDelegate.m

[code]#import “BNRAppDelegate.h”
#import “BNRHypnosisView.h”

@implementation BNRAppDelegate

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

// CGRect firstFrame = CGRectMake(160, 240, 100, 150);
CGRect firstFrame = self.window.bounds;
BNRHypnosisView *firstView = [[BNRHypnosisView alloc] initWithFrame:firstFrame];

[self.window addSubview:firstView];
//self.window.backgroundColor = [UIColor whiteColor];

[self.window makeKeyAndVisible];
return YES;

}
[/code]

BNRHypnosisView.h

#import "BNRHypnosisView.h"

@implementation BNRHypnosisView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //All BNRHypnosisViews start with a clear background color
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    CGRect bounds = self.bounds;
    
    //Figure out the center fo the bounds rectangle
    CGPoint center;
    center.x = bounds.origin.x + bounds.size.width / 2.0;
    center.y = bounds.origin.y + bounds.size.width / 2.0;

    //The circle will be the largest that will fit in the view
    float radius = (MIN(bounds.size.width, bounds.size.height) / 2.0);

    //UIBezierPath define and draw lines/curves that you can use to make shapes.
    UIBezierPath *path = [[UIBezierPath alloc] init];
    
    //Add an arc to the path at center, with radius of radius, from 0 to 2*pi radians (a circle)
    [path addArcWithCenter:center radius:radius startAngle:0.0 endAngle:M_PI * 2 clockwise:YES];
    
    path.lineWidth = 10;
    
    [[UIColor lightGrayColor] setStroke];
    
    //Draw the line.
    [path stroke];
}


@end

#2

[quote] ... //Figure out the center fo the bounds rectangle CGPoint center; center.x = bounds.origin.x + bounds.size.width / 2.0; center.y = bounds.origin.y + bounds.size.width / 2.0; ... [/quote]
That center is not quite the center of the bounds rectangle. Compare it to what’s in the book.
bounds.size.height not bounds.size.width should be used in the computation of center.y.


#3

ahhh, I see the error. Thanks!