ios – UITouch不释放视图

我有一个未被解除分配的自定义视图.我按下关闭按钮时关闭控制器.现在如果我只按下按钮,视图就会被解除分配.但是,如果用一根手指按下按钮,其他手指触摸视图,则在解除时不会取消分配,而是在下一次触摸事件中.

它的UITouch保留了我的视图参考而不是释放它.我怎样才能解决这个问题?

这是我的近距离行动的代码:

- (IBAction)closePressed:(UIButton *)sender {
    NSLog(@"Close pressed"); 
    if (self.loader)
    [self.loader cancelJsonLoading];
    [self.plView quit];
    [self dismissViewControllerAnimated:YES completion:nil];
}

最佳答案 你试着打电话:

[self.view resignFirstResponder];

这应取消所有待处理的UITouches.

如果这不起作用,您可以跟踪您的触摸:

>定义存储当前触摸的NSMutableSet:

NSMutableSet * _currentTouches;
>在你的init()中:

_currentTouches = [[NSMutableSet alloc] init];

并实施:

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super.touchesBegan:touches withEvent:event];
    [_currentTouches unionSet:touches]; // record new touches
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super.touchesEnded:touches withEvent:event];
    [_currentTouches minusSet:touches]; // remove ended touches
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super.touchesEnded:touches withEvent:event];
    [_currentTouches minusSet:touches]; // remove cancelled touches
}

然后,当您需要清理触摸时(例如,当您释放视图时):

- (void)cleanCurrentTouches {
    self touchesCancelled:_currentTouches withEvent:nil];
    _currentTouchesremoveAllObjects];
}

我认为,它有点hacky,但是医生说:

When an object receives a touchesCancelled:withEvent: message it
should clean up any state information that was established in its
touchesBegan:withEvent: implementation.

点赞