Re: Why does a custom CALayer property work differently from a standard one?
Re: Why does a custom CALayer property work differently from a standard one?
- Subject: Re: Why does a custom CALayer property work differently from a standard one?
- From: Corbin Dunn <email@hidden>
- Date: Thu, 25 Oct 2012 14:56:12 -0700
On Oct 25, 2012, at 4:38 AM, Roland King <email@hidden> wrote:
> Ive been struggling with this for a couple of days now.
>
> I have a custom CALayer with a custom property, percentage. It's @dynamic and set up to cause a redraw.
>
> +(BOOL)needsDisplayForKey:(NSString *)key
> {
> return [ @"percentage" isEqualToString:key ];
> }
>
> I return CABasicAnimations for percentage and for position with the code at the bottom in actionForKey:. The position one just makes the position change really slowly.
>
> For position all I have to do is make a CABasicAnimation, set the duration and nothing else and return it, the position animates smoothly from where it was to where it's going.
>
> If I do the same for 'percentage', just return a CABasicAnimation with the duration set, it doesn't animate, it just goes instantly to the final value. For this one I have to explicitly set the 'fromValue' to the current value in the presentation layer, leaving it nil just doesn't do the job. It finds the 'toValue' itself, but I have to tell it where it's coming from.
>
> Why do they work differently? This prevents me from using addAnimation:forKey: for 'percentage' because each one is different, depending what the current value of percentage is.
>
>
> -(id<CAAction>)actionForKey:(NSString *)event
> {
>
> if( [ event isEqualToString:@"position" ] )
> {
> CABasicAnimation *animation = [ CABasicAnimation animationWithKeyPath:@"position" ];
> animation.duration = 7.f;
> return animation;
Technically, the above code is incorrect. You have to set one of the values, per the header comment.
/* The objects defining the property values being interpolated between.
* All are optional, and no more than two should be non-nil. The object
* type should match the type of the property being animated (using the
* standard rules described in CALayer.h). The supported modes of
* animation are:
*
* - both `fromValue' and `toValue' non-nil. Interpolates between
* `fromValue' and `toValue'.
*
* - `fromValue' and `byValue' non-nil. Interpolates between
* `fromValue' and `fromValue' plus `byValue'.
*
* - `byValue' and `toValue' non-nil. Interpolates between `toValue'
* minus `byValue' and `toValue'.
*
* - `fromValue' non-nil. Interpolates between `fromValue' and the
* current presentation value of the property.
*
* - `toValue' non-nil. Interpolates between the layer's current value
* of the property in the render tree and `toValue'.
*
* - `byValue' non-nil. Interpolates between the layer's current value
* of the property in the render tree and that plus `byValue'. */
corbin
> }
>
> if( [ event isEqualToString:@"percentage" ] )
> {
> CABasicAnimation *animation = [ CABasicAnimation animationWithKeyPath:@"percentage" ];
> animation.duration = 2.f;
> animation.fromValue = [ self.presentationLayer valueForKey:event ];
> return animation;
> }
>
> return [ super actionForKey:event ];;
> }
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden