封装 带发送验证码的TextField

2017-04-12  本文已影响0人  山已几孑

需求:封装一个获取验证码的按钮像这样:

获取验证码
            let imageView = UIImageView(image: image)
            leftViewMode = UITextFieldViewMode.always

            rightView = UIButton(60)
            rightView?.frame = CGRect(x: 0, y: -10, width: 100, height: 30)
            rightView?.layer.cornerRadius = 6
            rightView?.layer.masksToBounds = true
         
            rightView = rightView
            rightViewMode = UITextFieldViewMode.always

            let imageR = image?.size
            
            let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: (imageR?.width)! + 20, height: (imageR?.height)!))
            imageView.image = image
            imageView.contentMode = UIViewContentMode.scaleAspectFit
            
            leftView = imageView
            leftViewMode = UITextFieldViewMode.always
    override func rightViewRect(forBounds bounds: CGRect) -> CGRect {
        var rect = super.rightViewRect(forBounds: bounds)
        rect.origin.x += -10
        return rect
    }

// 一发不可收拾,接着改了后面这些
    override func leftViewRect(forBounds bounds: CGRect) -> CGRect {
        var rect = super.leftViewRect(forBounds: bounds)
        rect.origin.x += 10
        return rect
    }
    override func editingRect(forBounds bounds: CGRect) -> CGRect
    {
        var rect = super.editingRect(forBounds: bounds)
        rect.origin.x += 10
        rect.size.width -= 20
        return rect
    }
    override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
        var rect = super.editingRect(forBounds: bounds)
        rect.origin.x += 10
        rect.size.width -= 20
        return rect
    }

最终效果实现,简单功能就没代码了,关键是下面的🕳️!

下面说说坑

-swift3.1 xcode8.3
本来计划不是用继承, 而是打算用***extention UITextField{} ***

但是在重写swift中func rightViewRect(forBounds bounds: CGRect) -> CGRect方法的时候,结果~boom!

报错了

method 'rightViewRectForBounds' with Objective-C selector 'rightViewRectForBounds:' conflicts with method 'rightViewRect(forBounds:)' with the same Objective-C selector

查过资料,但是不太明白

如果谁知道解决办法,或者为什么不行,还劳烦通知一下~

按钮是个extention,只是添加了一个方法没有遇到重写的坑,关键代码如下,里面有个dispathSourceTimer,网上资料也是各个版本都有,我的就也贴一下,有问题,找百度

func timeLess() {
        var _timeout: Int = tag
        
        let _queue = DispatchQueue.global()
        let _timer = DispatchSource.makeTimerSource(queue: _queue)

        //创建计时器
        _timer.scheduleRepeating(deadline: DispatchTime.init(uptimeNanoseconds: UInt64(_timeout)), interval: DispatchTimeInterval.seconds(1))
        _timer.setEventHandler { 
            //
            if _timeout <= 0 {
                // 倒计时结束
                _timer.cancel()
                DispatchQueue.main.async {
                    // 如需更新UI 代码请写在这里
                    self.isEnabled = true
                    self.setTitle("重新获取", for: UIControlState.normal)
                    self.backgroundColor = UIColor.orange
                }
            } else {
                
                print(_timeout)
                _timeout -= 1
                
                DispatchQueue.main.async {
                    // 如需更新UI 代码请写在这里
                    self.setTitle("已发送:\(_timeout)", for: UIControlState.normal)
                    self.backgroundColor = UIColor.lightGray
                    
                }
            }
        }
        _timer.resume()
    }

上一篇下一篇

猜你喜欢

热点阅读