swift UI控件的综合应用

来源:互联网 发布:女生必知防身方法 编辑:程序博客网 时间:2024/05/22 07:56

import UIKit


enum AddTimerType :Int{

   case onTime = 0

   case offTime = 1

   case onAndOffTime = 2

}


class TTAddTimerViewController:UIViewController,TTInterceptorProtocol,UITableViewDataSource,UITableViewDelegate,TTSelectTableViewCellDelegate {


   var addTimeType:AddTimerType!

   var lblOnTime:UILabel!

   var lblOnTimeTip:UILabel!

   var lblOffTime:UILabel!

   var lblOffTimeTip:UILabel!

   var btnOntime:UIButton!

   var btnOfftime:UIButton!

   var datePicker:UIDatePicker!

   var offdatePicker:UIDatePicker!

   var scrollView:UIScrollView!

   var clickCount :Int!

   var repeatView :UIView!

   var weekButtonArr = NSMutableArray()

   var showSelectWeekArr = NSMutableArray()

   var lblSelectWeek:UILabel!

   var timeSelectView:UIView!

   var offSelectView:UIView!


   var isShowDateTime:Bool =false

   var isShowSection2DateTime:Bool =false

    

   var currentDate:String?

    

   var tableView:UITableView!

    

   override func viewDidLoad() {

       super.viewDidLoad()

        self.view.backgroundColor =TTColor("#f2f2f2")

        self.navigationItem.title ="Add List"

        

       var dateFormatter = NSDateFormatter()

        dateFormatter.dateFormat ="HH:mm"

       self.currentDate = dateFormatter.stringFromDate(NSDate())

        

        self.tableView =UITableView(frame: self.view.bounds, style:UITableViewStyle.Plain)

       self.tableView.autoresizingMask = .FlexibleHeight | .FlexibleWidth

       self.tableView.delegate =self

       self.tableView.dataSource =self

       self.view.addSubview(self.tableView)

        

       self.tableView.tableFooterView =footerView()

        

    }


    //MARK:-  DataSource

   func numberOfSectionsInTableView(tableView: UITableView) -> Int {

       if self.addTimeType== .onAndOffTime{

           return 2

        }

       return 1

    }

    

   func tableView(tableView: UITableView, numberOfRowsInSection section: Int) ->Int {

       if section == 0{

           return (self.isShowDateTime ==true) ? 1 :0

        }else

        {

           return (self.isShowSection2DateTime ==true) ? 1:0

        }

        

    }

    

   func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        

        let cell =TTSelectTableViewCell()

        cell.delegate =self

       return cell

    }

    

    //MARK:- delegate

    

   func tableView(tableView: UITableView, viewForHeaderInSection section: Int) ->UIView? {

       var headView = UIView(frame:CGRectMake(0,0, tableView.frame.size.width, tableView.frame.size.height))

       let titleLabel = UILabel(frame:CGRectMake(10,0, 100, 50))

            titleLabel.font =UIFont.boldSystemFontOfSize(18)

            titleLabel.textColor =tableViewCellDefaultTextColor

            titleLabel.text = section ==0 ? "On time" :"Off time"

            headView.addSubview(titleLabel)

       if section == 0{

           if self.btnOntime ==nil{

               self.btnOntime =UIButton(frame: CGRectMake(headView.frame.size.width -100,0, 100, 50))

               self.btnOntime.titleLabel?.font =UIFont.systemFontOfSize(14)

               self.btnOntime.setTitle(self.currentDate, forState:UIControlState.Normal)

               self.btnOntime.imageEdgeInsets =UIEdgeInsetsMake(0,80,0, 0)

               self.btnOntime.tag =10086 + section

               self.btnOntime.setImage(UIImage(named:"smartmodel_nextBlue"), forState: UIControlState.Normal)

               self.btnOntime.setImage(UIImage(named:"smartmodel_downBlue"), forState: UIControlState.Selected)

               self.btnOntime.setTitleColor(appThemeColor, forState:UIControlState.Normal)

               self.btnOntime.autoresizingMask =UIViewAutoresizing.FlexibleTopMargin | UIViewAutoresizing.FlexibleBottomMargin

               self.btnOntime.addTarget(self, action:"chooseTimeAction:", forControlEvents: UIControlEvents.TouchUpInside)

            }

            headView.addSubview(self.btnOntime)

        }elseif section == 1{

           if self.btnOfftime ==nil{

               self.btnOfftime =UIButton(frame: CGRectMake(headView.frame.size.width -100,0, 100, 50))

               self.btnOfftime.titleLabel?.font =UIFont.systemFontOfSize(14)

               self.btnOfftime.setTitle(self.currentDate, forState:UIControlState.Normal)

               self.btnOfftime.imageEdgeInsets =UIEdgeInsetsMake(0,80,0, 0)

               self.btnOfftime.tag =10086 + section

               self.btnOfftime.setImage(UIImage(named:"smartmodel_nextBlue"), forState: UIControlState.Normal)

               self.btnOfftime.setImage(UIImage(named:"smartmodel_downBlue"), forState: UIControlState.Selected)

               self.btnOfftime.setTitleColor(appThemeColor, forState:UIControlState.Normal)

               self.btnOfftime.autoresizingMask =UIViewAutoresizing.FlexibleTopMargin | UIViewAutoresizing.FlexibleBottomMargin

               self.btnOfftime.addTarget(self, action:"chooseTimeAction:", forControlEvents: UIControlEvents.TouchUpInside)

            }

            headView.addSubview(self.btnOfftime)

        }

        

       let line = UIView(frame:CGRectMake(0,49, headView.bounds.width,1))

        line.backgroundColor =defaultLineColor

        headView.addSubview(line)

        

       return headView

    }

    

   func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

       return 50

    }

    

   func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

       return 212

    }

    

   func footerView() -> UIView{

       var footerView = UIView()

       let footerViewX = CGFloat(0)

       let footerViewY = CGFloat(0)

       let footerViewW = self.view.frame.width

       var footerViewH:CGFloat!

       if self.addTimeType== .onAndOffTime{

            footerViewH =212

        }else{

            

            footerViewH =self.view.frame.height -326

        }

        footerView.frame =CGRectMake( footerViewX, footerViewY, footerViewW, footerViewH)

        

        footerView.backgroundColor =TTColor("#f2f2f2")

       self.repeatView =UIView(frame: CGRectMake(0,0, self.view.frame.size.width,50))

        self.repeatView.backgroundColor =UIColor.whiteColor()

        

       var lblRepeat = UILabel(frame:CGRectMake(16,0,100,50))

        lblRepeat.text ="Repeat"

        lblRepeat.font =UIFont.systemFontOfSize(20)

       repeatView.addSubview(lblRepeat)

        footerView.addSubview(self.repeatView)

        

       self.lblSelectWeek =UILabel(frame: CGRectMake(lblRepeat.frame.width,0, self.repeatView.frame.width - lblRepeat.frame.width - 10, 50))

        self.repeatView.addSubview(self.lblSelectWeek)

        self.lblSelectWeek.textColor =appThemeColor

        self.lblSelectWeek.textAlignment =NSTextAlignment.Right

        

        

        //创建星期的按钮

       let btnWidth = (self.view.frame.size.width -40)/7

        var weekArr = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"]

       let count = weekArr.count

       var weekBtnYAddH = CGFloat?()

       for i in 0..<count {

           var btnWeek = UIButton(frame:CGRectMake(CGFloat(i) * btnWidth +20.0 , CGRectGetMaxY(repeatView.frame)+20, btnWidth, btnWidth))

            btnWeek.backgroundColor =UIColor.whiteColor()

            btnWeek.tag =100 + i;

            //btnWeek.titleLabel?.backgroundColor = UIColor.grayColor()

            btnWeek.setTitle(weekArr[i], forState:UIControlState.Normal)

            btnWeek.setTitleColor(TTColor("#999999"), forState:UIControlState.Normal)

            btnWeek.setTitleColor(appThemeColor, forState:UIControlState.Highlighted)

            btnWeek.addTarget(self, action:"weekBtnAction:", forControlEvents: UIControlEvents.TouchUpInside)

           weekButtonArr.addObject(btnWeek)

            //获取星期按钮的Y + height的值

            weekBtnYAddH = btnWeek.frame.origin.y + btnWeek.frame.size.height;

            

            footerView.addSubview(btnWeek)

        }

        

        //Save 按钮的创建

       var contentH = footerView.frame.height

       var btnSave = UIButton(frame:CGRectMake(0,contentH -46, self.view.frame.size.width,46))

        btnSave.backgroundColor =appThemeColor

        btnSave.setTitle("Save", forState:UIControlState.Normal)

        

        footerView.addSubview(btnSave)

        

       return footerView

    }

    

   override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

    

     //MARK:- TTSelectTableViewCellDelegate

  

   func  datePickerChange(datePicker:UIDatePicker,cell:TTSelectTableViewCell){

       var indexPath = self.tableView.indexPathForCell(cell)

       var dateFormatter = NSDateFormatter()

        dateFormatter.dateFormat ="HH:mm"

       var dateString = dateFormatter.stringFromDate(datePicker.date)

        

       if indexPath?.section ==0{

           self.btnOntime.setTitle(dateString, forState:UIControlState.Normal)

        }else{

           self.btnOfftime.setTitle(dateString, forState:UIControlState.Normal)

        }

    }


    

    //MARK:- Delegate or DataSource

    

    //MARK:- NSNotification Method

    

    //MARK:- Action Method

    

   func chooseTimeAction(sender:UIButton){

       let section = sender.tag -10086

       if section == 0{

           if !sender.selected {

               self.isShowDateTime =true

            }else{

               self.isShowDateTime =false

            }

           self.tableView.reloadSections(NSIndexSet(index:0), withRowAnimation: UITableViewRowAnimation.Automatic)

        }else{

           if !sender.selected {

                self.isShowSection2DateTime =true

            }else{

                self.isShowSection2DateTime =false

            }


           self.tableView.reloadSections(NSIndexSet(index:1), withRowAnimation: UITableViewRowAnimation.Fade)

        }

        sender.selected = !sender.selected

    }

    

   func weekBtnAction(btn:UIButton){

       if btn.selected ==false{

            btn.backgroundColor =appThemeColor

            btn.setTitleColor(TTColor("#ffffff"), forState:UIControlState.Normal)

            btn.selected =true

           if let buttonTitle = btn.titleLabel?.text{

               self.showSelectWeekArr.addObject(buttonTitle)

            }

            

        }else

        {

            btn.backgroundColor =UIColor.whiteColor()

            btn.setTitleColor(TTColor("#999999"), forState:UIControlState.Normal)

            btn.selected =false

           if let buttonTitle = btn.titleLabel?.text{

               if self.showSelectWeekArr.containsObject(buttonTitle){

                   self.showSelectWeekArr.removeObject(buttonTitle)

                }

            }

           

        }

       let seletWeekTitle = self.showSelectWeekArr.componentsJoinedByString(" ")

       self.lblSelectWeek.text = seletWeekTitle

    }

    //MARK:- Private Method


}


0 0