UIPinchGestureRecognizer问题与子视图

时间:2018-12-26 22:26:47

标签: ios objective-c uigesturerecognizer

我有以下代码:

m_singleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, nWidth - 1, nHeight - 1)];
m_singleView.backgroundColor = [UIColor clearColor];
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
[pinchGesture setCancelsTouchesInView:YES];
[m_singleView addGestureRecognizer:pinchGesture];
[m_singleView setUserInteractionEnabled:YES];
[m_MainView addSubview:m_singleView];

我遇到的问题是由于某种原因捏事件没有触发。但是,如果我将行从[m_singleView addGestureRecognizer:pinchGesture]更改为[m_MainView addGestureRecognizer:pinchGesture];,那么一切都会正常进行...我不能仅将事件添加到子视图吗?

谢谢!

2 个答案:

答案 0 :(得分:2)

是的,您可以向子视图添加手势。我测试了您的代码,如下所示。

首先添加代表。

@interface ViewController : UIViewController<UIGestureRecognizerDelegate>

- (void)viewDidLoad {
    [super viewDidLoad];
   
   UIView *m_singleView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, self.view.frame.size.width - 50, self.view.frame.size.height - 50)];
    self.view.backgroundColor=[UIColor greenColor];
    m_singleView.backgroundColor = [UIColor redColor];
    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch)];
    pinchGesture.delegate=self;
    [pinchGesture setCancelsTouchesInView:YES];
    [m_singleView addGestureRecognizer:pinchGesture];
    [m_singleView setUserInteractionEnabled:YES];
    [self.view addSubview:m_singleView];
}

-(void)pinch{
    NSLog(@"In PInch");
}

您已经使用过pinchgesture,因此可以按以下方式使用它。

enter image description here

答案 1 :(得分:0)

使用Debug视图层次结构查看m_singleView的大小是否为0?如果是这样,则需要更改m_singleView的大小,直到您可以触摸它为止。