容器视图的分段控件

时间:2018-11-11 07:10:03

标签: swift

我使用了三个容器视图,并将它们链接到代码中的分段控件,但是过渡仅在第一个视图和第二个视图中起作用

import UIKit

class ViewController: UIViewController
{
    @IBOutlet weak var parts: UIView!
    @IBOutlet weak var morelike: UIView!
    @IBOutlet weak var addlater: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    @IBAction func transction(_ sender: UISegmentedControl)
    {
        if (sender.selectedSegmentIndex == 0)
        {
            self.addlater.alpha = 0.0
            self.morelike.alpha = 1.0
            self.parts.alpha = 2.0
        }
        else
        {
            self.addlater.alpha = 2.0
            self.morelike.alpha = 1.0
            self.parts.alpha = 0.0
        }
    }
}

enter image description here

1 个答案:

答案 0 :(得分:0)

如果您希望每个细分都有一个可见的视图,则可以在过渡操作中使用它:

switch sender.selectedSegmentIndex {
    case 0:
        self.addlater.alpha = 0.0
        self.morelike.alpha = 0.0
        self.parts.alpha = 1.0
    case 1:
        self.addlater.alpha = 0.0
        self.morelike.alpha = 1.0
        self.parts.alpha = 0.0
    case 2:
        self.addlater.alpha = 1.0
        self.morelike.alpha = 0.0
        self.parts.alpha = 0.0
    default:
        break
}

或更清晰的方法:

switch sender.selectedSegmentIndex {
    case 0:
        addlater.isHidden = true
        morelike.isHidden = true
        parts.isHidden = false
    case 1:
        addlater.isHidden = true
        morelike.isHidden = false
        parts.isHidden = true
    case 2:
        addlater.isHidden = false
        morelike.isHidden = true
        parts.isHidden = true
    default:
        break
    }