图像视图的"touch area rect"在被动画处理以移动到其他位置后仍然存在



我正在使用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"];

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

从iPad:使用动画移动UIView,然后将其移回,这里有一些改编的代码,可以像以前一样对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];

最新更新