[英]TapGesture recognizer on multiple UIImageView not working
多個UIImageView上的TapGesture識別器無法正常工作,同時它檢測到最后添加的圖像視圖手勢..我已經這樣做了,
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myFunction:)];
tapped.numberOfTapsRequired = 1;
tapped.delegate = self;
UIImageView *sample_book1= [[UIImageView alloc]initWithFrame:CGRectMake(70, 135, 100,125) ];
sample_book1.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"mathematics.png"]];
sample_book1.userInteractionEnabled = YES;
sample_book1.tag = 0;
[sample_book1 addGestureRecognizer:tapped];
[self.view addSubview:sample_book1];
UIImageView *sample_book2= [[UIImageView alloc]initWithFrame:CGRectMake(220, 135, 100,125) ];
sample_book2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"biology.png"]];
sample_book2.userInteractionEnabled = YES;
sample_book2.tag = 1;
[sample_book2 addGestureRecognizer:tapped];
[self.view addSubview:sample_book2];
UIImageView *sample_book3= [[UIImageView alloc]initWithFrame:CGRectMake(370, 135, 100,125) ];
sample_book3.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"chemistry.png"]];
sample_book3.userInteractionEnabled = YES;
sample_book3.tag = 2;
[sample_book3 addGestureRecognizer:tapped];
[self.view addSubview:sample_book3];
點擊手勢在sample_book1,sample_book2中不起作用....它只在sample_book3中工作..我做錯了什么..
你做錯了是試圖以不應該使用的方式使用手勢。 手勢只能附加到一個視圖。 您需要為每個視圖創建一個新的視圖。
正如borrrden所說,在嘗試跟蹤手勢時,每個視圖都必須有自己的gestureRecognizer。 對於每個sample_books,您都應該使用
[sample_bookX addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(myFunction:)]];
而不是試圖多次添加相同的GR
myFunction收到的參數:然后是正確的tapGR,你可以通過調用sender.view來獲取tapped imageView(提供你的myFunction簽名看起來像
- (void) myFunction:(UIGestureRecognizer *)sender
干杯,
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.