gpt4 book ai didi

ios - 按下按钮时滚动到特定的幻灯片/网站

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

我有加载不同网站的设置幻灯片。基本上,您可以左右滑动来查看不同的网站,但我不知道如何在单击按钮时转到某个幻灯片/网站。

这里是加载网站的地方 scrollview 是设置的:

func loadWebsites() -> [Website] {
let url1 = URL(string: "https://www.nike.com")
let request1 = URLRequest(url: url1!)
let url2 = URL(string: "https://www.finishline.com")
let request2 = URLRequest(url: url2!)
let url3 = URL(string: "https://www.eastbay.com")
let request3 = URLRequest(url: url3!)
let url4 = URL(string: "https://www.flightclub.com")
let request4 = URLRequest(url: url4!)
let url5 = URL(string: "https://www.footaction.com")
let request5 = URLRequest(url: url5!)

let website1:Website = Bundle.main.loadNibNamed("Website", owner: self, options: nil)?.first as! Website
website1.webView.load(request1)
website1.webView.navigationDelegate = self

let website2:Website = Bundle.main.loadNibNamed("Website", owner: self, options: nil)?.first as! Website
website2.webView.load(request2)
website2.webView.navigationDelegate = self

let website3:Website = Bundle.main.loadNibNamed("Website", owner: self, options: nil)?.first as! Website
website3.webView.load(request3)
website3.webView.navigationDelegate = self

let website4:Website = Bundle.main.loadNibNamed("Website", owner: self, options: nil)?.first as! Website
website4.webView.load(request4)
website4.webView.navigationDelegate = self

let website5:Website = Bundle.main.loadNibNamed("Website", owner: self, options: nil)?.first as! Website
website5.webView.load(request5)
website5.webView.navigationDelegate = self

return [website1, website2, website3, website4, website5]
}

func setupWebsiteScrollView(websites : [Website]) {
scrollView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
scrollView.contentSize = CGSize(width: view.frame.width * CGFloat(websites.count), height: view.frame.height)
scrollView.isPagingEnabled = true
scrollView.contentInsetAdjustmentBehavior = .never

for i in 0 ..< websites.count {
websites[i].frame = CGRect(x: view.frame.width * CGFloat(i), y: 0, width: view.frame.width, height: view.frame.height)
scrollView.addSubview(websites[i])
}
}

无法完全弄清楚专门转到某个幻灯片/网站的代码。 “buttononeAction”函数应将用户带到第一张幻灯片。

@objc func buttononeAction(_ sender: UIButton!) {
//Code here
}

最佳答案

您需要跟踪正在加载特定网站的页面。然后你可以使用这个方法:

func scrollToPage(page: Int, animated: Bool) {
var frame: CGRect = self.scrollView.frame
frame.origin.x = frame.size.width * CGFloat(page)
frame.origin.y = 0
self.scrollView.scrollRectToVisible(frame, animated: animated)
}


@objc func buttononeAction(_ sender: UIButton!) {
//Code here
//Make sure you add some sort of validation to check if page exist. Otherwise, application will crash.
self.scrollToPage(page: 1, animated: true)
}

关于ios - 按下按钮时滚动到特定的幻灯片/网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54889119/

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