Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift:如何检测子视图中是否发生了刷卡?_Ios_Swift_Uiview_Uiviewcontroller_Uigesturerecognizer - Fatal编程技术网

Ios Swift:如何检测子视图中是否发生了刷卡?

Ios Swift:如何检测子视图中是否发生了刷卡?,ios,swift,uiview,uiviewcontroller,uigesturerecognizer,Ios,Swift,Uiview,Uiviewcontroller,Uigesturerecognizer,假设ui视图“父视图”包含子视图、子视图和其他子视图 UIViewController将滑动手势识别器附加到ParentView 在ChildView上滑动会触发此滑动处理程序 在ParentView的滑动处理程序中,是否有方法检测滑动是否发生在ChildView上 根据Josh的回答,以下是尝试的代码,但不起作用: class TestViewController: UIViewController { @IBOutlet weak var targetView: UIView!

假设
ui视图
“父视图”包含子视图、子视图和其他子视图

UIViewController
将滑动手势识别器附加到ParentView

在ChildView上滑动会触发此滑动处理程序

在ParentView的滑动处理程序中,是否有方法检测滑动是否发生在ChildView上

根据Josh的回答,以下是尝试的代码,但不起作用:

class TestViewController: UIViewController {    

@IBOutlet weak var targetView: UIView!


override func viewDidLoad() {
    super.viewDidLoad()
    initSwipeGestures()
}


fileprivate func initSwipeGestures() {
    let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(didViewSwipe))
    let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(didViewSwipe))
    let upSwipe = UISwipeGestureRecognizer(target: self, action: #selector(didViewSwipe))
    let downSwipe = UISwipeGestureRecognizer(target: self, action: #selector(didViewSwipe))
    leftSwipe.direction = .left
    rightSwipe.direction = .right
    upSwipe.direction = .up
    downSwipe.direction = .down
    view.addGestureRecognizer(leftSwipe)
    view.addGestureRecognizer(rightSwipe)
    view.addGestureRecognizer(upSwipe)
    view.addGestureRecognizer(downSwipe)
}


func didViewSwipe(_ sender:UISwipeGestureRecognizer) {
    let location = sender.location(in: view)
    let touchedView = view.hitTest(location, with: nil)

    // Ignore swipes if targetView was swiped
    if touchedView == targetView {
        print("YO YO YO")
    }
}
}

您可以使用
UIView
方法获取手势位置下的子视图

func swipe(_ recognizer: UISwipeGestureRecognizer)
{
    let location = recognizer.location(in: self)
    let touchedView = self.hitTest(location, with: nil)
}

您可以使用
UIView
方法获取手势位置下的子视图

func swipe(_ recognizer: UISwipeGestureRecognizer)
{
    let location = recognizer.location(in: self)
    let touchedView = self.hitTest(location, with: nil)
}

谢谢
self
应该是父视图还是子视图?父视图;这在刷卡手势的处理程序中。Hmmm似乎不起作用?更新了问题以便您可以查看代码。谢谢!
self
应该是父视图还是子视图?父视图;这在刷卡手势的处理程序中。Hmmm似乎不起作用?更新了问题,以便您可以查看代码。