0

不知道为什么这不能从父视图中删除添加的子视图。如果长按操作结束,我希望将其删除。我用 NSLog 进行了测试,我知道手势正确注册为结束,但它没有删除子视图。

-(void)addSubview:(UILongPressGestureRecognizer *)gesture {
    NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"overlayView" owner:self     options:nil];
    UIView *subView = [subviewArray objectAtIndex:0];

    if (gesture.state == UIGestureRecognizerStateBegan) {
        [self.view addSubview:subView];
        }
    if (gesture.state == UIGestureRecognizerStateEnded) {
        [subView removeFromSuperview];
        }    
}

所需功能:

长按开始:添加子视图。
长按结束:删除子视图。

4

1 回答 1

5

我希望你能通过在 .h 文件中声明 UIView *subview 然后在 viewDidLoad 中放入以下行来解决这个问题:

 NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"overlayView" owner:self     options:nil];
    UIView *subView = [subviewArray objectAtIndex:0];

接着:

-(void)addSubview:(UILongPressGestureRecognizer *)gesture {
    if (gesture.state == UIGestureRecognizerStateBegan) {
        [self.view addSubview:subView];
        }
    if (gesture.state == UIGestureRecognizerStateEnded) {
        [subView removeFromSuperview];
        }    
}
于 2013-10-07T09:29:52.160 回答