Skip to content

Instantly share code, notes, and snippets.

View JeremyXue77's full-sized avatar
🐈
Keep going

Jeremy Xue JeremyXue77

🐈
Keep going
View GitHub Profile
public enum HTTPMethod: String {
case get = "GET"
case post = "POST"
case put = "PUT"
case patch = "PATCH"
case delete = "DELETE"
}
protocol EndPointType {
var baseURL: URL { get }
var path: String { get }
var httpMethod: HTTPMethod { get }
var task: HTTPTask { get }
var headers: HTTPHeaders? { get }
}
func showMessageAlert(message: String) {
// 加入震動
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate)
let alert = UIAlertController(title: "Gesture Password", message: message, preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default) { [weak self](_) in
// 按下 OK 時,清空所有 lineLayers 中的 layer
self?.lineLayers.forEach { (layer) in
layer.removeFromSuperlayer()
}
self?.lineLayers.removeAll()
extension ViewController: GestureCollectionViewDelegate {
// 讓手指碰觸點與前一個點產生連線(與畫線大同小異,只是只會在畫面上加入一次)
func move(point: CGPoint) {
// 如果前一個點存在
if let currentPoint = currentPoint {
// 判斷目前與手指的連線是否存在
// 若無則新增一個 CAShapeLayer
if moveLayer == nil {
moveLayer = CAShapeLayer()
protocol GestureCollectionViewDelegate: class {
func move(point: CGPoint)
func selectedItem(indexPath: IndexPath)
func cancel()
}
class GestureCollectionView: UICollectionView {
weak var gestureDelegate: GestureCollectionViewDelegate?
private func drawLine(to point: CGPoint) {
// 判斷當前是否有前一個座標
if let currentPoint = currentPoint {
// 新增 CAShapeLayer
let shapeLayer = CAShapeLayer()
shapeLayer.frame = gestureCollectionView.bounds
shapeLayer.position = gestureCollectionView.center
shapeLayer.fillColor = nil
shapeLayer.lineWidth = 3
shapeLayer.strokeColor = UIColor.green.cgColor
enum GesturePasswordType: Int {
case setting = 0
case unlock = 1
}
@IBAction func changeType(_ sender: UISegmentedControl) {
guard let type = GesturePasswordType(rawValue: sender.selectedSegmentIndex) else { return }
gesturePasswordType = type
}
@IBAction func changeRows(_ sender: UIStepper) {
let alert = UIAlertController(title: "", message: "Your password will be cleared. Do you want to continue?", preferredStyle: .alert)
let continueAction = UIAlertAction(title: "Continue", style: .default) { [weak self](_) in
self?.row = Int(sender.value)
self?.password = []
self?.gestureCollectionView.reloadSections(IndexSet(integer: 0))
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alert.addAction(continueAction)
alert.addAction(cancelAction)
extension ViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = collectionView.bounds.width / CGFloat(row * 2 - 1)
return CGSize(width: width, height: width)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
let width = collectionView.bounds.width / CGFloat(row * 2 - 1)
return width
@IBAction func logging(_ sender: UIButton) {
// 創建 LAContext 實例
let context = LAContext()
// 設置取消按鈕標題
context.localizedCancelTitle = "Cancel"
// 宣告一個變數接收 canEvaluatePolicy 返回的錯誤
var error: NSError?
// 評估是否可以針對給定方案進行身份驗證
if context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &error) {
// 描述使用身份辨識的原因