IOS,Swift:创建一个包含2个视图的分屏,带有动画

时间:2016-08-16 16:50:17

标签: ios swift

我是研究Swift的绝对IOS新手。

我尝试在顶部创建一个带有日历UI控件的应用程序(已经有效),并在底部创建一个列表视图。

完全像底部的图片(我为巨大的照片道歉)。

现在,与日历应用程序一样,屏幕仅显示日历控件,但如果单击按钮 - 列表视图将在底部显示,屏幕将分割。

我能想到这样做的唯一方法是在代码中生成UITableView,将当前的ViewController指定为委托和数据源,并为视图(日历和列表视图)设置动画以增大/缩小以分割屏幕。 / p>

我不想使用StackView,因为那是IOS-9及以上。

有更好的方法吗?

enter image description here

1 个答案:

答案 0 :(得分:2)

我怀疑你正试图做这样的事情。希望它有所帮助:

Swift 2:

let view2 = UIView()

var screenWidth = CGFloat()
var screenHeight = CGFloat()

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

    let screenSize: CGRect = UIScreen.mainScreen().bounds
    screenWidth = screenSize.width
    screenHeight = screenSize.height

    let view1 = UIView()
    view1.frame = CGRectMake(0, 0, screenWidth, screenWidth)
    view1.backgroundColor = UIColor.blueColor().colorWithAlphaComponent(0.5)
    self.view.addSubview(view1)

    let lbl1 = UILabel()
    lbl1.frame = CGRectMake(0, 0, screenWidth, 50)
    lbl1.text = "This is view 1"
    lbl1.textAlignment = NSTextAlignment.Center
    lbl1.center = view1.center
    view1.addSubview(lbl1)

    let btn = UIButton()
    btn.frame = CGRectMake(0,0,200,100)
    btn.center = CGPoint(x: view1.center.x, y: view1.center.y+60)
    btn.setTitle("Show view 2", forState: .Normal)
    btn.addTarget(self, action: "showView2:", forControlEvents: .TouchUpInside)
    view1.addSubview(btn)


    view2.frame = CGRectMake(0, screenHeight, screenWidth, screenHeight-screenWidth)
    view2.backgroundColor = UIColor.redColor().colorWithAlphaComponent(0.5)
    self.view.addSubview(view2)

    let lbl2 = UILabel()
    lbl2.frame = CGRectMake(0, (screenHeight-screenWidth)/2-25, screenWidth, 50)
    lbl2.text = "This is view 2"
    lbl2.textAlignment = NSTextAlignment.Center
    view2.addSubview(lbl2)

}

var c = 0

func showView2(sender : UIButton) {

    UIView.animateWithDuration(1.0, delay: 0.0, options: .CurveEaseOut, animations: {

        if (self.c%2 == 0) {
            self.view2.frame = CGRectMake(0, self.screenWidth, self.screenWidth, self.screenHeight-self.screenWidth)
            sender.setTitle("Hide view 2", forState: .Normal)
        }
        else {
            self.view2.frame = CGRectMake(0, self.screenHeight, self.screenWidth, self.screenHeight-self.screenWidth)
            sender.setTitle("Show view 2", forState: .Normal)
        }

        }, completion: { finished in

    })

    c++

}

斯威夫特3:

let view2 = UIView()

var screenWidth = CGFloat()
var screenHeight = CGFloat()

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

    let screenSize: CGRect = UIScreen.main.bounds
    screenWidth = screenSize.width
    screenHeight = screenSize.height

    let view1 = UIView()
    view1.frame = CGRect(x: 0, y: 0, width: screenWidth, height: screenWidth)
    view1.backgroundColor = UIColor.blue.withAlphaComponent(0.5)
    self.view.addSubview(view1)

    let lbl1 = UILabel()
    lbl1.frame = CGRect(x: 0, y: 0, width: screenWidth, height: 50)
    lbl1.text = "This is view 1"
    lbl1.textAlignment = NSTextAlignment.center
    lbl1.center = view1.center
    view1.addSubview(lbl1)

    let btn = UIButton()
    btn.frame = CGRect(x: 0, y: 0, width: 200, height: 100)
    btn.center = CGPoint(x: view1.center.x, y: view1.center.y+60)
    btn.setTitle("Show view 2", for: .normal)
    btn.addTarget(self, action: #selector(showView2(_:)), for: .touchUpInside)
    view1.addSubview(btn)


    view2.frame = CGRect(x: 0, y: screenHeight, width: screenWidth, height: screenHeight-screenWidth)
    view2.backgroundColor = UIColor.red.withAlphaComponent(0.5)
    self.view.addSubview(view2)

    let lbl2 = UILabel()
    lbl2.frame = CGRect(x: 0, y: (screenHeight-screenWidth)/2-25, width: screenWidth, height: 50)
    lbl2.text = "This is view 2"
    lbl2.textAlignment = NSTextAlignment.center
    view2.addSubview(lbl2)

}

var c = 0

func showView2(_ sender : UIButton) {

    UIView.animate(withDuration: 1.0, delay: 0.0, options: .curveEaseOut, animations: {

        if (self.c%2 == 0) {
            self.view2.frame = CGRect(x: 0, y: self.screenWidth, width: self.screenWidth, height: self.screenHeight-self.screenWidth)
            sender.setTitle("Hide view 2", for: .normal)
        }
        else {
            self.view2.frame = CGRect(x: 0, y: self.screenHeight, width: self.screenWidth, height: self.screenHeight-self.screenWidth)
            sender.setTitle("Show view 2", for: .normal)
        }

    }, completion: { finished in

    })

    c += 1

}