0
我在我的菜單項下有一個單槓視圖,每當我點擊其中一個菜單項時,它就會出現在該項下。我在代碼中將其命名爲movingView
。下面是截圖:ScrollView和菜單項下的小橫條?
這菜單下我有一個UIScrollView
。我在UIScrollView
內部放置了5個視圖,我可以在它們之間滑動。現在,每當我滑動,我想要我的菜單項下的單槓視圖按照我的選擇。例如,當我點擊手槍時,它從步槍移動到手槍。當我從UIScrollView
內的第一頁滑動到第二頁時,我希望發生這種情況。
請別人幫我。已經3天了,我無法弄清楚如何做到這一點。
我的代碼:
class TabViewController: UIViewController
{
@IBOutlet var scrollView: UIScrollView!
@IBOutlet var buttons: [UIButton]!
@IBOutlet var backgroundView: UIImageView!
var movingView = UIView()
let screenWidth = UIScreen.main.bounds.width
var rifleViewController: UIViewController!
var pistolViewController: UIViewController!
var shotgunViewController: UIViewController!
var smgsViewController: UIViewController!
var sniperViewController: UIViewController!
override func viewDidLoad()
{
super.viewDidLoad()
let main = UIStoryboard(name: "Main", bundle: nil)
rifleViewController = main.instantiateViewController(withIdentifier: "rifles")
sniperViewController = main.instantiateViewController(withIdentifier: "snipers")
smgsViewController = main.instantiateViewController(withIdentifier: "smgss")
shotgunViewController = main.instantiateViewController(withIdentifier: "shotguns")
pistolViewController = main.instantiateViewController(withIdentifier: "pistols")
self.scrollView.contentSize = CGSize(width: self.view.frame.width * 5, height: self.view.frame.height)
rifleViewController.view.frame = scrollView.bounds
scrollView.addSubview(rifleViewController.view)
pistolViewController.view.frame = scrollView.bounds
pistolViewController.view.frame.origin.x = scrollView.frame.size.width
scrollView.addSubview(pistolViewController.view)
shotgunViewController.view.frame = scrollView.bounds
shotgunViewController.view.frame.origin.x = scrollView.frame.size.width * 2
scrollView.addSubview(shotgunViewController.view)
smgsViewController.view.frame = scrollView.bounds
smgsViewController.view.frame.origin.x = scrollView.frame.size.width * 3
scrollView.addSubview(smgsViewController.view)
sniperViewController.view.frame = scrollView.bounds
sniperViewController.view.frame.origin.x = scrollView.frame.size.width * 4
scrollView.addSubview(sniperViewController.view)
movingView = UIView(frame: CGRect(x: 0, y: backgroundView.frame.maxY - 5, width: screenWidth/5, height: 10))
movingView.backgroundColor = UIColor.white
backgroundView.addSubview(movingView)
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
@IBAction func riflePressTab(_ sender: Any)
{
scrollView.setContentOffset(CGPoint(x: 0, y: 0) , animated: true)
}
@IBAction func pistolPressTab(_ sender: Any)
{
scrollView.setContentOffset(CGPoint(x: scrollView.frame.size.width, y: 0) , animated: true)
}
@IBAction func shotgunPressTab(_ sender: Any)
{
scrollView.setContentOffset(CGPoint(x: scrollView.frame.size.width * 2, y: 0) , animated: true)
}
@IBAction func smgsPressTab(_ sender: Any)
{
scrollView.setContentOffset(CGPoint(x: scrollView.frame.size.width * 3, y: 0) , animated: true)
}
@IBAction func sniperPressTab(_ sender: Any)
{
scrollView.setContentOffset(CGPoint(x: scrollView.frame.size.width * 4, y: 0) , animated: true)
}
@IBAction func didPressTab(_ sender: UIButton)
{
let newx = sender.frame.origin.x
UIView.animate(withDuration: 0.2)
{
self.movingView.frame.origin.x = newx
}
}
}
我已經試過這個,但是當我跑我的應用程序,沒有任何反應,當我滾動
func scrollViewDidScroll(_ scrollView: UIScrollView) {
//Assuming your views are of full screen width or just change the SCREEN_WIDTH to whatever width your views occupy
//This will give you which index you are on, i.e you are showing the first view or second or third
let newIndex = scrollView.contentOffset.x/UIScreen.main.bounds.size.width;
animateUnderBar(forIndex index:Int(newIndex))
}
func animateUnderBar(forIndex index:Int){
let newx = self.movingView.frame.origin.x + CGFloat(index) * self.movingView.bounds.size.width
UIView.animate(withDuration: 0.2)
{
self.movingView.frame.origin.x = newx
}
}
謝謝你的回答。我得到這個錯誤:'Binary operator'*'不能應用於'Int'和'CGFloat'類型的操作符:let newx = self.movingView.frame.origin.x + index * self.movingView。 bounds.size.width –
Ahh忽略了,只是'CGFloat(index)' – Rikh
我編輯了我的問題。我將CGFloats轉換爲Int,現在我沒有錯誤。但是當我滾動到另一個視圖時,水平條不會移動 –