2

Interested in creating by own analog clock application - anyone know of some example project/code that implements an analogue clock? (i.e. not a digital clock). Ideally with a custom background, and custom hour/minute hands as images?

I note that the following link exists, however was hoping for something just a little more specific to matching what I'm doing (background image, images for the hour/minute hands): Example Code

TeaCupApp
  • 11,316
  • 18
  • 70
  • 150
Greg
  • 34,042
  • 79
  • 253
  • 454
  • HI @Greg have u r problem, i also want to create an analog clock with the same features. i have custom images for background and hour/minute images .Please help me in solving out my problem.Thanks – Rani Jul 11 '11 at 11:58

1 Answers1

5

Create a background image, and images for each of the hands (seconds, minutes, hours — whatever you want). Every image must be as big as the clock itself, but do not scale. Put the hands at the 12 mark. Make everything else alpha in the images with the hands.

Now, put all images at the same location in your view with an image view/well. Your clock should read 12 o'clock.

In your code, create a method which will be called every second by a timer. This method will rotate the views so that the hands are rotated to the correct position. You can use the transform property of NSView or UIView.


Note: Apple explains this in detail: "Translating, Scaling, and Rotating Views". An example:

// M_PI/4.0 is one quarter of a half circle, or 45 degrees.
CGAffineTransform xform = CGAffineTransformMakeRotation(M_PI/4.0);
hourHandsImageView.transform = xform;

Just by changing the images, you can change the appearance.

Constantino Tsarouhas
  • 6,846
  • 6
  • 43
  • 54
  • oh ok - so can I clarify then: (a) the background image/view should be at the back then? - haven't tried putting views into background yet, (b) so each image is a view and you rotate the view not the image itself then? (c) how do you center the base of the hour hand UIView to the centre of the image - do you manually calculate this position programatically with reference to the parent view co-ordinates? – Greg Jun 27 '11 at 23:25
  • @Greg (a) Yes. (b) You rotate the image view containing the image. (c) When you rotate a view, the view isn't moved. Don't worry about the center having displaced. ;-) (By the way, you can set the `center` property of one image to set the `center` property of another one. If all three image views have the same value for the `center` property, all is well. It's that simple.) – Constantino Tsarouhas Jun 27 '11 at 23:35
  • thanks Randy - I'll try this out now and see if I can get the hang of it – Greg Jun 27 '11 at 23:45