使用 Core Animation/CALayers 為時鐘設置動畫 (Animating a Clock with Core Animation/CALayers)


問題描述

使用 Core Animation/CALayers 為時鐘設置動畫 (Animating a Clock with Core Animation/CALayers)

I have a UIViewControler with 4 layers. 

  1. Face Plate of a Watch/Clock
  2. Second Hand
  3. Minute Hand
  4. Hour Hand

I've gotten each hand to start moving onload at the correct time (by defining angles on a circle based on the time the app-loads). 

How do I correctly define the motion and duration of motion (should be ad infinitum) using the CABasicAnimation accessor methods? 

For instance, I've set my secondHandMotion (which is an instantiated CABasicAnimation object) to being at an angle on the clock frame that corresponds to current time:

CABasicAnimation *secondHandMotion = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
secondHandMotion.fromValue = [NSNumber numberWithFloat:sAlpha];
secondHandMotion.toValue = [NSNumber numberWithFloat:(2*M_PI)];
[secondHand addAnimation:secondHandMotion forKey:@"transform"];

where sAlpha is the angle as a function of time.  The secondHandMotion.toValue statement is wrong, I know that. How do I tell it to keep moving, and at the correct interval, 1-second ticks?

Thanks!


參考解法

方法 1:

When I implemented this in one of my apps I started from this tutorial

Analog Clock using Quartz Core broken link

See Source on Github instead.

It recalculates every second which may give slightly more accurate results relating to the system clock.

The way I implemented the rotation was something like this (this is the animation part you still need the angle calculations you have done previously).

CGAffineTransform transform = CGAffineTransformIdentity;

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];

self.hourView.transform   = CGAffineTransformRotate(transform, hourAngle);
self.minView.transform    = CGAffineTransformRotate(transform, minutesAngle);
self.secondView.transform = CGAffineTransformRotate(transform, secondsAngle);

[UIView commitAnimations];

NOTE: I wrote this very early on in my iOS learning so it may be a bit horrible.

方法 2:

Have a look at PSAnalogClockView. It nicely solves your problem in a reusable way.

(by ArtSabintsevPaul.sMartin Reichl)

參考文件

  1. Animating a Clock with Core Animation/CALayers (CC BY-SA 3.0/4.0)

#nsdate #objective-c #core-animation #iOS #calayer






相關問題

如何使 UIDatePicker 顯示用戶選擇的時間 (How to make UIDatePicker to show user selected time)

將 ISO 8601 轉換為 NSDate (Convert ISO 8601 to NSDate)

NSDateFormatter 具有 NULL 值的部分結果 (NSDateFormatter Partial Results With NULL Values)

Cocoa touch如何將標籤設置為NSDate (Cocoa touch how to set label to NSDate)

即使電話日期時間錯誤,如何獲得正確的日期和時間? (How to get the correct date and time even if the phone date time is wrong?)

如何將字符串轉換為具有無關字符的 NSDate(swift)? (How to convert string to NSDate that has extraneous characters (swift)?)

找出兩個日期之間的確切差異 (Find exact difference between two dates)

使用 Core Animation/CALayers 為時鐘設置動畫 (Animating a Clock with Core Animation/CALayers)

返回 NULL 值 (Returns NULL value)

在iphone中排序NSDate的NSString (Sort NSString of NSDate in iphone)

我如何通過 Foundation 獲取星期幾? (How do I get the day of the week with Foundation?)

以秒為單位獲取設置的紀元時間和現在之間的差異 (Get difference between set epoch time and now in seconds)







留言討論