我有一个UIScrollView占据了整个屏幕的一半。在另一半中,我还有另一个UIView。
当用户在UIView中点击,平移,捏住时;我希望这些手势在UIScrollView中生效,就像它们发生在那里一样。
这可能吗?
我有一个UIScrollView占据了整个屏幕的一半。在另一半中,我还有另一个UIView。
当用户在UIView中点击,平移,捏住时;我希望这些手势在UIScrollView中生效,就像它们发生在那里一样。
这可能吗?
免责声明:这是一个相当轻巧的解决方案,但是根据您要实现的目标而定。
您可以尝试对UIScrollView
进行子类化,以提供对任何给定触摸事件是在scrollView框架中的“内部”还是“外部”的一些自定义识别,并且只要触摸发生在下部时就简单地将其声明为“ in”屏幕的一半,或您想要的其他“控制区域”。
class CustomScrollView: UIScrollView {
var targetFrame: CGRect? // set this to the frame of your "Other View"
override func point(inside point: CGPoint,with event: UIEvent?) -> Bool {
return targetFrame?.contains(point) == true
}
}
注意:如果您不提供CGRect
而只是提供return true
,则屏幕上任何位置的拖动或捏合手势都会在scrollView中注册。
我创建了一个名为PassGesturesView的UIView,它覆盖了hitTest方法。收到手势的UIView必须对其进行扩展:
//
// PassGesturesView.swift
// pass-gestures
//
// Created by Sergio Rodríguez Rama on 23/11/2019.
// Copyright © 2019 Sergio Rodríguez Rama. All rights reserved.
//
import UIKit
class PassGesturesView: UIView {
var sisterView: UIView?
override func hitTest(_ point: CGPoint,with event: UIEvent?) -> UIView? {
return sisterView
}
// MARK: - Init
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
然后,只需将滚动视图(或要在其中发送手势的任何其他UIView)设置为PassGesturesView的sisterView即可。
passGesturesView.sisterView = scrollView