Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
592 views
in Technique[技术] by (71.8m points)

objective c - tap gesture not recognized on uiimageview

I added two uiimageviews, one on another subview uiview (imageview1,imageview2). In the first view the top uiimageview is hidden(imageview2) and in the second view the bottom imageview is hidden(imageview1).

Allocating tap gesture:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneTap:)];
UITapGestureRecognizer *singleTap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneTap:)];

Set user interaction for both uiimageview to YES.

[singleTap setNumberOfTapsRequired:1];
[singleTap1 setNumberOfTapsRequired:1];

// adding gesture to uiimageview

Add tap gesture recognizer and selector respectively.

[imageview1 addGestureRecognizer:singleTap];
[imageview2 addGestureRecognizer:singleTap1];

But my taps are not recognized.

Can any one tell me where the mistake is?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try setting setUserInteractionEnabled:YES before adding gesture recognizer.

[imageview1 setUserInteractionEnabled:YES]
[imageview2 setUserInteractionEnabled:YES]

[imageview1 addGestureRecognizer:singleTap];
[imageview2 addGestureRecognizer:singleTap1];   

Update:

After the comment you have made I suggest you bring your views to the top before detecting the tap event. Because parent imageView is above and catches these taps.

[yourparentview bringSubviewToFront:imageview1];
[yourparentview bringSubviewToFront:imageview2];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...