简体   繁体   中英

Animating UIView - How to keep track of Position?

i animate (move) a subclassed UIImageView like this:

[UIView beginAnimations:nil context:NULL];  
[UIView setAnimationDuration: 1];  
[UIView setAnimationDelay:0];
[UIView setAnimationCurve: UIViewAnimationCurveEaseOut];  
[UIView setAnimationBeginsFromCurrentState:YES]; 
if (position == 0) position = 1;
if (position > 6) position--;    
self.transform = CGAffineTransformMakeTranslation(position*120, 0);
[UIView commitAnimations];

then, i want to highlight it by let it pulsate:

CAKeyframeAnimation *bounce = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
CATransform3D forward = CATransform3DMakeScale(1.3, 1.3, 1);
CATransform3D back = CATransform3DMakeScale(0.7, 0.7, 1);
CATransform3D forward2 = CATransform3DMakeScale(1.2, 1.2, 1);
CATransform3D back2 = CATransform3DMakeScale(0.9, 0.9, 1);
[bounce setValues:[NSArray arrayWithObjects:
                   [NSValue valueWithCATransform3D:CATransform3DIdentity],
                   [NSValue valueWithCATransform3D:forward],
                   [NSValue valueWithCATransform3D:back],
                   [NSValue valueWithCATransform3D:forward2],
                   [NSValue valueWithCATransform3D:back2],
                   [NSValue valueWithCATransform3D:CATransform3DIdentity],
                   nil]];
[bounce setDuration:0.6];
[[super layer] addAnimation:bounce forKey:@"bounceanimation"];

That all works. It's get moved by 120 Pixel but when a call the pulsate code it will pulsate on its original position, not where i've just moved it. why is that and how can i change it?

tobias

Okay, here's your problem:

self.transform = CGAffineTransformMakeTranslation(position*120, 0);

Notice that you are translating the view to another spot, but like any transform, the original stats of the object stay the same.

All you have to do is translate that code into a setFrame: method so that it actually moves the image.

Dont worry! just add this

   bounce.fillMode = kCAFillModeForwards;

It will make you your animation stay at end point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM