Objective c UIView TouchesBegind不';在动画中没有响应
我有一个继承UIImageView的可拖动类。当视图未设置动画时,拖动效果良好。但当设置动画时,它不会对触摸做出反应。动画完成后,触摸再次生效。但我需要它在触摸时暂停动画,并在触摸结束时恢复。 我花了一整天的时间研究它,但还是找不出原因 这是我的动画代码Objective c UIView TouchesBegind不';在动画中没有响应,objective-c,uiview,uiviewanimation,Objective C,Uiview,Uiviewanimation,我有一个继承UIImageView的可拖动类。当视图未设置动画时,拖动效果良好。但当设置动画时,它不会对触摸做出反应。动画完成后,触摸再次生效。但我需要它在触摸时暂停动画,并在触摸结束时恢复。 我花了一整天的时间研究它,但还是找不出原因 这是我的动画代码 [UIView animateWithDuration:5.0f delay:0 options:(UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserI
[UIView animateWithDuration:5.0f
delay:0
options:(UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction)
animations:^{
self.center = CGPointMake(160,240);
self.transform = CGAffineTransformIdentity;
}
completion:nil
];
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
NSLog(@"touch");
CGPoint pt = [[touches anyObject] locationInView:self];
startLocation = pt;
[self.layer removeAllAnimations];
[[self superview] bringSubviewToFront:self];
}
这是因为当动画开始时,ios会将动画视图放置到目标位置,但会在路径上绘制。因此,如果在移动时点击视图,实际上是点击其帧外的某个位置 在动画视图的init中,将userInteractionEnabled设置为NO。这样触摸事件由superview处理
self.userInteractionEnabled = NO;
在superview的TouchesBegind方法中,检查动画视图的presentationLayer位置。如果它们与触摸位置匹配,则将ToucheSBegind消息重定向到该视图
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint point = [[touches anyObject] locationInView:self.view];
CGPoint presentationPosition = [[animatingView.layer presentationLayer] position];
if (point.x > presentationPosition.x - 10 && point.x < presentationPosition.x + 10
&& point.y > presentationPosition.y - 10 && point.y < presentationPosition.y + 10) {
[animatingView touchesBegan:touches withEvent:event];
}
}
-(void)touchesbeated:(NSSet*)toucheevent:(UIEvent*)event{
CGPoint point=[[toucheanyobject]locationInView:self.view];
CGPoint presentationPosition=[[animatingView.layer presentationLayer]位置];
如果(点x>presentationPosition.x-10和点xpresentationPosition.y-10和点.y
顺便说一句,即使位置没有设置动画,我也会遇到问题,但另一个属性(frame
在我的例子中)是。。。即使视图的hitTest
和pointInside
方法被调用并正确返回,您仍然会得到被吞没的点击。另外,我只是在做如果([view pointInside:pointwithevent:event])
——不需要10点的slop是一个可能适合你的答案。