Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. extension ViewController: GestureCollectionViewDelegate {
  2.  
  3. // 讓手指碰觸點與前一個點產生連線(與畫線大同小異,只是只會在畫面上加入一次)
  4. func move(point: CGPoint) {
  5. // 如果前一個點存在
  6. if let currentPoint = currentPoint {
  7. // 判斷目前與手指的連線是否存在
  8. // 若無則新增一個 CAShapeLayer
  9. if moveLayer == nil {
  10. moveLayer = CAShapeLayer()
  11. view.layer.addSublayer(moveLayer!)
  12. }
  13. moveLayer?.frame = gestureCollectionView.bounds
  14. moveLayer?.position = gestureCollectionView.center
  15. moveLayer?.fillColor = nil
  16. moveLayer?.lineWidth = 3
  17. moveLayer?.strokeColor = UIColor.green.cgColor
  18. let path = UIBezierPath()
  19. path.move(to: currentPoint)
  20. path.addLine(to: point)
  21. moveLayer?.path = path.cgPath
  22. moveLayer?.lineCap = .round
  23. }
  24. }
  25.  
  26. // 判斷是否已經滑到 CollectionViewCell 中
  27. func selectedItem(indexPath: IndexPath) {
  28. // 首先判斷是否已經選取過該項目
  29. if selectedPassword.contains(indexPath.row) { return }
  30. let cell = gestureCollectionView.cellForItem(at: indexPath)
  31. // 畫線到 Cell 的中心
  32. drawLine(to: cell!.center)
  33. // 新增密碼
  34. selectedPassword.append(indexPath.row)
  35. // 刪除當前的滑動觸碰連線
  36. moveLayer?.removeFromSuperlayer()
  37. moveLayer = nil
  38. // 3D Touch 震動效果
  39. AudioServicesPlaySystemSound(1520)
  40. // 這邊我們會根據 Cell 是否被選取顯示不一樣的顏色
  41. // 所以我們會更新該 indexPath 的項目
  42. gestureCollectionView.reloadItems(at: [indexPath])
  43. }
  44.  
  45. // 滑動結束時判斷節所
  46. func cancel() {
  47. // 判斷當前的手勢密碼為哪個類型
  48. switch gesturePasswordType {
  49. // 設置密碼
  50. case .setting:
  51. // 如果密碼超過4碼,那麼就設定成功
  52. // 反之則會清除密碼,讓使用者在試一次
  53. if selectedPassword.count >= 4 {
  54. password = selectedPassword
  55. showMessageAlert(message: "Password setting is successful")
  56. } else {
  57. showMessageAlert(message: "Password must be greater than 4, please try again")
  58. }
  59. // 解鎖密碼
  60. case .unlock:
  61. // 判斷兩個 Array 是否相同
  62. if selectedPassword == password {
  63. showMessageAlert(message: "Unlocked successfully")
  64. } else {
  65. // 若是不相同,那麼則把所有畫面上的 Layer 都改為紅色
  66. lineLayers.forEach { (layer) in
  67. layer.strokeColor = UIColor.red.cgColor
  68. }
  69. gestureCollectionView.visibleCells.forEach { (cell) in
  70. cell.layer.borderColor = UIColor.red.cgColor
  71. }
  72. moveLayer?.strokeColor = UIColor.red.cgColor
  73. showMessageAlert(message: "Unlock failed, please try again")
  74. }
  75. }
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement