UILongPressGestureRecognizer พร้อมปุ่มต่างๆ

ฉันมี 2 ปุ่มที่มี "UILongPressGestureRecognizer" ต้องทำ:

ปุ่มโฟกัส 1:

-(IBAction)seleccionar46:(id)sender{
UILongPressGestureRecognizer *longpressGesture =[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandler:)];
longpressGesture.minimumPressDuration = 3;
[longpressGesture setDelegate:self];
[self.button1 addGestureRecognizer:longpressGesture];}

สำหรับปุ่ม 2:

    -(IBAction)seleccionar46:(id)sender{
UILongPressGestureRecognizer *longpressGesture =[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandler:)];
longpressGesture.minimumPressDuration = 3;
[longpressGesture setDelegate:self];
[self.button2 addGestureRecognizer:longpressGesture];}

และใน "longpressGesture" ฉันต้องการแยกความแตกต่างระหว่างปุ่ม 1 และปุ่ม 2 แต่ทำไม่ได้...

 - (void)longPressHandler:(UILongPressGestureRecognizer *)gestureRecognizer{//Here i need do the differentation}

ขอบคุณสำหรับทุกอย่าง!

ขอแสดงความนับถืออย่างสูง.


person Alberto Juarez    schedule 13.02.2012    source แหล่งที่มา


คำตอบ (1)


สิ่งที่คุณสามารถทำได้คือใช้คุณสมบัติ view ของ UIGestureRecognizer ดังนั้น หากคุณบันทึกการอ้างอิงไปยังปุ่มทั้งสองของคุณ คุณสามารถตรวจสอบความเท่าเทียมกันได้

ดังนั้นถ้าคุณมี

@interface blah
{
  UIButton *buttonOne;
  UIButton *buttonTwo;
}

จากนั้นคุณเพิ่มตัวจดจำให้กับปุ่มที่คุณสามารถทำได้ในตัวจัดการ

if(gestureRecognizer.view==buttonOne)
{
   //do stuff for button one
}
else if(gestureRecognizer.view==buttonTwo)
{
  //do stuff for button two
}

หวังว่ามันจะช่วยได้

person Daniel    schedule 13.02.2012
comment
ขอบคุณเพื่อน!ฉันต้องการบันทึกชื่อของปุ่มใน NSString ... ฉันจะทำอย่างไร? - person Alberto Juarez; 13.02.2012
comment
เพราะชื่อปุ่มเหมือนกับชื่อภาพหนึ่ง...และพอกดปุ่มไป 3 วินาทีก็แสดงภาพที่เกี่ยวข้องกับปุ่มนั้น...หวังว่าจะเข้าใจนะครับ...ขอบคุณทุกท่านครับ แดเนียล - person Alberto Juarez; 13.02.2012
comment
คุณควรเก็บพจนานุกรมหรืออะไรสักอย่างไว้เพื่อติดตามสิ่งนั้น หรือฮาร์ดโค้ดมันหากมีเพียงภาพเดียวต่อปุ่มและภาพคงที่ - person Daniel; 14.02.2012
comment
มันเป็นเพียงภาพเดียวต่อปุ่ม แต่ฉันไม่เข้าใจฮาร์ดคอร์... คุณช่วยยกตัวอย่างให้ฉันหน่อยได้ไหม? ขอบคุณแดเนียล - person Alberto Juarez; 14.02.2012