UIImageViewやUILabelのtag(タグ)の付け方

UIImageViewでタッチイベントをとりたかったので、tagをつけることにしました。
ただタグをつけるだけでは、UIImageViewのタッチイベントが取れません。
「User Interaction Enabled」にチェックを入れる必要があります。
これでタッチイベントが取れるようになります。

- (void) touchesEnded:(NSSet *) touches withEvent: (UIEvent *) event
{
	UITouch * touch = [[event allTouches] anyObject];
	if (touch.view.tag ==self.imageView.tag) {
		NSLog(@"Tap.");
	}
}

ソースで書くと以下のようになります。

self.imageView.userInteractionEnabled = YES;
self.imageLogo.tag = 100;

参考サイト:

http://sbkro.hatenablog.jp/entry/2012/11/27/233945

コメントを残す