UITapGestureRecognizer not working in UIImageView
Asked Answered
M

10

36

I had the following code:

UITapGestureRecognizer *showStoryTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showNewsStory:)];
[showStoryTapRecognizer setDelegate:self];
[self.storyImageView_ addGestureRecognizer:showStoryTapRecognizer];
[showStoryTapRecognizer release];

This however doesn't trigger the showNewsStory, why is this? I have enabled userInteraction in the image view.

Mellisamellisent answered 19/7, 2012 at 0:9 Comment(1)
is the ImageView in a tableview?Pandiculation
K
62
UITapGestureRecognizer *oneTouch=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(OneTouchHandeler)];

[oneTouch setNumberOfTouchesRequired:1];

[imageView addGestureRecognizer:oneTouch];

imageView.userInteractionEnabled = YES;
Kerakerala answered 19/7, 2012 at 10:56 Comment(0)
T
81

You should enable user interaction for UIImageView object:

[self.storyImageView_ setUserInteractionEnabled:YES];

EDIT:

Try to remove the

[showStoryTapRecognizer setDelegate:self];

I don't think UITapGestureRecognizer has its delegate methods as far as I know.

Tiga answered 19/7, 2012 at 0:14 Comment(4)
@Mellisamellisent actually, I thinks the code what you gave is fine (except the one I pointed out above). Maybe one of subviews covered the storyImageView_.Tiga
@Tiga [self.storyImageView_ setUserInteractionEnabled:YES]; this was helpful. but UIGestureRecognizer is the super class of UITapGestureRecognizer, so we can set delegate when ever UIGestureRecognizerDelegate required.Remora
That's the real answer. Rest of them did not work for me, except this one.Ptero
@Ptero glad it helps :)Tiga
K
62
UITapGestureRecognizer *oneTouch=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(OneTouchHandeler)];

[oneTouch setNumberOfTouchesRequired:1];

[imageView addGestureRecognizer:oneTouch];

imageView.userInteractionEnabled = YES;
Kerakerala answered 19/7, 2012 at 10:56 Comment(0)
C
16

UIImageView has user interaction disabled by default, unlike most other UIView subclasses in UIKit.

Countermand answered 19/7, 2012 at 0:52 Comment(2)
UILabel has it disabled as well.Flatways
Wasted a lot of time on this. Thank you. That checkbox is hidden away nicely ;)Terry
T
7

I also noticed that in swift3, if you are adding a gesture recognizer which also looks for the target and the target is usually self, then you have to make the UIView to which you are adding the gesture recognizer to be a lazy var. Otherwise the gesture recognizer won't work. I think this is a bug in swift3. Ideally if you are accessing self in a variable before the class is fully initialized, it should throw an error. The code below won't detect gesture recognizer.

let messageImageView: CachedImageView = {
    let iv = CachedImageView()
    iv.translatesAutoresizingMaskIntoConstraints = false
    iv.layer.cornerRadius = 16
    iv.layer.masksToBounds = true
    iv.contentMode = .scaleAspectFill
    iv.isUserInteractionEnabled = true
    let zoomTap = UITapGestureRecognizer(target: self, action: #selector(handleZoomTap))
    zoomTap.numberOfTapsRequired = 1
    iv.addGestureRecognizer(zoomTap)
    return iv
}()

To fix that, you have to use lazy var

lazy var messageImageView: CachedImageView = {
    let iv = CachedImageView()
    iv.translatesAutoresizingMaskIntoConstraints = false
    iv.layer.cornerRadius = 16
    iv.layer.masksToBounds = true
    iv.contentMode = .scaleAspectFill
    iv.isUserInteractionEnabled = true
    let zoomTap = UITapGestureRecognizer(target: self, action: #selector(handleZoomTap))
    zoomTap.numberOfTapsRequired = 1
    iv.addGestureRecognizer(zoomTap)
    return iv
}()
Truly answered 21/6, 2017 at 21:30 Comment(2)
That's a very helpful observation! I guess this happens because self doesn't yet exist when this method is called, so the gesture recognizer has no target.Infold
Thanks! Yes, we should use lazy var instead of let to make sure self does exist when you tap it.Martinet
C
4

Maybe ... action:@selector(showNewsStory) instead of action:@selector(showNewsStory:) . Please check it . Are there any other UITapGestureRecognizer in this controller ? Try this:

otherTapRecognizer.cancelsTouchesInView = NO;
Cookshop answered 19/7, 2012 at 1:23 Comment(1)
That's okay, if the selector is wrong, it'll lead a crash of EXC_BAD_ACCESS.Tiga
P
3

If you have already set imageView.userInteractionEnabled = YES; but the action still doesn't fire. Maybe it's because for one of superviews of imageView userInteractionEnabled is NO;

Puissance answered 21/6, 2017 at 3:18 Comment(0)
S
2

objective-c ios 10

UITapGestureRecognizer *oneTouch=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(OneTouchHandeler)];

[oneTouch setNumberOfTouchesRequired:1];

[imageView addGestureRecognizer:oneTouch];

imageView.userInteractionEnabled = YES;

// swift 3.0

   let oneTouch = UITapGestureRecognizer(target: self, action: #selector(self.OneTouchHandeler(_:)))

imageView.addGestureRecognizer(oneTouch)

imageView.isUserInteractionEnabled = true
Sphery answered 12/12, 2016 at 5:21 Comment(0)
A
2

add

storyImageView.isUserInteractionEnabled = true;

Arbitration answered 26/4, 2021 at 22:17 Comment(0)
B
0

if you allowed 2 different gesture, you should add below code snippet. For example, you use pickerView and also you want to detect tap gesture for same pickerView.

Asks the delegate if two gesture recognizers should be allowed to recognize gestures simultaneously.

Objective C

    -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return true;
}

Swift

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}
Bowman answered 1/11, 2018 at 8:23 Comment(0)
B
0
    use lazy var instead of let . Here is an example.

    lazy var loginSignUpView: LoginSignUpView = {
            let lsv = LoginSignUpView()
            lsv.loginRegisterButton.addTarget(self, action: #selector(handleLoginRegistrationButton), for: .touchUpInside)
            lsv.segmentedControlForLoginRegister.addTarget(self, action: #selector(handleSegmentedControlForLoginRegister), for: .valueChanged)
            lsv.profileImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleSelectProfileImageView)))

            return lsv
        }()


@objc func handleSelectProfileImageView() {
        print("Image tapped")
    }
Brynn answered 17/4, 2020 at 9:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.