How to add "UISwipeGestureRecognizer" and "TapGestureRecognizer" in Same viewcontroller in swift

huangapple go评论48阅读模式
英文:

How to add "UISwipeGestureRecognizer" and "TapGestureRecognizer" in Same viewcontroller in swift

问题

我需要为我的视图控制器添加左滑手势,同时在同一个视图控制器中,如果我点击图像视图,我需要使用 "UITapGestureRecognizer" 并推到另一个视图控制器以在另一个视图控制器中显示完整图像。

代码: respondToSwipeGesture 在这里起作用,我能够返回,但是对于 profileImageView 的点按手势不起作用。当我点击图像时,断点也不触发。如何使其工作,请指导我。

class CommentViewController: UIViewController, UIGestureRecognizerDelegate {
  
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.interactivePopGestureRecognizer?.delegate = self
        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
        let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
        swipeRight.direction = UISwipeGestureRecognizer.Direction.left
        self.view.addGestureRecognizer(swipeRight)
        
        profileImageView.addTapGestureRecognizer { [weak self] in
            self?.gotoStudentProfile()
        }
    }

    private func gotoStudentProfile() {
        let vc = Helper.getVcObject(vcName: .StudentProfileViewController, storyboardName: .Profile) as! StudentProfileViewController
        checkAndPushPop(vc, navigationController: navigationController)
    }
    
    // ... 下面是一些关于手势的代码,包括 `addTapGestureRecognizer` 和 `handleTapGesture` 的实现。
}

请注意,上述代码片段中,respondToSwipeGestureprofileImageView 的相关部分已被保留,以便您进一步调试和处理。

英文:

I need left swipe gesture for my viewcontroller and in the same viewcontroller i have one imageView if i click that imageView i need to use "UITapGestureRecognizer" and push to another viewcontroller to show that full image in another viewcontroller

code: here respondToSwipeGesture is working i am able to go back but tap gesture for profileImageView is not working. when i tap on image break point also not hitting. how to make it work. please guide me.

class CommentViewController: UIViewController, UIGestureRecognizerDelegate {

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationController?.interactivePopGestureRecognizer?.delegate = self        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
    let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
    swipeRight.direction = UISwipeGestureRecognizer.Direction.left
    self.view.addGestureRecognizer(swipeRight)
    
    profileImageView.addTapGestureRecognizer { [weak self] in
        self?.gotoStudentProfile()
    }
}

private func gotoStudentProfile() {
let vc = Helper.getVcObject(vcName: .StudentProfileViewController, storyboardName: .Profile) as! StudentProfileViewController
checkAndPushPop(vc, navigationController: navigationController)
 }

fileprivate typealias Action = (() -> Void)?

fileprivate var tapGestureRecognizerAction: Action? {
    set {
        if let newValue = newValue {
            // Computed properties get stored as associated objects
            objc_setAssociatedObject(self, &AssociatedObjectKeys.tapGestureRecognizer, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
        }
    }
    get {
        let tapGestureRecognizerActionInstance = objc_getAssociatedObject(self, &AssociatedObjectKeys.tapGestureRecognizer) as? Action
        return tapGestureRecognizerActionInstance
    }
}

 public func addTapGestureRecognizer(action: (() -> Void)?) {
    self.isUserInteractionEnabled = true
    self.tapGestureRecognizerAction = action
    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTapGesture))
    self.addGestureRecognizer(tapGestureRecognizer)
 }  

@objc fileprivate func handleTapGesture(sender: UITapGestureRecognizer) {
    if let action = self.tapGestureRecognizerAction {
        action?()
    } else {
        print("no action")
    }
}

答案1

得分: 1

默认情况下,UIImageViewisUserInteractionEnabled 被设置为 false。您需要将其设置为 true
另外,由于您没有分享 addTapGestureRecognizer 方法的实现,请确保它没有任何错误。

英文:

By default, UIImageView has isUserInteractionEnabled set to false. You have to set it to true.
Also, since you have not shared the implementation of the addTapGestureRecognizer method, make sure that it does not have any bugs.

答案2

得分: 1

profileImageView.addTapGestureRecognizer
上面这段代码设置了isUserInteractionEnabled以启用图像。

profileImageView.isUserInteractionEnabled = true

已更新

let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap))

profileImageView.addGestureRecognizer(tap)

profileImageView.isUserInteractionEnabled = true

// 当调用handleTap时触发的函数
@objc func handleTap(_ sender: UITapGestureRecognizer) {
    print("Hello World")
}

试试这种方式,然后告诉我。

英文:
profileImageView.addTapGestureRecognizer

above this code set isUserInteractionEnabled enable for image

profileImageView.isUserInteractionEnabled = true

Updated

    let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap))

    profileImageView.addGestureRecognizer(tap)

    profileImageView.isUserInteractionEnabled = true
    
    // function which is triggered when handleTap is called
    @objc func handleTap(_ sender: UITapGestureRecognizer) {
        print("Hello World")
    }

Try this way and let me know

huangapple
  • 本文由 发表于 2023年7月13日 14:56:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76676676.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定