Any example iPhone code for an Analog Clock?

2019-07-15 02:00发布

问题:

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

回答1:

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.