美文网首页SwiftiOS进阶指南iOS开发
Swift 2.0 键盘遮挡自定义View 继承View即可实现

Swift 2.0 键盘遮挡自定义View 继承View即可实现

作者: xida0325 | 来源:发表于2015-12-05 14:11 被阅读347次

    //

    //XDView.swift

    //Tiger

    //

    //Created by admin on 15/12/3.

    //Copyright © 2015年xidaMM. All rights reserved.

    //

    importUIKit

    classXDView:UIView{

    varkoffset:CGFloat!

    varisDown:Bool? =nil

    overrideinit(frame:CGRect) {

    super.init(frame: frame)

    setUp()

    print("1")

    }

    requiredinit?(coder aDecoder:NSCoder) {

    super.init(coder: aDecoder)

    //setUp()

    print("2")

    }

    overridefuncawakeFromNib() {

    super.awakeFromNib()

    setUp()

    print("3")

    }

    funcsetUp(){

    koffset=0

    NSNotificationCenter.defaultCenter().addObserver(self, selector:"keyboardWillShow:", name:UIKeyboardWillShowNotification, object:nil)

    NSNotificationCenter.defaultCenter().addObserver(self, selector:"keyboardWillHide:", name:UIKeyboardWillHideNotification, object:nil)

    //

    }

    //MARK:键盘显示和隐藏

    funckeyboardWillShow(notificate:NSNotification) {

    ifisDown==false{

    return

    }

    //获取键盘高度,在不同设备上,以及中英文下是不同的

    letkeyHeight = notificate.userInfo![UIKeyboardFrameEndUserInfoKey]?.CGRectValue.origin.y

    letfirstResponder =self.findFirstResponceView(self)as?UITextField

    guardfirstResponder !=nilelse{

    return

    }

    firstResponder!.delegate=self

    //根据当前的第一响应者的frame获取相对于self.view的frame

    letrectWithView = firstResponder?.convertRect((firstResponder?.bounds)!, toView:self)

    //计算出键盘顶端到inputTextView panel底端的距离(加上自定义的缓冲距离INTERVAL_KEYBOARD)

    print(rectWithView)

    koffset= keyHeight! - (rectWithView!.origin.y

    + (firstResponder?.frame.size.height)!)

    //取得键盘的动画时间,这样可以在视图上移的时候更连贯

    letduration = notificate.userInfo![UIKeyboardAnimationDurationUserInfoKey]?.doubleValue

    ifkoffset<0{

    UIView.animateWithDuration(duration!, animations: { () ->Voidin

    self.frame=CGRectMake(0,self.frame.origin.y+self.koffset,self.frame.size.width,self.frame.size.height)

    self.isDown=false

    })

    }

    }

    funcfindFirstResponceView(myView:UIView) ->UIView? {

    ifmyView.isFirstResponder() {

    returnmyView

    }

    forsubViewinmyView.subviews{

    letresult =self.findFirstResponceView(subView)

    if(result !=nil) {

    returnresult

    }

    }

    returnnil

    }

    funckeyboardWillHide(notificate:NSNotification) {

    //取得键盘的动画时间,这样可以在视图上移的时候更连贯

    letduration = notificate.userInfo![UIKeyboardAnimationDurationUserInfoKey]?.doubleValue

    ifkoffset<0{

    UIView.animateWithDuration(duration!, animations: { () ->Voidin

    self.frame=CGRectMake(0,self.frame.origin.y-self.koffset,self.frame.size.width,self.frame.size.height)

    self.isDown=true

    })

    }

    }

    deinit{

    NSNotificationCenter.defaultCenter().removeObserver(self)

    }

    /*

    // Only override drawRect: if you perform custom drawing.

    // An empty implementation adversely affects performance during animation.

    override func drawRect(rect: CGRect) {

    // Drawing code

    }

    */

    }

    相关文章

      网友评论

        本文标题:Swift 2.0 键盘遮挡自定义View 继承View即可实现

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