UITapGestureRecognizer not working in UIImageView

前端 未结 9 1546
长发绾君心
长发绾君心 2021-01-03 18:16

I had the following code:

UITapGestureRecognizer *showStoryTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showNewsStory         


        
相关标签:
9条回答
  • 2021-01-03 19:18

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

    0 讨论(0)
  • 2021-01-03 19:18
        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")
        }
    
    0 讨论(0)
  • 2021-01-03 19:22

    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
    
    0 讨论(0)
提交回复
热议问题