2012-03-30 84 views
2

我动画(移动)一个子类的UIImageView像这样:动画UIView - 如何跟踪位置?

[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]; 

然后,我想让它脉动以突出显示它:

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"]; 

这所有的作品。它被120像素移动,但是当打电话给脉冲码时,它会在原来的位置上跳动,而不是我刚才移动的位置。为什么是这样的,我该如何改变它?

tobias

回答

1

好,这是你的问题:

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

请注意,您是翻译视图到另一个地方,但像任何变换,对象的原始统计数据保持不变。

您只需将该代码转换为setFrame:方法,以便实际移动图像。

+0

终于明白了!谢谢!把它改成[self setFrame:CGRectMake(30 + position * 120,self.frame.origin.y,self.frame.size.width,self.frame.size.height)]; – 2012-03-30 18:19:08

+0

没问题!乐于帮助! – David 2012-03-30 22:06:49

0

别担心!只需加上这个

bounce.fillMode = kCAFillModeForwards; 

它会让你的动画停留在终点。