32

我有以下代码:

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

然而,这不会触发 showNewsStory,这是为什么呢?我在图像视图中启用了 userInteraction。

4

10 回答 10

78

UIImageView您应该为对象启用用户交互:

[self.storyImageView_ setUserInteractionEnabled:YES];

编辑:

尝试删除

[showStoryTapRecognizer setDelegate:self];

UITapGestureRecognizer据我所知,我 认为没有它的委托方法。

于 2012-07-19T00:14:48.923 回答
62
UITapGestureRecognizer *oneTouch=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(OneTouchHandeler)];

[oneTouch setNumberOfTouchesRequired:1];

[imageView addGestureRecognizer:oneTouch];

imageView.userInteractionEnabled = YES;
于 2012-07-19T10:56:38.963 回答
14

UIImageViewUIView与UIKit 中的大多数其他子类不同,默认情况下禁用用户交互。

于 2012-07-19T00:52:05.627 回答
7

我还注意到,在 swift3 中,如果您要添加一个手势识别器,该手势识别器也会查找目标并且目标通常是self,那么您必须将要添加手势识别器的 UIView 设置为lazy var. 否则手势识别器将无法工作。我认为这是swift3中的一个错误。理想情况下,如果您在类完全初始化之前访问变量中的 self,它应该会引发错误。下面的代码不会检测手势识别器。

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
}()

要解决这个问题,你必须使用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
}()
于 2017-06-21T21:30:51.050 回答
4

也许......action:@selector(showNewsStory)而不是 action:@selector(showNewsStory:) 。请检查一下 。UITapGestureRecognizer这个控制器里还有其他的吗?尝试这个:

otherTapRecognizer.cancelsTouchesInView = NO;
于 2012-07-19T01:23:18.217 回答
2

目标c ios 10

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

[oneTouch setNumberOfTouchesRequired:1];

[imageView addGestureRecognizer:oneTouch];

imageView.userInteractionEnabled = YES;

// 快速 3.0

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

imageView.addGestureRecognizer(oneTouch)

imageView.isUserInteractionEnabled = true
于 2016-12-12T05:21:14.583 回答
2

如果您已经设置imageView.userInteractionEnabled = YES;但操作仍然没有触发。也许是因为 imageView userInteractionEnabled 的超级视图之一是 NO;

于 2017-06-21T03:18:57.247 回答
1

添加

storyImageView.isUserInteractionEnabled = true;

于 2021-04-26T22:17:25.110 回答
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")
    }
于 2020-04-17T09:44:23.217 回答
0

如果您允许 2 种不同的手势,您应该添加下面的代码片段。例如,您使用 pickerView 并且还想检测相同 pickerView 的点击手势。

询问代理是否应允许两个手势识别器同时识别手势。

目标 C

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

迅速

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}
于 2018-11-01T08:23:08.277 回答