gpt4 book ai didi

ios - UITouch 不释放 View

转载 作者:可可西里 更新时间:2023-11-01 06:02:28 29 4
gpt4 key购买 nike

我有一个未被释放的自定义 View 。我在按下关闭按钮时关闭 Controller 。现在,如果我只按下按钮, View 就会被解除分配。但是,如果用一根手指按下按钮,另一根手指触摸 View ,则它不会在关闭时释放,而是在下一次触摸事件时释放。

它的 UITouch 保留我的 View 的引用而不释放它。我怎样才能解决这个问题?

这是我关闭操作的代码:

- (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
}

然后,当您需要清除触摸时(例如,当您释放 View 时):

- (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.

关于ios - UITouch 不释放 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40087761/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com