0

我正在使用 CABasicAnimation 移动图像,但我注意到它的触摸事件矩形保持在其旧位置 - 如何更新矩形使其不会注册在错误的位置?

CABasicAnimation *theAnimation; 
theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
theAnimation.duration=0.3;
theAnimation.repeatCount=0;
theAnimation.fillMode = kCAFillModeForwards;
theAnimation.removedOnCompletion = NO;
theAnimation.autoreverses=NO;
theAnimation.fromValue=[NSNumber numberWithFloat:0];
theAnimation.toValue=[NSNumber numberWithFloat:-rect.size.width*0.9];
[self.layer addAnimation:theAnimation forKey:@"animateLayer"];
4

1 回答 1

1

在这种情况下,您需要移动, 而不是的TouchImageView子类。后者是一个较低级别的绘图表面,但不是处理触摸事件等的。UIImageViewself.layer

iPad:Move UIView with animation, then move it back,这里有一些改编的代码可以translation像你一样为属性设置动画:

CGFloat moveX = -rect.size.width*0.9;
CGFloat moveY = 0;

[UIView beginAnimations: @"Move View" context:nil];
[UIView setAnimationDuration: 0.3 ];
self.transform = CGAffineTransformMakeTranslation(moveX,moveY);
[UIView commitAnimations];

但是,您应该能够通过更改视图的框架来简化这一点:

CGRect newFrame = self.frame;
newFrame.x -=rect.size.width*0.9;

[UIView beginAnimations: @"Move View" context:nil];
[UIView setAnimationDuration: 0.3 ];
self.frame = newFrame;
[UIView commitAnimations];
于 2012-07-05T11:26:19.557 回答