控制3分段



我使用的是Xcode 8.1, swift 3.0

我有代码分段它的工作与2控制

当我尝试添加3它不工作任何想法是什么错了我的代码

这一个工作与2分段

@IBAction func showComponent(_ sender: UISegmentedControl) {
   if sender.selectedSegmentIndex == 0 {
      UIView.animate(withDuration: 0.0, animations: {
         self.containerViewA.alpha = 1
         self.containerViewB.alpha = 0
      })     
   } else {
      UIView.animate(withDuration: 0.0, animations: {
         self.containerViewA.alpha = 0
         self.containerViewB.alpha = 1
      })
   } 
} 

这个不能用3分段

在这里我尝试新的代码与3x分段控制;

@IBAction func showComponent(_ sender: UISegmentedControl) {
   if sender.selectedSegmentIndex == 0 {
      UIView.animate(withDuration: 0.5, animations: {
         self.containerViewA.alpha = 1
         self.containerViewB.alpha = 0
         self.containerViewC.alpha = 0
      })
      UIView.animate(withDuration: 0.5, animations: {
         self.containerViewA.alpha = 0
         self.containerViewB.alpha = 1
         self.containerViewC.alpha = 0
   } else {     
      UIView.animate(withDuration: 0.5, animations: {
         self.containerViewA.alpha = 0
         self.containerViewB.alpha = 0
         self.containerViewC.alpha = 1
      })
   } 
} 

我正在尝试使用这段代码,它现在的工作:)

import UIKit
class ViewController: UIViewController {
    @IBOutlet weak var orange: UIView!
    @IBOutlet weak var yellow: UIView!
    @IBOutlet weak var red: UIView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    @IBAction func showComponent(_ sender: UISegmentedControl) {
        if sender.selectedSegmentIndex == 0 {
                self.orange.alpha = 1
              self.yellow.alpha = 0
                self.red.alpha = 0
        }
              if sender.selectedSegmentIndex == 1 {
                self.orange.alpha = 0
                 self.yellow.alpha = 1
               self.red.alpha = 0
        }
                if sender.selectedSegmentIndex == 2 {
               self.orange.alpha = 0
                self.yellow.alpha = 0
                self.red.alpha = 1
          }
     }
}

相关内容

  • 没有找到相关文章

最新更新