UIImageView orqali UIButtonni bosish mumkinmi?

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]; 

Mening ilovam rasmning to'liq qismida tugmachani ko'rsatadi, lekin men uni bosish tasviriga javob bermaydi. Buning o'rniga, uni aniqlashda jurnalning chiqishi kabi bir martalik signal aniqlanadi. Lekin, bu tugmani tanlab olish va ba'zi funktsiyalarni bajarish uchun istayman.

Oldindan rahmat.

wahib

9

8 javoblar

Buni sinab ko'rdim va u men uchun ishlaydi .......

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];
5
qo'shib qo'ydi
Rasmlarni tugmachaga o'rnatsangiz, nima uchun UIImageView-ni ishlatasiz?
qo'shib qo'ydi muallif tcurdt, manba
  1. you have to add define button as custom.It will work for you.

    UIButton *Button = [UIButton buttonWithType:UIButtonTypeCustom];

3
qo'shib qo'ydi

Siz faqat bitta tugmani bosib foydalanishingiz mumkin:

[UIButton setImage:[imageNamed:@"image.png"]];
3
qo'shib qo'ydi
Sizning yechimingiz eng oqlangan ko'rinadi
qo'shib qo'ydi muallif nurxyz, manba

tugmani a ning quyi sinfiga qo'shishingiz kerak

scrollview

-ni tanlang

yo'q

tasavvur

.. tugma tugmacha ko'rinishining pastki klassi bo'lsa, uni bosib bo'lmaydi. tugmachani tanib olish uchun har bir tugma uchun maxsus tagni belgilang

Bu yordam beradi deb umid qilaman

1
qo'shib qo'ydi

UIButtonni UIImageVew-da ko'rsatish imkoniyatiga ega bo'laman va bu yangilash kodidir. Men yaqinda yuzaga kelgan muammolar haqida so'zlab berdim.

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

Hozirda ikkita variant bor. Mening tugmani ImageView yoki ScrollViewning pastki ko'rinishida bo'lishini xohlaysizmi. Scrollview tugmachasining subview ko'rinishini qilsam [self addSubView: btn]; u o'ng holatida ko'rsatiladi va tıklanabilir, lekin muammo, navbatdagi barcha rasmlarda yaratilganligi, chunki men uni yuqori ko'rinish va ixtiyoriy scrollview ko'rinishini yarataman. Boshqa ko'rinishga ega bo'lsam, i.e, har bir tasvir uchun yagona bo'lgan ImageView, lekin yana ham uning barcha rasmlarda ko'rsatiladi va uni tıklanamaz: /

Any1 lahzali tugmachada har bir tasvirning turli joylarida turli xil tugmachalarga ega bo'lishim uchun, men uni tıklanabilir qilish va dinamik tugma bilan rasm o'rtasidagi aloqani saqlab qolish uchun menga yo'l-yo'riq bera oladimi?

0
qo'shib qo'ydi

U tıklanabilir rasm bo'lsa, rasm ustida tugmani afzal qilaman.

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];
0
qo'shib qo'ydi
Sizning javobingiz uchun tashakkur. Yoki rasmning tugmachasi yoki yo'qmi, men uchun muhim emas. Men xohlaganim uni tıklanabilir qilish. Hozirda ikkita variant bor. Mening tugmani ImageView yoki ScrollViewning pastki ko'rinishida bo'lishini xohlaysizmi. Scrollview tugmachasining subview ko'rinishini qilsam [self addSubView: btn]; u o'ng holatida ko'rsatiladi va tıklanabilir, lekin muammo, navbatdagi barcha rasmlarda yaratilganligi, chunki men uni yuqori ko'rinish va ixtiyoriy scrollview ko'rinishini yarataman. Boshqa ko'rinishga ega bo'lsam, i.e, har bir tasvir uchun yagona bo'lgan ImageView, u tıklanamaz: /
qo'shib qo'ydi muallif Wahib Ul Haq, manba

Mening ilovalarimdan birida bunday qildim. Yuqori chizilgan tugmachalar bilan bir qator tasvirlarni o'z ichiga olgan xib faylini yaratdim. Men ularni sinf faylidagi tegishli savdo nuqtalariga uladim. Keyin touchesBegan-ni bosgan holda ishladim:

if(CGRectContainsPoint(btnFirstButton.frame, touchLocation)){
    //do something useful
}else if(CGRectContainsPoint(btnSecondButton.frame, touchLocation)){
    //do something useful
}

Umid qilamanki, men uchun :-)

0
qo'shib qo'ydi

yoqilgan tugmani foydalanuvchi interfeysi sozlamalarini o'rnatishga harakat qiling.

[btn setEnabled:YES];

Cosy UIImageView sukut o'chirilgan tugmachining foydalanuvchi interfeysi o'rnatadi.

Boshqacha qilib aytganda, buni sinab ko'rishingiz mumkin. Faqat oxirgi ikki qatorni o'zgartiring.

O'zgartirish:

[imageView addSubview:btn];
[self addSubview:imageView]; 

bu bilan:

[self addSubview:imageView];
[self addSubview:btn];

Tasavvurni birinchi va ikkinchi tugmani aytib o'tganim kabi qo'shing. Boshqacha qilib aytganda, tugma tasavvurning orqasida joylashtiriladi va yashirin ko'rinadi.

0
qo'shib qo'ydi