英文:
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` 的实现。
}
请注意,上述代码片段中,respondToSwipeGesture 和 profileImageView 的相关部分已被保留,以便您进一步调试和处理。
英文:
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
默认情况下,UIImageView 的 isUserInteractionEnabled 被设置为 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论