• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
CAKeyframeAnimation for 'position' not working
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

CAKeyframeAnimation for 'position' not working


  • Subject: CAKeyframeAnimation for 'position' not working
  • From: Christopher J Kemsley <email@hidden>
  • Date: Thu, 1 Oct 2009 14:45:45 -0700

(Note: I apologize for not having this nicely formatted. My previous message - 8kB over the size limit - was rejected because it was too large, so I had to remove the extra HTML)


I have a CAKeyframeAnimation that simply will not work.

I have a layer. When I apply a CABasicAnimation to it, it animated exactly as expected. When I apply the keyframe animation, it doesn't...

Here's the code for the keyframe animation:

+ (CAAnimation*) randomPathAnimationWithBounds:(CGRect)inBounds keyFrames:(UInt32)inKeys layer:(CALayer*)inLayer duration:(CGFloat) duration {


// Setup our variables for keeping track of points and relative timings
CGPoint points[inKeys] ;
CGFloat timing[inKeys+1] ;

timing[inKeys] = CKRandomRange(1000) ; // CKRandomRange(range) is the same as random()%range
CGFloat total = timing[inKeys] ; // Keep a running tally of our total int count for later normalization


// This will produce a random point putting the layer somewhere outside the parent's bounds, supplied to us as bounds
CGPoint r1 = [self randomPointForLayer:inLayer outOfBounds:inBounds] ;
inLayer.position = r1 ; // apply the initial position


// Make our arrays
NSMutableArray * locArr = [NSMutableArray arrayWithCapacity:inKeys+1] ;
NSMutableArray * timArr = [NSMutableArray arrayWithCapacity:inKeys+1] ;

UInt32 i ;
for ( i=0 ; i<inKeys ; i++ )
{
// Get timing
timing[i] = CKRandomRange(1000) ; // Make a new random relative timing
total += timing[i] ; // add it to the running tally

// Get some random point inside the bounds
points[i] = CGPointMake ( CKRandomRange(inBounds.size.width) +inBounds.origin.x , CKRandomRange(inBounds.size.height) +inBounds.origin.y ) ;

// Add that point to the location array
[ locArr addObject: [NSValue valueWithCGPoint:points[i]] ] ;
}

// Get an endpoint, outside the bounds, and add it to the array
CGPoint r2 = [self randomPointForLayer:inLayer outOfBounds:inBounds] ;
[locArr addObject:[NSValue valueWithCGPoint:r2]] ;


// Normalize the timings, comulative
timing[0] = timing[0] / total * duration ;
for ( i=1 ; i<inKeys+1 ; i++ )
timing[i] = timing[i] / total * duration + timing[i-1] ;

// Fill the timing value array
for ( i=0 ; i<inKeys+1 ; i++ )
[timArr addObject:[NSNumber numberWithFloat:timing[i]]] ;


// Make the animation
CAKeyframeAnimation * animation = [CAKeyframeAnimation animationWithKeyPath:@"position"] ;
animation.values = locArr ;
animation.duration = duration ;
animation.calculationMode = kCAAnimationLinear ;
animation.keyTimes = timArr ;
animation.fillMode = kCAFillModeForwards ;


// Print stuff
NSLog ( @"%@" , animation ) ;
NSLog ( @" --> %lu, %lu" , locArr.count , timArr.count ) ;
NSLog ( @" --> %@" , animation.keyTimes ) ;
NSLog ( @" --> %@" , animation.values ) ;

return animation ;
}





The output, from the print statements, to show that it has VALID values, is:


2009-10-01 13:11:00.887 Whose Line[56132:207] <CAKeyframeAnimation: 0x3921510>
2009-10-01 13:11:00.888 Whose Line[56132:207] --> 11, 11
2009-10-01 13:11:00.888 Whose Line[56132:207] --> (
0.178501,
0.4809336,
0.5841551,
0.8514135,
0.9723865,
1.163051,
1.258054,
1.540105,
1.850756,
1.916502,
2
)
2009-10-01 13:11:00.888 Whose Line[56132:207] --> (
NSPoint: {437, 159},
NSPoint: {30, 0},
NSPoint: {446, 272},
NSPoint: {398, 21},
NSPoint: {469, 117},
NSPoint: {377, 111},
NSPoint: {84, 95},
NSPoint: {101, 111},
NSPoint: {291, 138},
NSPoint: {359, 237},
NSPoint: {7, -12}
)




The timings look right - the duration was set to 2 seconds... So they're all in order and randomly spaced.

The positions are also all inside the bounds (iPhone landscape, full screen @ 480x320)

However, when I add the animation, nothing happens...

Any ideas would be greatly appreciated!
_______________________________________________

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


  • Follow-Ups:
    • Re: CAKeyframeAnimation for 'position' not working
      • From: David Duncan <email@hidden>
  • Prev by Date: very newbie question
  • Next by Date: Re: CAKeyframeAnimation for 'position' not working
  • Previous by thread: Re: very newbie question
  • Next by thread: Re: CAKeyframeAnimation for 'position' not working
  • Index(es):
    • Date
    • Thread