swift - 适合新手的下拉展开,收缩功能

//

//  ViewController.swift

//  demo

//

//  Created by 刘波 on 2017/8/9.

//  Copyright © 2017 Liu. All rights reserved.

//


import UIKit


class asdViewController: UIViewController {

    var flag = true

    var view1: UIView!

    var view2: UIView!

    let width = UIScreen.main.bounds.width

    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

        view.backgroundColor = UIColor.white

        

        

        

        

        view1 = UIView(frame: CGRect(x: 0, y: 74, width: width, height:100))

        view1.backgroundColor = UIColor.red

        

        let btn = generateBtn(frame: CGRect(x:0, y: 0, width: self.view.bounds.width, height: 40),tag: 1)

        

        

        let label2 = UILabel(frame: CGRect(x: 0, y: 10, width: 50, height: 20))

        label2.text = "2343234234234"

        

        let tewxt = UITextField(frame: CGRect(x: 0, y: 50, width:  self.view.bounds.width, height: 20))

        tewxt.layer.borderWidth = 1

        

        

        let btn2 = generateBtn(frame: CGRect(x:0, y: 0, width: self.view.bounds.width, height:40),tag: 2)

        

        view2 = UIView(frame: CGRect(x: 0, y: 174, width: width, height:40))

        view2.backgroundColor = UIColor.blue

        

        let label3 = UILabel(frame: CGRect(x: 0, y: 10, width: 50, height: 20))

        label3.text = "23234"

        

        view1.addSubview(label2)

        view1.addSubview(tewxt)

        view2.addSubview(label3)

        

        view1.addSubview(btn)

        self.view.addSubview(view1)

        

        view2.addSubview(btn2)

        self.view.addSubview(view2)

    }

    func generateBtn(frame:CGRect,tag:Int) ->UIButton{

        let btn = UIButton(type: .custom)

        btn.frame = frame

        btn.layer.borderWidth = 1

        btn.backgroundColor = UIColor.white

        btn.tag = tag

        btn.layer.borderColor = UIColor( red:0, green:0, blue:0, alpha:0.2 ).cgColor

        btn.addTarget(self, action: #selector(btn_click(_:)), for: .touchUpInside)

        let imag = UIImageView(image:UIImage(named:"customer_0001_-2"))

        imag.frame = CGRect(x:0, y:20, width:15, height:0)

        imag.frame.origin.x = self.view.bounds.width - imag.frame.width-20

        imag.contentModeUIViewContentMode.scaleAspectFill

        btn.addSubview(imag)

        

        

        return btn

    }

    func btn_click(_ sender: UIButton){

        if sender.tag == 1 {

            //self.view1.isHidden = !flag

            if flag{

                self.view1.frame = CGRect(x: 0, y: 74, width: width, height: 40)

                self.view2.frame = CGRect(x: 0, y: 124, width: width, height: 40)

            }else{

                self.view1.frame = CGRect(x: 0, y: 74, width: width, height: 100)

                self.view2.frame = CGRect(x: 0, y: 174, width: width, height: 40)

            }

        }

        if sender.tag == 2 {

            if flag{

                self.view1.frame = CGRect(x: 0, y: 74, width: width, height: 40)

                self.view2.frame = CGRect(x: 0, y: 124, width: width, height:100)

            }else{

                self.view1.frame = CGRect(x: 0, y:74, width: width, height: 40)

                self.view2.frame = CGRect(x: 0, y: 124, width: width, height:40)

            }

        }

        

        flag = !flag

       // btn.isSelected = !btn.isSelected

    }

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

    

    

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值