1. 程式人生 > >【Swift 3.1】iOS開發筆記(四)

【Swift 3.1】iOS開發筆記(四)

  一、唱片旋轉效果(360°無限順時針旋轉)

    func animationRotateCover() {
        coverImageView.layer.removeAllAnimations()

        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.fromValue = 0
        animation.toValue = CGFloat.pi * 2
        animation.duration = 5
        animation.isCumulative 
= true animation.repeatCount = Float.infinity coverImageView.layer.add(animation, forKey: nil) }

  二、防止檔案被 iCloud 同步備份

    NSURLIsExcludedFromBackupKey

  三、禁止 UICollectionView reload/insert 動畫

      UIView.performWithoutAnimation {
           self.videoPartCollectionView.reloadData()
      }

  四、Objective-C 中的 performSelector 在 Swift 裡變成了 sendAction

    var rightTappedSelector: Selector?

    @IBAction func rightTappedAction(_ sender: Any) {
        guard let selector = rightTappedSelector else {
            return
        }
        rightButton.sendAction(selector, to: nil, 
for: nil) }

  五、獲得 CGAffineTransform 的 rotation 資訊

extension CGAffineTransform {

    func getTransformRotation() -> CGFloat {
        return atan2(self.b, self.a)  * 180 / CGFloat.pi
    }
    
}

  六、獲取 Date 的 nano 時間

extension Date {

    func nanosecond() -> Int64 {
        let nanosecond: Int64 = Int64(Calendar.current.dateComponents([.nanosecond], from: self).nanosecond ?? 0)
        return Int64(self.timeIntervalSince1970 * 1000000000) + nanosecond
    }

}

  七、AVCapturePhotoOutput.capturePhoto 崩潰的問題

    不要讓 UIViewController 實現 AVCapturePhotoCaptureDelegate ,要跟 AVCam 例子一樣弄一個 class PhotoCaptureDelegate: NSObject, AVCapturePhotoCaptureDelegate 就不崩潰了,什麼鬼問題