在iOS上,有没有办法将方法addTarget添加到UIImageView?

Jer*_*y L 3 iphone uikit uiview uiimageview ios

我刚刚发现一个UIButton对象有一个addTarget处理UI事件的方法,但是一个UIImageView对象没有这样的方法,所以我们不能在代码中执行它,也不能使用Interface Builder为UIImageView对象添加这样的Action .手势识别器可以使用,但有一个简单的方法来补充addTarget,以UIImageView使我们的代码是不是部分由手势识别处理,并部分由处理addTarget方法?

Sen*_*ior 7

添加一个调用按钮调用的相同选择器的手势识别器应该没有太大的麻烦:

UIButton *button = [[UIButton alloc] init];
[button addTarget:self action:@selector(tapped:) forControlEvents:UIControlEventTouchUpInside];

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[[self view] addGestureRecognizer:tapRecognizer];


- (void)tapped:(id)sender {}
Run Code Online (Sandbox Code Playgroud)