国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

iOS 生成圖片驗證碼(實用功能)

2019-10-21 18:47:58
字體:
供稿:網(wǎng)友

1.數(shù)據(jù)源

codeArray = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]

2.隨機生成驗證碼

for index in 0..<4 {      let i = (Int)(arc4random()) % (codeArray.count-1)      let str = codeArray[i]      if index == 0 {        codeStr = str as NSString      }else {        codeStr = "/(codeStr)/(str)" as NSString      }    }

3.創(chuàng)建放置驗證碼的label

for i in 0..<codeStr.length {      let pxx = arc4random() % UInt32(randWidth)      let px = CGFloat(pxx) + CGFloat(i)*(self.frame.size.width-3)/CGFloat(codeStr.length)      let py = arc4random() % UInt32(randHeight)      let label = UILabel.init(frame: CGRect.init(x: px + 3, y: CGFloat(py), width: textSize.width, height: textSize.height))      label.text = String.init(format: "%C", codeStr.character(at: i))      label.font = UIFont.systemFont(ofSize: 20)      if (isRotation == true) {        let a = arc4random() % UInt32(60)        let r = Double(a)-30        label.transform = CGAffineTransform.init(rotationAngle: CGFloat(r*M_PI)/180)      }      bgView.addSubview(label)    }

4.底部view是隨便畫幾條細線

for _ in 0...9 {      let path = UIBezierPath.init()      let pX = arc4random() % UInt32(self.frame.width)      let pY = arc4random() % UInt32(self.frame.height)      path.move(to: CGPoint.init(x: CGFloat(pX), y: CGFloat(pY)))      let ptX = arc4random() % UInt32(self.frame.width)      let ptY = arc4random() % UInt32(self.frame.height)      path.addLine(to: CGPoint.init(x: CGFloat(ptX), y: CGFloat(ptY)))      let layer = CAShapeLayer.init()      layer.strokeColor = UIColor.getRandomColor(withAlpha: 0.2).cgColor//layer的邊框色      layer.lineWidth = 1.0      layer.strokeEnd = 1      layer.fillColor = UIColor.clear.cgColor      layer.path = path.cgPath      bgView.layer.addSublayer(layer)    }

5.調(diào)用方法及回調(diào)

public func refreshCode(callBack: @escaping (_ str: NSString) -> ()) {    initCodeView()    callBack(codeStr)  }

6.效果圖

ios,生成圖片驗證碼,驗證碼

7.源碼地址

以上所述是小編給大家介紹的iOS 生成圖片驗證碼(實用功能),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對VEVB武林網(wǎng)網(wǎng)站的支持!


注:相關(guān)教程知識閱讀請移步到IOS開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 合江县| 洞口县| 霍城县| 改则县| 内丘县| 漳浦县| 贺州市| 衡阳县| 山东省| 阿克陶县| 郎溪县| 宜城市| 辰溪县| 云阳县| 临安市| 喀喇沁旗| 当阳市| 库尔勒市| 南澳县| 两当县| 凤城市| 吴堡县| 鹰潭市| 大渡口区| 镇江市| 绩溪县| 夹江县| 遂宁市| 阳朔县| 耒阳市| 麻城市| 双辽市| 新闻| 渝北区| 鄱阳县| 南皮县| 射阳县| 锦州市| 无锡市| 二连浩特市| 固原市|