Featured post
iphone - Inheriting UIView - Losing instance variables -
bit of objective-c rookie, i've looked around answer haven't been able find 1 forgive me if obvious question.
basically, need draw on screen segments of circle (for instance, 90 degree segment, horizontal , vertical line meet @ bottom left , arc connects end points). i've managed achieve in custom class called circlesegment inherits uiview , overrides drawrect
.
my issue achieving programatically; need way of creating circlesegment class , storing in desired angle before draws segment itself.
here's have far:
circlesegment.h
#import <uikit/uikit.h> @interface circlesegment : uiview { float anglesize; uicolor *backcolor; } -(float)convertdegtorad:(float)degrees; -(float)convertradtodeg:(float)radians; @property (nonatomic) float anglesize; @property (nonatomic, retain) uicolor *backcolor; @end
circlesegment.m
#import "circlesegment.h" @implementation circlesegment @synthesize anglesize; @synthesize backcolor; // initialisation overrides // ------------------------ - (id)initwithframe:(cgrect)frame { if ((self = [super initwithframe:frame])) { self.opaque = no; self.backgroundcolor = [uicolor clearcolor]; } return self; } - (void)setbackgroundcolor:(uicolor *)newbgcolor { // ignore. } - (void)setopaque:(bool)newisopaque { // ignore. } // math functions // -------------- // converts degrees radians. -(float)convertdegtorad:(float)degrees { return degrees * m_pi / 180; } // converts radians degrees. -(float)convertradtodeg:(float)radians { return radians * 180 / m_pi; } // drawing code // ------------ - (void)drawrect:(cgrect)rect { float endangle = 360 - anglesize; uibezierpath* apath = [uibezierpath bezierpathwitharccenter:cgpointmake(100, 100) radius:100 startangle:[self convertdegtorad:270] endangle:[self convertdegtorad:endangle] clockwise:yes]; [apath addlinetopoint:cgpointmake(100.0, 100.0)]; [apath closepath]; cgcontextref aref = uigraphicsgetcurrentcontext(); cgcontextsetfillcolorwithcolor(aref, backcolor.cgcolor); cgcontextsavegstate(aref); apath.linewidth = 1; [apath fill]; [apath stroke]; //cgcontextrestoregstate(aref); } - (void)dealloc { [super dealloc]; } @end
note .m file bit messy various bits of test code...
so want create instance of circlesegment, store angle in anglesize property, draw segment based on angle , add view main app view display it...
to try , achieve that, i've add following test code viewdidload
on viewcontroller:
circlesegment *seg1 = [[circlesegment alloc] init]; seg1.backcolor = [uicolor greencolor]; seg1.anglesize = 10; [self.view addsubview:seg1];
it seems store uicolor , anglesize fine when breakpoint places, if place breakpoint in drawrect
overrode on circlesegment.m, values have reverted nil values (or whatever correct term be, feel free correct me).
i'd appreciate if point me in right direction!
thanks
it seems store uicolor , anglesize fine when breakpoint places, if place breakpoint in drawrect overrode on circlesegment.m, values have reverted nil values (or whatever correct term be, feel free correct me).
are sure working same instance?
drop in nslog(@"%@ %p", nsstringfromselector(_cmd), self);
in viewdidload
, in drawrect
, make sure dealing same instance.
(i've seen cases developer alloc/initwithframe:
view , use instance created during nib loading, wondering why that instance wasn't initialized.)
- Get link
- X
- Other Apps
Comments
Post a Comment