Swift 简简单单实现手机九宫格手势密码解锁

简介: 原文:Swift 简简单单实现手机九宫格手势密码解锁大家可以看到我之前的文章[HTML5 Canvas简简单单实现手机九宫格手势密码解锁] 本文是使用苹果语言对其进行了移植 颜色配色是拾取的支付宝的颜色 本文的目的说明:语言是想通的  只要思路在 语言只是手段而已 这是本人自学swift一个...
原文: Swift 简简单单实现手机九宫格手势密码解锁

大家可以看到我之前的文章[HTML5 Canvas简简单单实现手机九宫格手势密码解锁

本文是使用苹果语言对其进行了移植 颜色配色是拾取的支付宝的颜色

本文的目的说明:语言是想通的  只要思路在 语言只是手段而已

这是本人自学swift一个礼拜 然后花了三个小时写出来的肯定会有不规范的地方 

因为思路比较简单 大家可以参考 javascript 版本

废话不多说先上效果 

(对了 大家如果能在转载的地方注明出处的话 那就是极好的 http://www.cnblogs.com/zzzzz/p/swift.html  )

 

自定义一个UIView对象,注意需要在启动的controller中实例化这个对象然后给controller附上

  override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.view = NineCellLockView(frame: CGRectZero)
    }

然后是主要的代码UIView:

import UIKit

class NineCellLockView: UIView {
    
    var fingerPoint:CGPoint = CGPoint()
    var linePointointCollection:Array<CGPoint> = Array<CGPoint>()
    var ninePointCollection:Array<CGPoint> = Array<CGPoint>()
    
    var selectPointIndexCollection:Array<Int> = Array<Int>()
    
    
    var circleRadius:CGFloat = 28
    var circleCenterDistance:CGFloat = 96
    var firstCirclePointX:CGFloat = 96
    var firstCirclePointY:CGFloat = 200
    
    func FillNinePointCollection()
    {
        for row in 0...2
        {
            for column in 0...2
            {
                let tempX:CGFloat = CGFloat(column)*self.circleCenterDistance + self.firstCirclePointX
                let tempY:CGFloat = CGFloat(row)*self.circleCenterDistance + self.firstCirclePointY
                self.ninePointCollection.append(CGPoint(x: tempX,y:tempY))
            }
        }
    }
    
    
    func drawCicle(centerPoint:CGPoint,index:Int)
    {
        var context = UIGraphicsGetCurrentContext();
        CGContextSetLineWidth(context, 2.0);
        CGContextAddArc(context, centerPoint.x, centerPoint.y, self.circleRadius, 0.0, CGFloat(M_PI * 2.0), 1)
        let currentIsSelected:Bool = contains(self.selectPointIndexCollection, index)
        if(currentIsSelected)
        {
            //96 169 252
            CGContextSetStrokeColorWithColor(context, UIColor(red: 96/255.0, green: 169/255.0, blue: 252/255.0, alpha: 1).CGColor)
        }else
        {
            
            CGContextSetStrokeColorWithColor(context,  UIColor(red: 144/255.0, green: 149/255.0, blue: 173/255.0, alpha: 1).CGColor)
        }
        CGContextStrokePath(context);
        CGContextAddArc(context, centerPoint.x, centerPoint.y, self.circleRadius, 0.0, CGFloat(M_PI * 2.0), 1)
        CGContextSetFillColorWithColor(context,  UIColor(red: 35/255.0, green: 39/255.0, blue: 54/255.0, alpha: 1).CGColor)
        CGContextFillPath(context)
        if(currentIsSelected)
        {
            CGContextAddArc(context, centerPoint.x, centerPoint.y, 10, 0.0, CGFloat(M_PI * 2.0), 1)
            CGContextSetFillColorWithColor(context, UIColor(red: 96/255.0, green: 169/255.0, blue: 252/255.0, alpha: 1).CGColor)
            CGContextFillPath(context)
        }
    }
    
    func drawNineCircle()
    {
        for p in 0...self.ninePointCollection.count-1
        {
            self.drawCicle(self.ninePointCollection[p],index:p);
        }
        
    }
    
    override init(frame:CGRect)
    {
        super.init(frame:frame)
        //26 29 40
        self.backgroundColor = UIColor(red: 35/255.0, green: 39/255.0, blue: 54/255.0, alpha: 1)
        FillNinePointCollection()
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func DrawLine(p1:CGPoint,p2:CGPoint)
    {
        var bp = UIBezierPath()
        bp.lineWidth  = 3
        bp.lineCapStyle = kCGLineCapRound
        UIColor(red: 96/255.0, green: 169/255.0, blue: 252/255.0, alpha: 1).setStroke()
        bp.moveToPoint(p1)
        bp.addLineToPoint(p2)
        bp.stroke()
        
    }
    
    override func drawRect(rect: CGRect) {
        
        if(self.selectPointIndexCollection.count > 0)
        {
            for index in 0...self.selectPointIndexCollection.count-1
            {
                let nextIndex = index+1
                if(nextIndex <= self.selectPointIndexCollection.count-1)
                {
                    let firstPointIndex=self.selectPointIndexCollection[index]
                    let secondPointIndex=self.selectPointIndexCollection[nextIndex]
                    self.DrawLine(self.ninePointCollection[firstPointIndex],p2:self.ninePointCollection[secondPointIndex])
                }
            }
            if self.fingerPoint.x != -100
            {
                let lastPointIndex=self.selectPointIndexCollection[self.selectPointIndexCollection.count-1]
                self.DrawLine(self.ninePointCollection[lastPointIndex],p2:fingerPoint)
            }
            
        }
        self.drawNineCircle()
    }
    
    
    func distanceBetweenTwoPoint(p1:CGPoint,p2:CGPoint)->CGFloat
    {
        return pow(pow((p1.x-p2.x), 2)+pow((p1.y-p2.y), 2), 0.5)
    }
    
    func CircleIsTouchThenPushInSelectPointIndexCollection(fingerPoint:CGPoint)
    {
        
        for index in 0...self.ninePointCollection.count-1
        {
            if(!contains(self.selectPointIndexCollection, index))
            {
                if(self.distanceBetweenTwoPoint(fingerPoint,p2:self.ninePointCollection[index]) <= circleRadius)
                {
                    self.selectPointIndexCollection.append(index);
                }
            }
        }
        
    }
    
    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        var t = touches.anyObject() as UITouch
        self.selectPointIndexCollection.removeAll(keepCapacity: false)
        self.fingerPoint = t.locationInView(self)
        self.CircleIsTouchThenPushInSelectPointIndexCollection(fingerPoint);
        self.setNeedsDisplay()
    }
    
    override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
        var t = touches.anyObject() as UITouch
        self.fingerPoint = t.locationInView(self)
        
        self.CircleIsTouchThenPushInSelectPointIndexCollection(self.fingerPoint);
        
        self.setNeedsDisplay()
    }
    
    override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
        self.fingerPoint.x = -100
        self.setNeedsDisplay()
        if(self.selectPointIndexCollection.count>0)
        {
            var ReStr:String = ""
            for index in 0...self.selectPointIndexCollection.count-1
            {
                ReStr += String(self.selectPointIndexCollection[index]) + ","
            }
            
            let alertV = UIAlertView(title: "您的结果", message: ReStr, delegate: nil, cancelButtonTitle: "我知道了")
            alertV.show()
        }
    }
}

  

目录
相关文章
|
2月前
|
JavaScript 前端开发 数据安全/隐私保护
vue 前端 邮箱、密码、手机号码等输入验证规则
vue 前端 邮箱、密码、手机号码等输入验证规则
237 0
|
4月前
|
JavaScript 数据安全/隐私保护
jquery正则表达式验证手机号密码和姓名字段
jquery正则表达式验证手机号密码和姓名字段
|
数据安全/隐私保护
手机键盘密码
手机键盘密码
104 0
|
SQL 关系型数据库 MySQL
mysql更新密码字段为手机号后六位前面拼接上Q后面拼接上W然后md5加密
mysql更新密码字段为手机号后六位前面拼接上Q后面拼接上W然后md5加密
300 0
|
前端开发
「CSS畅想」我的发呆专属,反复解锁手机屏幕
前端玩转CSS,可以创造出不少有趣的效果。今天实现了一个解锁手机屏幕的效果,简单的快乐。
157 1
|
安全 小程序 物联网
手机即钥匙 阿里云IoT联合爱玛推出“真无感解锁”智能电动车
5月17日,阿里云IoT联合爱玛打造的首款轻智能电动车悦秀Q166正式在天猫商城“爱玛官方旗舰店”售卖,让用户实现手机即钥匙“真无感解锁,离车即上锁”等全新骑行体验。
598 0
手机即钥匙 阿里云IoT联合爱玛推出“真无感解锁”智能电动车
|
传感器 安全 生物认证
iOS传感器开发——为APP添加手机密码、指纹进行安全验证
iOS传感器开发——为APP添加手机密码、指纹进行安全验证
252 0
iOS传感器开发——为APP添加手机密码、指纹进行安全验证
|
数据安全/隐私保护 Android开发
android 校验用户名密码手机邮箱身份证邮编等
android 校验用户名密码手机邮箱身份证邮编等