ios - 带有 subview 的 UIPinchGestureRecognizer 问题

标签 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]; 那么一切都会正常...我可以不添加事件吗只有 subview ?

谢谢!

最佳答案

是的,你可以给 subview 添加手势。我测试了您的代码,如下所示,工作正常。

首先添加委托(delegate)。

@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");
}

你已经使用了捏合手势,所以你可以像下面这样使用它。

enter image description here

关于ios - 带有 subview 的 UIPinchGestureRecognizer 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53937804/

相关文章:

ios - 捕获手势识别器开始状态的 Swift 惯用方法

ios - 将 UIGestureRecognizer 添加到自定义 drawRect 或 UIBezierPath

ios - Swift 以编程方式启动 UILongPressGesture

iphone - 让 applicationWillTerminate 和 applicationDidEnterBackground 工作

objective-c - 使用OpenCV和iOS创建封闭区域时将任何图像转换为可着色

iphone - 继承 UIViewController 类和 XIB

ios - 如何在 Objective C 中使用 HSV 缩放颜色

iphone - 如何在不像素化的情况下调整(缩小)UIView 的大小?

ios - 需要有关 ALAssetsLibrary 枚举的帮助

iOS Xcode 连接到 MySQL 数据库