我在应用程序中有一个圆形视图,并且在该圆形视图中移动了一些对象(例如图形),在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;
}

自昨晚以来,我一直被困在这里,我们将不胜感激。

提前致谢。

最佳答案

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

但是NSTimer可以帮助我们

但是我觉得我们可以通过在调用NSTimer时添加设置/替换touchesMoved实例来解决此问题,并且可以在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
}

关于ios - touchesEnded和touchesCancelled有时在触摸后都未调用已移动,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38495903/

10-10 23:45