我正在使用CABasicAnimation移动图像,但我注意到它的触摸事件的rect仍保留在旧位置 - 如何更新rect以便它不会在错误的位置注册?
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"];
答案 0 :(得分:1)
在这种情况下,您需要移动TouchImageView
的{{1}}子类,而不是UIImageView
。后者是较低级别的绘图表面,但不处理触摸事件等。
从iPad: Move UIView with animation, then move it back开始,这里有一些改编的代码,可以像您一样动画self.layer
属性:
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];