首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UITouch未发布视图

UITouch未发布视图
EN

Stack Overflow用户
提问于 2016-10-17 21:30:39
回答 1查看 91关注 0票数 4

我有一个没有被释放的自定义视图。按下关闭按钮后,我关闭控制器。现在,如果我只按下按钮,视图就会被释放。但是如果用一根手指按下按钮,另一根手指接触视图,它不会在解除时释放,而是在下一次触摸事件时释放。

它的UITouch保留了我视图的引用,而不是释放它。我该如何解决这个问题呢?

下面是我的关闭操作的代码:

代码语言:javascript
复制
- (IBAction)closePressed:(UIButton *)sender {
    NSLog(@"Close pressed"); 
    if (self.loader)
    [self.loader cancelJsonLoading];
    [self.plView quit];
    [self dismissViewControllerAnimated:YES completion:nil];
}
EN

回答 1

Stack Overflow用户

发布于 2016-10-17 22:14:33

你有没有试着打电话给:

代码语言:javascript
复制
[self.view resignFirstResponder];

这应该会取消所有挂起的UITouches。

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

  • 定义一个用来存储当前触摸的NSMutableSet:

NSMutableSet *_currentTouches;

  • in你的init():

_currentTouches = [[NSMutableSet alloc] init];

并实现:

代码语言:javascript
复制
- (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
}

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

代码语言:javascript
复制
- (void)cleanCurrentTouches {
    self touchesCancelled:_currentTouches withEvent:nil];
    _currentTouchesremoveAllObjects];
}

我觉得有点老生常谈,但医生说:

当一个对象接收到一个触摸时,它应该清除在它的触摸中建立的任何状态信息clean :withEvent:implementation。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40087761

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档