原文: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()
}
}
}

  

Swift 简简单单实现手机九宫格手势密码解锁的更多相关文章

  1. HTML5 Canvas简简单单实现手机九宫格手势密码解锁

    原文:HTML5 Canvas简简单单实现手机九宫格手势密码解锁 早上花了一个半小时写了一个基于HTML Canvas的手势解锁,主要是为了好玩,可能以后会用到. 思路:根据配置计算出九个点的位置,存 ...

  2. iOS 九宫格手势密码

    代码地址如下:http://www.demodashi.com/demo/11490.html 一.准备工作 需要准备什么环境 xcode,iOS8+ 本例子实现什么功能 主要实现手势密码设置,验证 ...

  3. Appnium+python实现手势密码为什么总是报错

    最近一直在尝试Appnium实现Android手机自动化测试,一直一直卡在一个点上,那就是手势密码,因为所测应用的手势密码使用的不是单个的imageview实现的手势密码解锁窗,所以只能靠坐标点来定位 ...

  4. App自动化(2)--Python&Appium实现安卓手机九宫格解锁

    九宫格作为常见的手势密码,我们在使用的时候,是从起点开始,按住不放,然后滑动手指,直到最后一个点松开手指,如果与设置的手势密码匹配,则解锁成功. 现在大多数九宫格作为一个元素存在,很难定位到每一个点. ...

  5. 支付宝钱包手势密码破解实战(root过的手机可直接绕过手势密码)

    /* 本文章由 莫灰灰 编写,转载请注明出处. 作者:莫灰灰    邮箱: minzhenfei@163.com */ 背景 随着移动互联网的普及以及手机屏幕越做越大等特点,在移动设备上购物.消费已是 ...

  6. [转载]支付宝钱包手势密码破解实战(root过的手机可直接绕过手势密码)

    /* *转自http://blog.csdn.net/hu3167343/article/details/36418063 *本文章由 莫灰灰 编写,转载请注明出处. *作者:莫灰灰    邮箱: m ...

  7. Html5实现手机九宫格password解锁功能

    HTML5真的是非常强大,前端时间看到一个canvas实现九宫格的password解锁. 今天抽空模仿了一个,特定分享一下. 效果截图例如以下: 效果看起来还不错吧! 源代码例如以下: <!DO ...

  8. swift 实现iOS手势密码、指纹密码、faceID

    本博客包含了如何实现iOS手势密码.指纹密码.faceID全步骤,包括了完整的代码. 先附上demo地址https://github.com/Liuyubao/LYBTouchID,支持swift3. ...

  9. Appium-实现手势密码登陆

    前言: 前几天有人问我,手势登陆如何做?于是我找了一个APP试了试,所以本文来总结使用Python+Appium来实现手势密码登陆APP. 环境: MacOS:10.13.4 Appium-deskt ...

随机推荐

  1. 部署Redis主-从

    Redis主-从部署实践 0. 前言 这篇文章简要介绍Redis的主从部署,实现了一主二从,使用两个哨兵监控,以实现简单的HA,其中从库作为备机. 1. 部署 这里有三台服务器,其中239主机上的Re ...

  2. .NET MVC学习笔记(一)

    看了些关于MVC的资料,做一些MVC的笔记. 分解关注点 在MVC世界里有个很重要的观念--"分解关注点"(Separation of Concerns),指的是:当你进行软件开发 ...

  3. 无尽的循环ViewPager

    现在的情况 不改变的源代码,什么时候ViewPager滑动到最后item的时候,他就无法再往右滑动:当ViewPager滑动到第一个item的时候,他也无法再往前滑动. (以上全是废话) 设想 我们能 ...

  4. 策略模式Strategy——坐什么车回家?

    1.存在的问题和模型 :2014年6月       学校:廊坊师范        家:石家庄       人物:学生 又快到期末考试了.回家的节奏也奔上日程,无聊之余就想想这次回家的事儿. 对我来说回 ...

  5. HDU 3065 病毒在继续 (AC自己主动机)

    中国标题不解释 Sample Input 3 AA BB CC ooxxCC%dAAAoen....END   Sample Output AA: 2 CC: 1 输出病毒出现的次数! #includ ...

  6. NUnit3 Test Adapter vs2015

    NUnit的安装 前言:NUnit是什么? NUnit 是一个单元测试框架,专门针对于.NET来写的.NUnit是xUnit家族种的第4个主打产品,完全由C#语言来编写,并且编写时充分利用了许多.NE ...

  7. hexo 部署至Git遇到的坑

    查找资料的时候发现了next这个博客主题,next!非常的漂亮,顺手查看了hexo的相关部署. Hexo官方介绍 Hexo 是一个快速.简洁且高效的博客框架.Hexo 使用 Markdown(或其他渲 ...

  8. 开源服务发现项目Zookeeper,Doozer,Etcd

    这篇文章是Jason Wilder对于常见的服务项目发现Zookeeper.Doozer,Etcd所写的一篇博客,其原文地址例如以下:Open-Source Service Discovery. 服务 ...

  9. Lichee (六) 优化配置的微内核

    我们的分析<Lichee(二) 在sun4i_crane平台下的编译 >的时候.竟然没有一个步骤是在配置内核 make ARCH=arm menuconfig 细致的读过的代码的会发现,在 ...

  10. VS2015, .NET 4.6, C# 6.0, F# 4.0等重量级产品正式上线

    VS2015, .NET 4.6, C# 6.0, F# 4.0等重量级产品正式上线 Visual Studio Visual Studio 2015 下载 VS2015新功能列表 ‘ Visual ...