MENU
コンテンツ再構築中

Objective-C:UIImageViewのタッチを可能にする

UIImageViewをタッチしたときに何か処理をさせたい場合のメモ。

INDEX

実装の方法

UIImageView の.userInteractionEnabled プロパティを使用する(UIViewはデフォルトでYES)
メソッドのなかにタッチされたビューを判定しその中に処理を記述する。

[code]
myView.userInteractionEnabled = YES;

//タッチ開始時に呼び出される
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//タッチされたUIImageViewの判定
if([event touchesForView:myView] != NULL)
{
//タッチ認識時の処理
}
}
[/code]

UIViewController や UIView の直接のサブクラスでタッチイベントを実装するときは、以下のメソッドもオーバーライドする必要がある。処理がない場合は空で構わない。

[code]
//タッチ維持のまま指を動かしたときに呼び出される
– (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@”touchesMoved:”);
}
[/code]
[code]
//タッチした指が離れたときに呼び出される
– (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@”touchesEnded:”);
}
[/code]

[code]
//タッチ処理が中断されたときに呼び出される
– (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@”touchesCancelled:”);
}
[/code]

関連する項目

Please share it!
  • URLをコピーしました!
  • URLをコピーしました!
INDEX