ios - 带有子视图的 UIPinchGestureRecognizer 问题
问题描述
我有以下代码:
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];
那么一切都会正常工作......我不能只为子视图添加事件吗?
谢谢!
解决方案
是的,您可以向子视图添加手势。我测试了你的代码,如下所示。
首先添加委托。
@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,所以你可以像下面这样使用它。
推荐阅读
- javascript - Deno 有办法跟踪异步上下文吗?
- python - 将虚拟环境保存在 Anaconda 中并与其他同事共享
- python - 向递归斐波那契程序添加计数器
- html - highcharts datetime 类型在 bootstrap 4 列中不起作用
- django - 具有多个条件的 order_by 导致不同的行为不端
- firebase - 使用 collectionGroup 更新集合中所有文档的 HTTP 函数
- javascript - 重命名对象键
- java - 如何从字符串中删除符号并将它们放回去?
- node.js - 如何让 docker 下运行的 nodejs 服务器通过 mongoose 连接到 mongodb?
- sql - 通过 SQL 在 Access 数据库上批量运行 UPDATE 语句的方法