swift版本手势解锁
这个很有意思啊
控制器部分
//
// ViewController.swift
// 手势解锁
//
// Created by admin on 16/1/25.
// Copyright © 2016年 jin. All rights reserved.
//
import UIKit
class ViewController: UIViewController,Gesturedelegate {
override func viewDidLoad() {
super.viewDidLoad()
// 初始化自定义类
let gesture = Gesture(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.size.width, 400))
//
gesture.backgroundColor = UIColor.clearColor()
gesture.passwordDelegate = self
self.view.backgroundColor = UIColor(patternImage: UIImage(imageLiteral: "Home_refresh_bg"))
self.view.addSubview(gesture)
// Do any additional setup after loading the view, typically from a nib.
}
func gestureCheckPassword(password: String) {
if password == "0123"
{
let alert = UIAlertController(title: "登录成功", message: "登录成功", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "确认", style: UIAlertActionStyle.Cancel, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
// 用下面这个弹窗会有警告
//let alert = UIAlertView(title: "登录成功", message: "登录成功", delegate: nil, cancelButtonTitle: "登录成功")
}
}
}
自定义类,解锁类,其实更多的是画图
//
// Gesture.swift
// 手势解锁
//
// Created by admin on 16/1/25.
// Copyright © 2016年 jin. All rights reserved.
//
import UIKit
@objc protocol Gesturedelegate
{
optional func gestureCheckPassword(password:String)
}
class Gesture: UIView {
lazy var buttons:[UIButton] = []// 存放所有按钮
lazy var selectedButtons:[UIButton] = []// 存放以及选择的按钮
lazy var point:CGPoint = CGPoint(x: 0, y: 0)// 存放当前的点击的位置
weak var passwordDelegate:Gesturedelegate!// 验证密码的代理
override init(frame: CGRect) {
super.init(frame: frame)
// 添加按钮
for var i = 0;i < 9;i++
{
let button = UIButton()
// 禁止交互,不禁止的话划过的时候会改变样式
button.userInteractionEnabled = false
// 设置tag,用来组合密码
button.tag = i
self.buttons.append(button)
self.addSubview(button)
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// 设置按钮的布局
override func layoutSubviews() {
// 获得不同状态的图片
let normalImage = UIImage(imageLiteral: "gesture_node_normal")
let selectedImage = UIImage(imageLiteral: "gesture_node_highlighted")
// 按钮的行数
let colunm = 3
// 根据行数计算边距
let margin = (self.frame.size.width - (normalImage.size.width * CGFloat(colunm))) / CGFloat(colunm + 1)
// 计算边距,利用取余和除
for var i = 0;i < self.buttons.count;i++
{
let button = self.buttons[i]
let marginLeft = CGFloat(i % colunm)
let marginTop = CGFloat(i / colunm)
let buttonRect = CGRectMake((marginLeft * normalImage.size.width) + (marginLeft + 1) * margin, ((marginTop + 1) * margin) + ((marginTop) * normalImage.size.height), normalImage.size.width, normalImage.size.height)
button.frame = buttonRect
button.setImage(normalImage, forState: UIControlState.Normal)
button.setImage(selectedImage, forState: UIControlState.Selected)
self.addSubview(button)
}
}
// 开始点击的时候
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.touchesMoved(touches, withEvent: event)
}
// 设置当前连线状态
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
// 获取当前touch对象
let touch = (touches as NSSet).anyObject()
// 获取当前触摸位置
let location:CGPoint = (touch?.locationInView(touch?.view))!
// 循环所有的按钮
for var i = 0;i < self.buttons.count;i++
{
let button = self.buttons[i]
// 当触摸在按钮上时,设置按钮的状态,并加入类属性,注意这里的函数
if CGRectContainsPoint(button.frame, location) && button.selected == false
{
button.selected = true
self.selectedButtons.append(button)
}
}
// 追加当前触摸位置点
self.point = location
self.setNeedsDisplay()
}
// 结束触摸时做处理
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
// 循环取消以选中的按钮状态
self.selectedButtons.forEach({$0.selected = false})
// 删除已经记录的所有已选中的按钮
self.selectedButtons.removeAll()
self.setNeedsDisplay()
}
// 画当前连线状态
override func drawRect(rect: CGRect) {
// 设置path对象
let lines:UIBezierPath = UIBezierPath()
// 如果没有选中则直接返回
if self.selectedButtons.count == 0
{
return
}
// 设置起点
lines.moveToPoint((self.selectedButtons.first?.center)!)
// 初始化密码字符串
var password = ""
// 获取当前选中的按钮的点和tag
selectedButtons.forEach({
button in
lines.addLineToPoint(button.center)
password += "\(button.tag)"
})
// 添加当前触摸的点
lines.addLineToPoint(self.point)
// 设置线的格式
lines.lineCapStyle = CGLineCap.Round
lines.lineJoinStyle = CGLineJoin.Round
lines.lineWidth = 10
let lineColor = UIColor.blueColor()
lineColor.set()
// 画
lines.stroke()
// 执行密码的代理方法
self.passwordDelegate?.gestureCheckPassword?(password)
}
}