简体   繁体   English

不同类别的手势识别器?

[英]Gesture recogniser in different class?

I was wondering if this was possible. 我想知道这是否可能。 To have a class which adds new imageViews to the main view and assigns a gesture recogniser to it. 具有一个将新的imageViews添加到主视图并为其分配手势识别器的类。

So in my view builder class, I have the following: 因此,在我的视图构建器类中,我具有以下内容:

UIImageView *headerPlusIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"plusIcon.png"]];
headerPlusIcon.frame = CGRectMake(header.frame.size.width - 2.5*(logoSize - 8), yPosition*1.6, logoSize*0.9, logoSize*0.9);
headerPlusIcon.userInteractionEnabled = YES;

UIGestureRecognizer *headerTapGesture = [[UIGestureRecognizer alloc] initWithTarget:mainView action:@selector(testTapGesture:)];
[headerPlusIcon addGestureRecognizer:headerTapGesture];

The tap gesture method goes like this: 点击手势方法如下所示:

-(void)testTapGesture:(UITapGestureRecognizer *)gesture
{
    dispatch_async(dispatch_get_main_queue(), ^{
        mainView.backgroundColor = [UIColor redColor];
    });
}

mainView is passed into this class via the constructor and is simply the main view. mainView通过构造函数传递给此类,并且仅仅是主视图。

This is called like this: 这样称呼:

mainViewbuilder = [[MainViewBuilder alloc] initWithBaseView:self.view];
[mainViewbuilder buildHeader];

Unfortunately the tap gesture method never gets called... how would this be done properly? 不幸的是,点击手势方法从未被调用过……如何正确完成呢? Thanks! 谢谢!

Try UITapGestureRecognizer 尝试使用UITapGestureRecognizer

Apple Docs on UITapGestureRecognizer UITapGestureRecognizer上的Apple文档

The code you posted never adds your headerPlusIcon to the view hierarchy. 您发布的代码永远不会将headerPlusIcon添加到视图层次结构中。 You say in your answer to Adam that you got it working, but I don't see how if you don't add the headerPlusIcon to your view controller's content view hierarchy somewhere. 您在对Adam的答复中说,它可以正常工作,但是我不知道如何不将headerPlusIcon添加到视图控制器的内容视图层次结构中。

Note that mucking around in a view controller's view hierarchy from outside is not good object-oriented design. 请注意,从外部混入视图控制器的视图层次结构不是一个好的面向对象设计。

It would be better to have the view controller ask another object for a view (probably through a protocol) and then add that view itself. 最好让视图控制器向另一个对象请求一个视图(可能通过协议),然后再添加该视图本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM