Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You asked kind of the <a href="https://stackoverflow.com/questions/4767794/analog-clock-with-uiimages-for-hands">same question</a> and I said that Layers is much better... well this time I have written the hole program in a record time than less than three weeks ;). If you want to use images then get your images and set them inside <code>initWithFrame:</code></p> <p>Interface: ClockView.h</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; #import &lt;QuartzCore/QuartzCore.h&gt; @interface ClockView : UIView { CALayer *containerLayer; CALayer *hourHand; CALayer *minHand; CALayer *secHand; NSTimer *timer; } - (void) start; - (void) stop; @end </code></pre> <p>Implementation: ClockView.m</p> <pre><code>#import "ClockView.h" @implementation ClockView float Degrees2Radians(float degrees) { return degrees * M_PI / 180; } - (void) start{ timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateClock) userInfo:nil repeats:YES]; } - (void) stop{ [timer invalidate]; timer = nil; } - (void) updateClock{ NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:[NSDate date]]; NSInteger seconds = [dateComponents second]; NSInteger minutes = [dateComponents minute]; NSInteger hours = [dateComponents hour]; //correction of inverted clock seconds += 30; seconds %=60; minutes += 30; minutes %=60; hours += 6; hours %=12; hourHand.transform = CATransform3DMakeRotation (Degrees2Radians(hours*360/12), 0, 0, 1); minHand.transform = CATransform3DMakeRotation (Degrees2Radians(minutes*360/60), 0, 0, 1); secHand.transform = CATransform3DMakeRotation (Degrees2Radians(seconds*360/60), 0, 0, 1); } - (void) layoutSubviews{ [super layoutSubviews]; containerLayer.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height); float length = MIN(self.frame.size.width, self.frame.size.height)/2; CGPoint c = CGPointMake(self.frame.size.width/2, self.frame.size.height/2); hourHand.position = minHand.position = secHand.position = c; hourHand.bounds = CGRectMake(0,0,10,length*0.5); minHand.bounds = CGRectMake(0,0,8,length*0.8); secHand.bounds = CGRectMake(0,0,4,length); hourHand.anchorPoint = CGPointMake(0.5,0.0); minHand.anchorPoint = CGPointMake(0.5,0.0); secHand.anchorPoint = CGPointMake(0.5,0.0); } - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { containerLayer = [[CALayer layer] retain]; hourHand = [[CALayer layer] retain]; minHand = [[CALayer layer] retain]; secHand = [[CALayer layer] retain]; //paint your hands: simple colors hourHand.backgroundColor = [UIColor blackColor].CGColor; hourHand.cornerRadius = 3; minHand.backgroundColor = [UIColor grayColor].CGColor; secHand.backgroundColor = [UIColor whiteColor].CGColor; secHand.borderWidth = 1.0; secHand.borderColor = [UIColor grayColor].CGColor; //put images //hourHand.contents = (id)[UIImage imageNamed:@"hour.png"].CGImage; //minHand.contents = (id)[UIImage imageNamed:@"min.png"].CGImage; //secHand.contents = (id)[UIImage imageNamed:@"sec.png"].CGImage; [containerLayer addSublayer:hourHand]; [containerLayer addSublayer:minHand]; [containerLayer addSublayer:secHand]; [self.layer addSublayer:containerLayer]; self.layer.borderColor = [UIColor greenColor].CGColor; self.layer.borderWidth = 1.0; } return self; } - (void)dealloc { [self stop]; [hourHand release]; [minHand release]; [secHand release]; [containerLayer release]; [super dealloc]; } @end </code></pre> <p>Usage:</p> <pre><code>#import "ClockView.h" - (void)viewDidLoad { [super viewDidLoad]; ClockView *clockView = [[ClockView alloc] initWithFrame:CGRectMake(0,0,100,100)]; [self.view addSubview:clockView]; [clockView start]; [clockView release]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload