Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extension ViewController: GestureCollectionViewDelegate {
- // 讓手指碰觸點與前一個點產生連線(與畫線大同小異,只是只會在畫面上加入一次)
- func move(point: CGPoint) {
- // 如果前一個點存在
- if let currentPoint = currentPoint {
- // 判斷目前與手指的連線是否存在
- // 若無則新增一個 CAShapeLayer
- if moveLayer == nil {
- moveLayer = CAShapeLayer()
- view.layer.addSublayer(moveLayer!)
- }
- moveLayer?.frame = gestureCollectionView.bounds
- moveLayer?.position = gestureCollectionView.center
- moveLayer?.fillColor = nil
- moveLayer?.lineWidth = 3
- moveLayer?.strokeColor = UIColor.green.cgColor
- let path = UIBezierPath()
- path.move(to: currentPoint)
- path.addLine(to: point)
- moveLayer?.path = path.cgPath
- moveLayer?.lineCap = .round
- }
- }
- // 判斷是否已經滑到 CollectionViewCell 中
- func selectedItem(indexPath: IndexPath) {
- // 首先判斷是否已經選取過該項目
- if selectedPassword.contains(indexPath.row) { return }
- let cell = gestureCollectionView.cellForItem(at: indexPath)
- // 畫線到 Cell 的中心
- drawLine(to: cell!.center)
- // 新增密碼
- selectedPassword.append(indexPath.row)
- // 刪除當前的滑動觸碰連線
- moveLayer?.removeFromSuperlayer()
- moveLayer = nil
- // 3D Touch 震動效果
- AudioServicesPlaySystemSound(1520)
- // 這邊我們會根據 Cell 是否被選取顯示不一樣的顏色
- // 所以我們會更新該 indexPath 的項目
- gestureCollectionView.reloadItems(at: [indexPath])
- }
- // 滑動結束時判斷節所
- func cancel() {
- // 判斷當前的手勢密碼為哪個類型
- switch gesturePasswordType {
- // 設置密碼
- case .setting:
- // 如果密碼超過4碼,那麼就設定成功
- // 反之則會清除密碼,讓使用者在試一次
- if selectedPassword.count >= 4 {
- password = selectedPassword
- showMessageAlert(message: "Password setting is successful")
- } else {
- showMessageAlert(message: "Password must be greater than 4, please try again")
- }
- // 解鎖密碼
- case .unlock:
- // 判斷兩個 Array 是否相同
- if selectedPassword == password {
- showMessageAlert(message: "Unlocked successfully")
- } else {
- // 若是不相同,那麼則把所有畫面上的 Layer 都改為紅色
- lineLayers.forEach { (layer) in
- layer.strokeColor = UIColor.red.cgColor
- }
- gestureCollectionView.visibleCells.forEach { (cell) in
- cell.layer.borderColor = UIColor.red.cgColor
- }
- moveLayer?.strokeColor = UIColor.red.cgColor
- showMessageAlert(message: "Unlock failed, please try again")
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement