gpt4 book ai didi

iphone - UILongPressGestureRecognizer 不删除 View

转载 作者:行者123 更新时间:2023-11-28 22:24:23 25 4
gpt4 key购买 nike

我没看出这里有什么问题。当用户点击并按住时,我添加一个 View ,当触摸完成时, View 被删除。这不起作用,我确实看到正在发送 UIGestureRecognizerStateEnded。

但是,如果我在该状态之外调用 [tmpView removeFromSuperview];,它会毫无问题地被删除。

知道是什么原因造成的吗?

  -(void)longTapped:(UILongPressGestureRecognizer*)recognizer {
UIView *tmpView = [[UIView alloc] init];
tmpView.backgroundColor = [UIColor greenColor];

// Position the menu so it fits properly
tmpView.frame = CGRectMake(0, 100, 320, 250);

// Add the menu to our view and remove when touches have ended
if (recognizer.state == UIGestureRecognizerStateBegan) {
[self.view addSubview:tmpView];
}
else if(recognizer.state == UIGestureRecognizerStateEnded){
[tmpView removeFromSuperview];
}
}

最佳答案

第二次调用 -longTapped: 方法时,它在 tmpView 变量中实例化了一个 UIView 的新实例,并试图将其从其父 View 中删除。当长按开始时,您需要在 Controller 上存储对添加的 View 的引用,当长按结束时,您需要从其父 View 中删除该对象。

@interface myVC ()
@property (nonatomic, weak) UIView *tmpView;
@end

-(void)longTapped:(UILongPressGestureRecognizer*)recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) {
// Add the menu to our view and remove when touches have ended
self.tmpView = [[UIView alloc] init];
self.tmpView.backgroundColor = [UIColor greenColor];

// Position the menu so it fits properly
self.tmpView.frame = CGRectMake(0, 100, 320, 250);

[self.view addSubview:self.tmpView];
}
else if(recognizer.state == UIGestureRecognizerStateEnded){
[self.tmpView removeFromSuperview];
self.tmpView = nil;
}
}

关于iphone - UILongPressGestureRecognizer 不删除 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19389547/

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