1

正如这个 SO-answer中所解释的,我正在尝试UITapGestureRecognizerUIIMageView. 我的方法可以在下面找到。

的实例MenuButtonImageView是 的子视图homeScreenViewController.view

我也实施了touchesBegan:withEvent:touchesMoved:withEvent:而且touchesEnded:withEvent:工作正常。

问题:不知何故handleSingleFingeredTap:从未被调用。这可能是什么问题?

@interface MenuButtonImageView : UIImageView

@property (nonatomic, weak) IBOutlet HomescreenViewController *homeScreenViewController;
@property (nonatomic, readwrite) Visibility visibility;

- (void)handleSingleFingeredTap:(UITapGestureRecognizer *)recognizer;

@end

@implementation MenuButtonImageView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.visibility = HIDDEN;
        self.userInteractionEnabled = YES;
        UITapGestureRecognizer * singleFingeredTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleFingeredTap:)];
        singleFingeredTap.numberOfTapsRequired = 1;
        [self addGestureRecognizer:singleFingeredTap];
    }
    return self;
}

- (void)handleSingleFingeredTap:(UITapGestureRecognizer *)recognizer {
    NSLog(@"handleTap was called"); // This method is not called!
    [self toggleMainMenu];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { ... }
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { ... }
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { ... }

@end
4

1 回答 1

1

initWithFrame您在方法中添加识别器。检查您的视图是否以这种方式初始化。它可能是用另一个构造函数创建的initWithCoder,然后你的代码不会被执行。在方法中放置一个断点initWithFrame以找出答案。

您可以将代码放在单独的方法中,并从两个构造函数中调用它。这取决于你如何使用它。当initWithFrame您从代码创建视图时,使用非常普遍。

于 2012-08-30T17:21:52.307 回答