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 简简单单实现手机九宫格手势密码解锁的更多相关文章
- HTML5 Canvas简简单单实现手机九宫格手势密码解锁
原文:HTML5 Canvas简简单单实现手机九宫格手势密码解锁 早上花了一个半小时写了一个基于HTML Canvas的手势解锁,主要是为了好玩,可能以后会用到. 思路:根据配置计算出九个点的位置,存 ...
- iOS 九宫格手势密码
代码地址如下:http://www.demodashi.com/demo/11490.html 一.准备工作 需要准备什么环境 xcode,iOS8+ 本例子实现什么功能 主要实现手势密码设置,验证 ...
- Appnium+python实现手势密码为什么总是报错
最近一直在尝试Appnium实现Android手机自动化测试,一直一直卡在一个点上,那就是手势密码,因为所测应用的手势密码使用的不是单个的imageview实现的手势密码解锁窗,所以只能靠坐标点来定位 ...
- App自动化(2)--Python&Appium实现安卓手机九宫格解锁
九宫格作为常见的手势密码,我们在使用的时候,是从起点开始,按住不放,然后滑动手指,直到最后一个点松开手指,如果与设置的手势密码匹配,则解锁成功. 现在大多数九宫格作为一个元素存在,很难定位到每一个点. ...
- 支付宝钱包手势密码破解实战(root过的手机可直接绕过手势密码)
/* 本文章由 莫灰灰 编写,转载请注明出处. 作者:莫灰灰 邮箱: minzhenfei@163.com */ 背景 随着移动互联网的普及以及手机屏幕越做越大等特点,在移动设备上购物.消费已是 ...
- [转载]支付宝钱包手势密码破解实战(root过的手机可直接绕过手势密码)
/* *转自http://blog.csdn.net/hu3167343/article/details/36418063 *本文章由 莫灰灰 编写,转载请注明出处. *作者:莫灰灰 邮箱: m ...
- Html5实现手机九宫格password解锁功能
HTML5真的是非常强大,前端时间看到一个canvas实现九宫格的password解锁. 今天抽空模仿了一个,特定分享一下. 效果截图例如以下: 效果看起来还不错吧! 源代码例如以下: <!DO ...
- swift 实现iOS手势密码、指纹密码、faceID
本博客包含了如何实现iOS手势密码.指纹密码.faceID全步骤,包括了完整的代码. 先附上demo地址https://github.com/Liuyubao/LYBTouchID,支持swift3. ...
- Appium-实现手势密码登陆
前言: 前几天有人问我,手势登陆如何做?于是我找了一个APP试了试,所以本文来总结使用Python+Appium来实现手势密码登陆APP. 环境: MacOS:10.13.4 Appium-deskt ...
随机推荐
- 玩转Web之Jsp(一)-----jsp中的静态包含(<%@include file="url"%>)与动态包含(<jsp:include>)
在jsp中include有两种形式,其中<%@include file="url"%>是指令元素,<jsp:include page="" f ...
- Swing开发界面时的一个bug复盘
问题:QA突然发个截图说一个Dialog上展示的东西变形了 分析:不理解,什么也没做,怎么会变形,刚刚我用的时候还正常.看看代码,的确什么也没更改:在本地测一下,也没有问题:baidu,bing,st ...
- CodeForces 69D Dot (游戏+记忆)
Description Anton and Dasha like to play different games during breaks on checkered paper. By the 11 ...
- LeetCode之Sort List
称号:Sort a linked list in O(n log n) time using constant space complexity. 对一个单链表进行排序,要求时间复杂度为O(n log ...
- MCC460MNC08
因为搜得辛苦,正好也写点关于我morto工作有关的事情,给大家和我一样扫盲,哈哈 The GSM Mobile Country Code (MCC) is different from the i ...
- EF6操作Sqlite数据库的项目兼容性问题
vs2010无法正确打开2015创建的项目里面操作Sqlite数据库时使用EF6创建的edmx文件(会显示空白) 但是可以正常查询 vs2015无法正确打开2010创建的项目里面操作Sqlite数 ...
- Centos使用dd命令制作U盘启动盘 wodim刻录光盘
首先格式化U盘:使用fdisk -l 查看U盘到挂载点,如我的为/dev/sdb1.卸载U盘,运行格式化命令:mkfs.vfat /dev/sdb1 然后又一次挂载U盘,開始制作启动盘: # dd ...
- JavaScript之二:this
在JavaScript中,this的指代对象是什么?最精辟的解释却只有一句话: when a function of an object was called, the object will be ...
- 用命令行在github新建一个项目
Github Repository API中说明了可以通过发送一个请求来认证,之后就能通过命令行自动新建远程仓库了. 认证 curl -u 'username' https://api.github. ...
- android怎么在launcher改动内置apk的icon
找到launcher下的IconCache中加入变量用来存储要改动apk的包名 及要改动成的icon private String[] className = {"com.google.an ...