gpt4 book ai didi

ios - UIPageViewController 未在单击按钮时完成转换

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

我有一个 UIPageViewController 处理一组 viewControllers当我在它们之间滑动时,一切都很好,但是我有一些方法(成功登录后和单击菜单按钮后)触发“跳转”到 UIPageViewControllers 中的 viewController > 数组。每当我跳转到一个页面时,转换都没有完成,并且我有一个屏幕“卡在”两个 viewControllers

之间

为简单起见,我将只添加成功登录代码,因为我为按钮点击设置了相同的所有内容,并且发生了同样的事情。

这是我的 UIPageViewController 设置:

import UIKit

class PageViewController: UIPageViewController , UIPageViewControllerDataSource , UIPageViewControllerDelegate , ClubViewControllerDelegate , CustomSvDelegate{

//clubvcdelegate protocol method

func fbLoggedInWithSuccess(){
self.displayPageForIndex(index: 4, animated: false)
}

//menubuttondelegate protocol method
func menuButtonPressed0() {

self.displayPageForIndex(index: 0, animated: false)
self.currentPageIndex = 0
self.closeMenu()
}

//page swipe control

lazy var vcArray : [UIViewController] = {
return [self.vcInstance (name : "clubVC"),
self.vcInstance (name : "menuVC"),
self.vcInstance (name : "contactVC"),
self.vcInstance (name : "aboutVC"),
self.vcInstance (name : "membersVC")]
}()

private func vcInstance(name : String) -> UIViewController{
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
return storyBoard.instantiateViewController(withIdentifier: name)
}


override func viewDidLoad() {
super.viewDidLoad()
self.dataSource = self
self.delegate = self
if let clubVC = vcArray.first {
let club = clubVC as! ClubViewController
club.delegate = self
setViewControllers([clubVC], direction: .forward, animated: true, completion: nil)
}
self.generateStackView()
}


override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
for view in self.view.subviews {
if view is UIScrollView {view.frame = UIScreen.main.bounds} else if view is UIPageControl {view.backgroundColor = UIColor.clear}
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?{
guard let viewControllerIndex = vcArray.index(of: viewController) else {return nil}

let previousIndex = viewControllerIndex - 1

guard previousIndex >= 0 else {return vcArray.last}

guard vcArray.count > previousIndex else {return nil}

return vcArray[previousIndex]

}


public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?{
guard let viewControllerIndex = vcArray.index(of: viewController) else {return nil}

let nextIndex = viewControllerIndex + 1

guard nextIndex < vcArray.count else {return vcArray.first}

guard vcArray.count > nextIndex else {return nil}

return vcArray[nextIndex]

}

public func presentationCount(for pageViewController: UIPageViewController) -> Int{
return vcArray.count
}


public func presentationIndex(for pageViewController: UIPageViewController) -> Int{
guard let firstViewController = viewControllers?.first ,
let firstViewControllerIndex = vcArray.index(of: firstViewController) else {
return 0
}
return firstViewControllerIndex
}

这就是我设置 fblogin 成功的方式:

protocol ClubViewControllerDelegate:class {
func fbLoggedInWithSuccess()
}

class ClubViewController: UIViewController , FBSDKLoginButtonDelegate {
var delegate:ClubViewControllerDelegate?

func showFbDetails(){

let accesToken = FBSDKAccessToken.current()

guard let accesTokenString = accesToken?.tokenString else {return}

let credentials = FIRFacebookAuthProvider.credential(withAccessToken: accesTokenString)

FIRAuth.auth()?.signIn(with: credentials, completion: { (user, error) in
if error != nil {
print ("something went wrong" , error!)
return
}
print ("succes")
self.delegate?.fbLoggedInWithSuccess()
})

}

图片: enter image description here enter image description here

编辑2这仅在动画为 true 时发生,为 false 时正确完成

编辑3这是我现在用来设置 View Controller 的方法:

    func displayPageForIndex(index : Int , animated : Bool = true){
assert(index >= 0 && index < self.vcArray.count, "Error: Attempting to display a page for an out of bounds index")
if self.currentPageIndex == index {return}
if index < self.currentPageIndex {
self.setViewControllers([self.vcArray[index]], direction: .reverse, animated: false, completion: nil)
} else if index > self.currentPageIndex {
self.setViewControllers([self.vcArray[index]], direction: .forward, animated: false, completion: nil)
}
}

我在 fbLoggenInWithSuccessmenuButtonPressed 中调用它。另外,有一个变量 CurrentIndex 保存页面的当前索引

最佳答案

正如马特建议的那样,删除 viewDidLayoutSubviews 解决了这个问题

关于ios - UIPageViewController 未在单击按钮时完成转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42498138/

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