gpt4 book ai didi

swift - 我想重现Twitter和Instagram的通知系统

转载 作者:行者123 更新时间:2023-11-30 10:36:41 24 4
gpt4 key购买 nike

我试图将我的 View Controller 分为两个不同的部分,通过向右或向左滑动进行访问,就像 Twitter 和 Instagram 的通知一样。

twitter notifications

我不知道这个设计的名称是什么,所以我在 Google 上的研究没有成功。我尝试自己做。我有一个 View Controller ,其中有一个 View 包含两个部分标签,光标在下面,还有一个 View 容器包含页面 View Controller 以启用滑动。我可以在两个部分之间滑动,但我不知道在两个部分之间滑动时如何移动光标并更改标签的颜色和粗细。

simulator of what I have done

这是我绘制线条和光标的 View 代码:

class LineView: UIView {

let cursor = CAShapeLayer()

override func draw(_ rect: CGRect) {
let line = UIBezierPath()
line.lineWidth = 1
line.move(to: CGPoint(x: 0, y: 0))
line.addLine(to: CGPoint(x: self.frame.width,y: 0))
UIColor.lightGray.setStroke()
line.stroke()

let path = UIBezierPath()
path.move(to: CGPoint(x: 0, y: 0))
path.addLine(to: CGPoint(x: self.frame.width / 2, y: 0))
cursor.path = path.cgPath
cursor.strokeColor = UIColor(red:1.00, green:0.09, blue:0.40, alpha:1.0).cgColor
cursor.lineWidth = 2

layer.addSublayer(cursor)
}
}

和我的页面 View Controller 的代码:

class HomePageViewController: UIPageViewController, UIPageViewControllerDataSource {

lazy var viewControllerList : [UIViewController] = {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController1 = storyboard.instantiateViewController(withIdentifier: "PublicViewController")
let viewController2 = storyboard.instantiateViewController(withIdentifier: "PrivateViewController")

return [viewController1, viewController2]
}()

override func viewDidLoad() {
super.viewDidLoad()

self.dataSource = self

if let firstViewController = viewControllerList.first {
self.setViewControllers([firstViewController], direction: .forward, animated: false, completion: nil)
}
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let index = viewControllerList.firstIndex(of: viewController) else { return nil }
let previousIndex = index - 1
guard previousIndex >= 0 else { return nil }
guard viewControllerList.count > previousIndex else { return nil }
return viewControllerList[previousIndex]
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let index = viewControllerList.firstIndex(of: viewController) else { return nil }
let nextIndex = index + 1
guard viewControllerList.count != nextIndex else { return nil }
guard viewControllerList.count > nextIndex else { return nil }
return viewControllerList[nextIndex]
}

}

我不确定这是否是正确的方法!

最佳答案

您可以使用标题的 Collection View ,即公共(public)私有(private)

对于 Controller ,我已将 UIScrollView 放置在 Storyboard的 View Controller 内。然后在 ScrollView 中添加两个容器 View ,其高度与 ScrollView 相等。有关代码您可以在下面的项目中下载。

这是您想要的完整演示项目:CustomStepperController

关于swift - 我想重现Twitter和Instagram的通知系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57842217/

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