封装

作者: 鱼得琴 | 来源:发表于2017-01-12 10:18 被阅读0次

    Button:

    fun GenearateBtn(title:String,titleColor:UIColor,frame:CGRect,backgroundColor:UIColor,action:Selector){
        let Btn = UIButton(frame: frame)
        Btn.setTitle(title, for: .normal)
        Btn.setTitleColor(titleColor, for: .normal)
        Btn.backgroundColor = backgroundColor
        Btn.addTarget(self, action: action, for: .touchUpInside)
        view.addSubview(Btn)
    }
    

    Banner轮播(CollectionView):

    class Banner: UIView{
    
    var CollectionView: UICollectionView?
    var flowlayout = UICollectionViewFlowLayout()
    var pageControl = UIPageControl()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.createSubviews(frame: frame)
    }
    
    func createSubviews(frame: CGRect){
        CollectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height)
            , collectionViewLayout: flowlayout)
        flowlayout.itemSize = CGSize(width:frame.size.width, height:frame.size.height)
        flowlayout.minimumInteritemSpacing = 0;
        flowlayout.minimumLineSpacing = 0;
        flowlayout.scrollDirection = UICollectionViewScrollDirection.horizontal
        CollectionView!.backgroundColor = UIColor.lightGray
        CollectionView!.isPagingEnabled = true
        CollectionView!.dataSource  = self
        CollectionView!.delegate = self
        CollectionView!.showsHorizontalScrollIndicator = false
        CollectionView!.showsVerticalScrollIndicator = false
        CollectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
        self.addSubview(CollectionView!)
        pageControl = UIPageControl.init(frame: CGRect(x:0,y: 0,width:frame.size.width / 2,height:30))
        pageControl.center = CGPoint(x:frame.size.width / 2, y:frame.size.height - 20);
        self.addSubview(pageControl);
        
        self.addTimer()
    }
    
    func addTimer(){
        let timer1 = Timer.init(timeInterval: 5, target: self, selector: #selector(nextPageView), userInfo: nil, repeats: true)
        RunLoop.current.add(timer1, forMode: RunLoopMode.commonModes)
        timer = timer1
    }
    
    func nextPageView(){
        
        let indexPath = self.CollectionView!.indexPathsForVisibleItems.last
        var item = (indexPath?.item)! + 1;
        var section = indexPath!.section;
        //滑动到最后一个item的时候,下一个变为下一Section的第一个
        if item == images.count {
            item = 0
            section += 1
        }
        self.pageControl.currentPage = item;
        let nextIndexPath = IndexPath(row: item, section: section)
        CollectionView!.scrollToItem(at: nextIndexPath, at: UICollectionViewScrollPosition.left, animated: true)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    }
    
    extension Banner:UICollectionViewDelegate, UICollectionViewDataSource{
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = CollectionView?.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
        let colorArr = [UIColor.blue,UIColor.black,UIColor.cyan]
        cell?.backgroundColor = colorArr[indexPath.row]
        return cell!
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 99999
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 3
    }
    
    }
    

    Button Section:

    //产生按钮区域
    func setupButtonView(){
        let View = UIView()
        View.width = SCREEN_WIDTH
    
        //ButtonView的起始Y
        View.frame.origin.y = 150
    
        self.view.addSubview(View)
        let styleLabel = setStyleLabel("按钮Section")
        View.addSubview(styleLabel)
        //测试数组,按钮的名字
        let nameGroup = ["哈哈","哈哈","哈哈","哈哈","哈哈","哈哈","哈哈"]
        for index in 0 ..< nameGroup.count {
            let button = setupButton(CGFloat(index),inGroup: nameGroup  )
            
            if index == nameGroup.count - 1{
                View.height = button.frame.maxY + 10
            }
            View.addSubview(button)
            ViewY = View.frame.maxY
        }
        view.addSubview(View)
    }
    //按钮区域的名字Label
    func setStyleLabel(_ labelName:String) -> UILabel{
        let styleLabel = UILabel(frame: CGRect(x: 5, y: 5, width: SCREEN_WIDTH - 10, height: 10))
        styleLabel.text = labelName
        return styleLabel
    }
    //产生按钮
    func setupButton(_ index:CGFloat,inGroup:[String]) -> circleButton{
        let buttonWH = SCREEN_WIDTH / 4
        let button = circleButton(frame: CGRect(x: index.truncatingRemainder(dividingBy: 4) * SCREEN_WIDTH/4 , y: 20, width: buttonWH, height: buttonWH))
        button.setImage(UIImage(named:"test"), for: .normal)
        if index > 3 {
            button.frame.origin.y = buttonWH + 20
        }
        button.setTitle("按钮", for: .normal)
        return button
    }
    

    Button Section里的circleButton:

    import UIKit
    
    class circleButton: UIButton {
    
    override func layoutSubviews() {
        super.layoutSubviews()
        imageView?.frame.origin.x = 10
        imageView?.width = self.width - 20
        imageView?.height = (imageView?.width)!
        imageView?.layer.cornerRadius = (self.width - 20) / 2
        imageView?.layer.masksToBounds = true
        
        titleLabel?.frame.origin.x = 0
        titleLabel?.frame.origin.y = imageView!.height
        titleLabel?.width = self.width
        titleLabel?.height = self.height - self.titleLabel!.frame.origin.y
        titleLabel?.textAlignment = .center
        titleLabel?.font = UIFont.systemFont(ofSize: 12)
        titleLabel?.textColor = UIColor.black
    }
    
    }
    

    输入框随键盘升起

    import UIKit
    
    class ViewController: UIViewController {
    
    let SCREENW = UIScreen.main.bounds.width
    let SCREENH = UIScreen.main.bounds.height
    
    var commentBtn:UIButton!
    var inputLineView:UIView!
    var inputTextView:UITextView!
    var layView:UIView?
    
    var changeY:CGFloat!
    var keyboardDuration:Double!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //注册键盘的监听
        let notiCenter =  NotificationCenter.default
        notiCenter.addObserver(self, selector: #selector(keyBoardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        notiCenter.addObserver(self, selector: #selector(keyBoardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
        //评论按钮
        commentBtn = UIButton(frame: CGRect(x: 100, y: 100 , width: 100, height: 100))
        commentBtn.setTitle("Comment", for: .normal)
        commentBtn.setTitleColor(UIColor.white, for: .normal)
        commentBtn.backgroundColor = UIColor.blue
        commentBtn.addTarget(self, action: #selector(CommentBtnClick), for: .touchUpInside)
        view.addSubview(commentBtn)
      
    }
    
    func keyBoardWillShow(notifction:NSNotification){
        let kbInfo = notifction.userInfo
        //获取键盘的size
        let kbRect = (kbInfo?[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
        //键盘的y偏移量
        changeY = kbRect.origin.y - SCREENH
        
        //键盘弹出的时间
        keyboardDuration = kbInfo?[UIKeyboardAnimationDurationUserInfoKey] as! Double
        
        //界面偏移动画
        UIView.animate(withDuration: keyboardDuration) {
            self.inputLineView.transform = CGAffineTransform(translationX: 0, y: self.changeY - self.inputLineView.frame.height)
        }
    }
    
    func keyBoardWillHide(notifction:NSNotification){
        UIView.animate(withDuration: 0.25) {
            self.inputLineView.transform = CGAffineTransform(translationX: 0, y: self.SCREENH)
            self.layView?.isHidden = true
        }
    }
    
    
    func CommentBtnClick(){
    
        if inputLineView == nil{
            inputLineView = UIView(frame: CGRect(x: 0, y: SCREENH , width: SCREENW, height: 50))
            inputLineView.backgroundColor = UIColor.cyan
            view.addSubview(inputLineView)
     
            inputTextView = UITextView(frame: CGRect(x: 3, y: 3, width: SCREENW - 6, height: 44))
            inputTextView.layer.borderColor = UIColor.darkGray.cgColor
            inputTextView.layer.borderWidth = 1
            inputLineView.addSubview(inputTextView)
            inputTextView.becomeFirstResponder()
        } else {
            inputTextView.becomeFirstResponder()
        }
        
        if layView == nil{
            self.layView = UIView(frame: self.view.frame)
            layView?.backgroundColor = UIColor.gray
            layView?.alpha = 0.5
            let tap = UITapGestureRecognizer(target: self, action: #selector(tapLayView))
            layView?.addGestureRecognizer(tap)
            self.view.insertSubview(layView!, belowSubview: inputLineView!)
        } else {
            layView?.isHidden = false
        }
    
    }
    //点击layView,收回键盘
    func tapLayView(){
        self.inputTextView.resignFirstResponder()
    }
    
    }

    相关文章

      网友评论

          本文标题:封装

          本文链接:https://www.haomeiwen.com/subject/widdbttx.html