Graphics Reference
In-Depth Information
the result of calling -animationForKey: with the animation object passed to our delegate
method. It's not exactly an elegant solution, though.
Fortunately, there is an easier way. Like all NSObject subclasses, CAAnimation conforms
to the KVC (Key-Value Coding) ad hoc protocol, which allows you to set and get
properties by name using the -setValue:forKey: and -valueForKey: methods. But
CAAnimation has an unusual feature: It acts like an NSDictionary , allowing you to set
arbitrary key/value pairs even if they do not match up to any of the declared properties of
the animation class that you are using.
This means that you can tag an animation with additional data for your own use. In this
case, we will attach the clock hand UIView to the animation, so we can easily determine
which view each animation relates to. We can then use this information in the delegate
method to update the correct hand (see Listing 8.4).
Listing 8.4 Using KVC to Tag an Animation with Additional Data
@interface ViewController ()
@property ( nonatomic , weak ) IBOutlet UIImageView *hourHand;
@property ( nonatomic , weak ) IBOutlet UIImageView *minuteHand;
@property ( nonatomic , weak ) IBOutlet UIImageView *secondHand;
@property ( nonatomic , weak ) NSTimer *timer;
@end
@implementation ViewController
- ( void )viewDidLoad
{
[ super viewDidLoad ];
//adjust anchor points
self .secondHand. layer . anchorPoint = CGPointMake ( 0.5f , 0.9f );
self .minuteHand. layer . anchorPoint = CGPointMake ( 0.5f , 0.9f );
self .hourHand. layer . anchorPoint = CGPointMake ( 0.5f , 0.9f );
//start timer
self .timer = [ NSTimer scheduledTimerWithTimeInterval : 1.0
target : self
selector : @selector (tick)
userInfo : nil
repeats : YES ];
//set initial hand positions
[ self updateHandsAnimated : NO ];
Search WWH ::




Custom Search