ios – UIView对象中的UIGestureRecognizer

我创建了一个UIViewController,它包含一个UIView对象.

我希望UIView对象响应1手指单击,并且还用2个手指捏住.

我点击了xib中的“User Interaction Enabled”和“Multiple touch”选项.

我在UIView函数initWithFrame:中创建了UITapGestureRecognizer和UIPinchGestureRecognizer,因为它是UIView对象的唯一入口点.但该对象不响应UIGestureRecognizer对象.为什么?或者,放置UIGestureRecognizer对象的最佳位置在哪里?

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        // single tap
        UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];

        singleTap.numberOfTapsrequired = 1;
        singleTap.numberOftouchesrequired = 1;
        [self addGestureRecognizer: singleTap];

        // 2 fingers pinch
        UIPinchGestureRecognizer* doubleMove = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleMove:)];

        [self addGestureRecognizer: doubleMove];
    }
    return self;
}

解决方法

您的问题是,当您在IB中实例化自定义视图子类时,会调用initWithCoder :.如果您使用initWithFrame以编程方式完成它:它可以正常工作.

有两种方法可以解决这个问题,

>将手势设置移动到另一个方法,并在initWithFrame:和initWithCoder中调用它们:如果您打算重用此组件并在手势上公开某种回调,我建议您这样做.>如果要实现此操作并与控制器元素进行大量交互,请在viewDidLoad中添加它们.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...