【问题标题】:Neither touchesEnded nor touchesCancelled not called sometimes after touchesMovedtouchesEnded 和 touchesCancelled 都不会在 touchesMoved 之后调用
【发布时间】:2016-07-21 05:53:38
【问题描述】:

我的应用程序中有一个圆形视图,我正在移动该圆形视图中的一些对象,例如图形,它在 90% 的情况下工作正常,但在某些情况下它不调用 TouchEnded,我的重置图形代码在 TouchEnded方法,所以它在下面某个时候卸载是我的触摸委托方法的代码。

#pragma mark - Touch Events For Rotation of GRAPH

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch* touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view];

    prevAngle = [Utilities angleBetweenPoint:touchPoint toPoint:self.view.center];
/// convert negative angle into positive angle
    if(prevAngle < 0){
        prevAngle = PI_DOUBLE + prevAngle;
    }

    //
    [_viewStaticRadialPart hideToolTip];
}
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
    CGFloat diffAngle, tempCurAngle, tempPrevAngle, curTransformAngle, newTransformAngle;

    NSLog(@"touchesMoved");
// Get the only touch (multipleTouchEnabled is NO)
    UITouch* touch = [touches anyObject];
    // Track the touch
    CGPoint touchPoint = [touch locationInView:self.view];


    curAngle = [Utilities angleBetweenPoint:touchPoint toPoint:self.view.center];

    /// convert negative angle into positive angle
    if(curAngle < 0){
        curAngle = PI_DOUBLE + curAngle;
    }
        prevAngle = curAngle;
}

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self resetViewsOnceRotationStops];


}
- (void)touchesCancelled:(nullable NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event
{
    [resetViewsAfterTouchStops invalidate];
    resetViewsAfterTouchStops = nil;
}

我从昨晚开始就被困在这里,所以我们将不胜感激。

提前致谢。

【问题讨论】:

  • 您尝试在 touchesEnded 方法中添加调试点?
  • 是的,我尝试调试,它从不调用 TouchEnded,只有最后一个方法调用是 TouchesMoved :(

标签: ios objective-c cocoa-touch touches


【解决方案1】:

我从核心层面不知道这个问题的原因。

但是NSTimer可以帮助我们

但我觉得我们可以通过在调用touchesMoved 时添加设置/替换NSTimer 实例来解决这个问题,我们可以在touchesEndedtouchesCancelled 上重置该计时器。因此,无论哪种情况,您的 touchesEndedtouchesCancelled 未调用计时器都会完成这项工作,并且您的重置逻辑按预期工作。

演示源代码

- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
    NSLog(@"touchesMoved");

    //Timer re-initialize code here
    if (resetViewsAfterTouchStops) {

         [resetViewsAfterTouchStops invalidate];
         resetViewsAfterTouchStops = nil;
    }

    resetViewsAfterTouchStops =  [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(resetViewsOnceRotationStops) userInfo:nil repeats:NO];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    isTouched = NO;
    [resetViewsAfterTouchStops invalidate];
    resetViewsAfterTouchStops = nil;

    [self resetViewsOnceRotationStops];
    [self setUserInteractionOnSectorsAs:YES];
}

- (void)touchesCancelled:(nullable NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event
{
    NSLog(@"touchesCancelled");
    if(resetViewsAfterTouchStops)
    {
         [self resetViewsOnceRotationStops];
         [self setUserInteractionOnSectorsAs:YES];
    }

    [resetViewsAfterTouchStops invalidate];
    resetViewsAfterTouchStops = nil;
}

- (void) resetViewsOnceRotationStops
{
    //your reset code will be place here
}

【讨论】:

  • amorbytes,感谢您的回答,但在这里我有一个关于内存管理的查询,如果我在屏幕上多次触摸,任何计时器将如何生成,因为我的屏幕正在使用非常高的内存和 CPU,所以这个解决方案可以在没有任何内存问题或滞后的情况下工作吗?
  • @Dhanesh:这是个好问题,但我正在重新初始化计时器,所以前一个计时器将变为nil,所以我觉得它不会对内存造成太大影响。
【解决方案2】:

如果您正在使用任何对象,请尝试删除该视图上的 UIGestureRecognizer 对象。 UIGestureRecognizer 对象会干扰触摸事件的生命周期。

【讨论】:

  • 是的@Shaggy,我也尝试过不使用 UIGestureRecognizer 但仍然没有成功。
  • 该视图是否嵌入在 UIScrollView 中?
  • 不,它只是带有多个子层的普通 UIView。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-10
  • 2023-03-19
  • 1970-01-01
  • 1970-01-01
  • 2013-01-29
  • 1970-01-01
  • 2023-04-06
相关资源
最近更新 更多