-4
- (void)viewDidLoad
{
    [super viewDidLoad];
    CGRect imageRect = (CGRect){105, 180, 110, 110};
    UIImage *image = [UIImage imageNamed: @"sasuke.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:imageRect];
    [imageView setImage: image];
    UILongPressGestureRecognizer *longGnizer=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longGo:)];
    imageView.userInteractionEnabled = YES;
    [imageView addGestureRecognizer:longGnizer];
    [self.view addSubview:imageView];
}

- (void)longGo:(UILongPressGestureRecognizer *)aGer{
    if(aGer.state==UIGestureRecognizerStateBegan) {
       NSLog(@"%s",__func__);
    }
}

这是代码,但是 func longGo 从来没有工作过,为什么?当我使用 [self.view addGestureRecognizer:longGnizer] 时,它确实有效。

4

1 回答 1

7

UIImageViews 已默认userInteractionEnabled设置为NO。你必须设置imageView.userInteractionEnabled = YES;.

于 2013-11-02T02:29:49.013 回答