gpt4 book ai didi

ios - 拦截 UIPageViewController 的 UIPanGestureRecognizer 会破坏 UIPageViewController 的行为

转载 作者:行者123 更新时间:2023-11-28 08:32:28 25 4
gpt4 key购买 nike

我正在尝试将 UIPageViewController 的 UIPanGestureRecognizer 分配给不同的(自定义) View ,以便用于页面切换的平移手势仅适用于屏幕的特定区域。代码基本上如下:

let testView = UIView(frame: CGRect(x: 0, y:self.view.frame.height/2, width: 320, height: self.view.frame.height/2))
self.view.addSubview(testView)
for gr in self.pageViewController!.view.subviews[0].gestureRecognizers! {
if gr.isKindOfClass(UIPanGestureRecognizer) {
testView.addGestureRecognizer(gr)
}
}

问题是“下一个” View Controller 在进行平移时不会立即可见,在第一次平移时,当前 View Controller (第一个)似乎是 PageViewController 中唯一的 View Controller ,它仅在结束平移和进行第二次平移以显示下一个 View Controller 。

请注意,如果不将 UIPanGesture 分配给不同的 View ,PageViewController 将按预期工作,下一个 View Controller 自第一次平移后可见。

我为什么要这样做?我需要在 PageViewController 内的 View Controller 中添加一个 subview ,当用户在其上方做手势时,它不会“平移”PageViewController。 p>

最佳答案

UIPageViewController

class PageController:UIPageViewController {

let viewControllers:[UIViewController] = [MyController(), MyController(), MyController()]

override init(transitionStyle style: UIPageViewControllerTransitionStyle, navigationOrientation: UIPageViewControllerNavigationOrientation, options: [String : AnyObject]?) {
super.init(transitionStyle: .Scroll, navigationOrientation: .Horizontal, options: options)
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

override func viewDidLoad() {
super.viewDidLoad()

if let firstVC = controllers.first {
setViewControllers([firstVC], direction.Forward, animated: true, completion:nil)
}
}

func pageViewController(pageViewController: UIPageViewController,
viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.indexOf(viewController) else {
return nil
}

let previousIndex = viewControllerIndex - 1

guard previousIndex >= 0 else {
return nil
}

guard orderedViewControllers.count > previousIndex else {
return nil
}

return orderedViewControllers[previousIndex]
}

func pageViewController(pageViewController: UIPageViewController,
viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.indexOf(viewController) else {
return nil
}

let nextIndex = viewControllerIndex + 1
let orderedViewControllersCount = orderedViewControllers.count

guard orderedViewControllersCount != nextIndex else {
return nil
}

guard orderedViewControllersCount > nextIndex else {
return nil
}

return orderedViewControllers[nextIndex]
}

}

查看

class SmallView:UIView {
//Since yours is not a ScrollView I changed to a view. You can add the Pan Gesture here.
var colors:[UIColor] = [UIColor.redColor(), UIColor.blueColor(), UIColor.blackColor()]

convenience init(frame: CGRect, x:Int) {
self.init()
self.frame = frame
self.backgroundColor = colors[x]

setupGesture()
}

func setupGesture() {
let pan = UIPanGestureRecognizer(target: self, action: #selector(SmallView.action))
}
func action(gesture:UIPanGestureRecognizer) {
print("gesture = ", gesture)
}
}

我的 Controller

class MyController:UIViewController {

override func viewDidLoad() {
......
let view = SmallView(frame:CGRect(//some frame here))
self.view.addSubview(view)
}

关于ios - 拦截 UIPageViewController 的 UIPanGestureRecognizer 会破坏 UIPageViewController 的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38597300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com