I want to create a UIButton to display over an UIImageView. My code snippet is as shown below:
imageView = [[UIImageView alloc] initWithImage:image]; //initWithImage:image
imageView.userInteractionEnabled = YES; //to enable touch interaction with image
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTitle:@"ClickMe" forState:UIControlStateNormal];
[btn.titleLabel setFont:[UIFont systemFontOfSize:16]];
btn.frame = CGRectMake(340, 550, 70, 50);
[btn addTarget:self action:@selector(onClickHotSpotButton) forControlEvents:UIControlEventTouchUpInside]; //it wasnt working
[imageView addSubview:btn];
[self addSubview:imageView];
私のアプリケーションは、私がしたい画像の部分にボタンを正確に表示しますが、クリック画像には反応しません。その代わりに、私はその検出時に出力を記録するので、シングルタップを検出します。しかし、私はこのボタンをクリックして選択し、いくつかの機能を実行したい。
前もって感謝します。
ワヒブ
私はこれを試して、それは私のために働く.......
UIImageView * imageView = [[UIImageView alloc]init];
[imageView setImage:[UIImage imageNamed:@"image.jpeg"]];
[imageView setFrame:CGRectMake(10,10,300,300)];
[imageView setContentMode:UIViewContentModeScaleToFill];
[imageView setUserInteractionEnabled:TRUE];
UIButton * ButtonOnImageView = [[UIButton alloc]init];
[ButtonOnImageView setFrame:CGRectMake(135,120,60,30)];
[ButtonOnImageView setImage:[UIImage imageNamed:@"image.jpeg"] forState:UIControlStateHighlighted];
[ButtonOnImageView setImage:[UIImage imageNamed:@"image2.jpeg"] forState:UIControlStateNormal];
[ButtonOnImageView addTarget:self action:@selector(OnClickOfTheButton) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:ButtonOnImageView];
[self.view addSubview:imageView];
クリック可能なイメージの場合は、イメージオーバーボタンが好きです。
UIButton *sampleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[sampleButton setFrame:CGRectMake(kLeftMargin, 10, self.view.bounds.size.width - kLeftMargin - kRightMargin, 52)];
[sampleButton setTitle:@"Button Title" forState:UIControlStateNormal];
[sampleButton setFont:[UIFont boldSystemFontOfSize:20]];
[sampleButton setBackgroundImage:[[UIImage imageNamed:@"redButton.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
[sampleButton addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:sampleButton];
私は私のアプリの一つでこれをしました。私は一連の画像を含むxibファイルを作りました。各画像はボタンの上に描かれていました。それらをクラスファイルの対応するアウトレットに接続しました。私はそれを使用してtouchesBeganでクリックされました:
if(CGRectContainsPoint(btnFirstButton.frame, touchLocation)){
//do something useful
}else if(CGRectContainsPoint(btnSecondButton.frame, touchLocation)){
//do something useful
}
希望が助けてくれる - 私のために働いた:-)
UIImageVIewでUIButtonを表示することができました。これが更新コードです。最近の問題を一番下に述べました。
imageView = [[UIImageView alloc] initWithImage:image]; //initWithImage:image
//imageView.tag = i;//tag our images for later use when we place them in serial form
[imageView setContentMode:UIViewContentModeScaleToFill]; //though it has no impact
imageView.userInteractionEnabled = YES; //to enable touch interaction with image
[self addSubview:imageView];
UIButton *btn = [[UIButton buttonWithType:UIButtonTypeContactAdd] retain]; //Plus icon button
btn.frame = CGRectMake(215, 550, 100, 100);
[btn addTarget:self action:@selector(onClickHotSpotButton) forControlEvents:UIControlEventTouchUpInside]; //it wasnt working
**[self addSubview:btn]; //Buttons are clickable but shows button on all images**
//[imageView addSubview:btn]; //Buttons are not clickable and are shown on all images
私は今、二つの選択肢があります。私は自分のボタンをImageViewのサブビューにするのか、ImageViewの親であるScrollViewにするのか。もし私が[self addSubView:btn]のようにScrollviewのボタンのサブビューを作るなら、それは右の位置に表示され、クリック可能ですが、親ビューのサブビュー、つまりスクロールビューになっているため、キューのすべてのイメージに作成されるという問題があります。それ以外の場合、すべての画像で一意ですが、すべての画像に表示され、クリックできないImageViewという子ビューのサブビューにすると、
私はそれをクリック可能にし、動的ボタンと画像との間のリンクを維持する方法について、any1が私を導くことができるので、キュー内の各画像上の様々な位置に異なるボタンを持つことができます。
ボタンのユーザインタラクションを有効に設定してみてください。
[btn setEnabled:YES];
UIImageViewは、デフォルトで無効になっているボタンのユーザインタラクションを設定します。
それ以外の場合は試してみてください。最後の2行を変更してください。
置換:
[imageView addSubview:btn];
[self addSubview:imageView];
これとともに:
[self addSubview:imageView];
[self addSubview:btn];
私が言及したように、最初にイメージビューを追加し、ボタンを2番目に追加してください。そうでなければ、ボタンは画像ビューの背後に置かれ、隠されたように見えます。