6

私はちょうど iPhone の開発について述べているところですが、私がやりたいことを探している答えを見つけることができないようです。

プログラムで UIImageView を作成し、タッチ機能のイベント ハンドラーを設定できるように思えます。

C#では、次のようなものがあります

ボタン b = new Button(); b.Click+= ハンドラー コード

今私はこれを持っています

CGRect myImageRect = CGRectMake(0.0f, 0.0f, 141.0f, 151.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];

myImage.userInteractionEnabled = YES;
[myImage setImage:[UIImage imageNamed:@"myImage.png"]];
myImage.opaque = YES; // explicitly opaque for performance
[self.view addSubview:myImage];
[myImage release];

タッチ イベントをオーバーライドするにはどうすればよいですか?

ありがとう

4

7 に答える 7

9

あなたの質問に対する完全な答えではありませんが、「見えないボタンの回避策」を実行するよりも、次の解決策の方が良いと思います..単純に UIImageView からサブクラス化し、次のメソッドをオーバーライドします。

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

 //your UIImageView has been touched :)

 //event -> "A UIEvent object representing the event to which the touches belong."

 //touches -> "A set of UITouch instances in the event represented by event that    represent the touches in the UITouchPhaseEnded phase."

}

お役に立てれば...

于 2010-03-16T08:24:47.167 に答える
3

画像を背景画像としてカスタム UIButton を使用するだけです。

于 2010-09-29T19:01:09.740 に答える
0

UIImageView の下に非表示のボタンを配置し、画像ビューを に設定しuserInteractionEnabled: NO、タッチをボタンに渡すことを提案する他の回答を見てきました。

于 2010-03-11T19:44:07.767 に答える
0

UIImageView とは何の関係もありません。タイプ custom で UIButton を使用し、アクション内にタッチアップを追加できます... set button.imageview setImage: すなわち:

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0,0,100,100); //edit for you own frame
UIImage *img = [UIImage imageNamed:@"myimage.png"];
[button setImage:img forState:UIControlStateNormal];

UIImageView を使いたい場合は、イメージビューにジェスチャーを追加する必要があります。それ以外の場合は、UIImageView をサブクラス化し、タッチ イベントを使用できます。

于 2012-09-27T11:09:53.617 に答える
0

UIImageView または UIView に onClick イベントを追加する最も簡単な方法は、それにタップ ジェスチャ レコグナイザーを追加することです。

UIImage *myImage = [UIImage imageNamed:@"myImage.png"];
UIImageView *myImageView = [[UIImageView alloc] initWithImage: myImage];

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action: @selector(onImageViewClicked)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
[self.myImageView addGestureRecognizer:singleTap];
[self.myImageView setUserInteractionEnabled:YES];
于 2013-08-27T07:29:01.497 に答える
0

で直接行う方法が見つかりませんでしたUIImageView。ただし、それをサブクラス化し、nextResponderメソッドをオーバーライドして、制御下にあるデリゲートを返すと、目的のことができます。デリゲートをサブクラスのプロパティとして公開すると、タッチ イベントのデリゲートを渡していつでも変更できます。

于 2010-03-11T18:05:32.023 に答える