LoginSignup
18
22

More than 3 years have passed since last update.

iOSの有名リポジトリのコードの小技

Posted at

うまい人の研究してわかったこと

  • アクセス修飾子をちゃんと書く
  • アクセス修飾子ごとにextentionを切る
  • internal var heroIDToSourceView = [String: UIView]() というふうに、propertyをoptionalにするようなことはしない
    • optinalはoptinalにする必要にするときだけ
  • delegateはweakにする
  • UIView(frame: transitionContainer?.bounds ?? .zero) nullのcheckに演算子を使う
  • for v in animatingToViews { _ = context.snapshotView(for: v) }
  • 1行for文もあり
  • deferの使い方が上手い(loadingを消したり、初期化処理をしたりする)

    • deferとは、途中でreturnしても実行される文
  • funcに対するコメントを書く

  /**
   Update the progress for the interactive transition.
   - Parameters:
   - progress: the current progress, must be between 0...1
   */
  • propertyのViewControllerの宣言が、くっそうまい(関数的で、他に影響がないように書いている)
lazy var mmPlayerLayer: MMPlayerLayer = {
        let l = MMPlayerLayer()
        l.cacheType = .memory(count: 5)
        l.coverFitType = .fitToPlayerView
        l.videoGravity = AVLayerVideoGravity.resizeAspectFill
        l.replace(cover: CoverA.instantiateFromNib())
        return l
    }()

Layerのアニメーションに関して

fileprivate func setFrameWith(quadrant: VideoPositionType, dismissVideo: Bool) {
        let margin = self.config.margin
        var rect = self.config.playLayer?.playView?.frame ?? .zero
        let size = UIScreen.main.bounds
        switch quadrant {
        case .leftTop:
            rect.origin.x = dismissVideo ? -rect.size.width : margin
            rect.origin.y = margin
        case .rightTop:
            rect.origin.x = dismissVideo ? size.width : size.width-rect.size.width-margin
            rect.origin.y = margin
        case .leftBottom:
            rect.origin.x = dismissVideo ? -rect.size.width : margin
            rect.origin.y = size.height-rect.size.height-margin
        case .rightBottom:
            rect.origin.x = dismissVideo ? size.width : size.width-rect.size.width-margin
            rect.origin.y = size.height-rect.size.height-margin
        }

        to?.view.alpha = 0.0
        UIView.animate(withDuration: self.config.duration, animations: {
            if dismissVideo {
                self.config.playLayer?.playView?.alpha = 0.0
            }
            self.config.playLayer?.playView?.frame = rect
        }) { [unowned self] (_) in
            if dismissVideo {

                (self.config as? MMPlayerPassViewPresentConfig)?._dismissGesture = true
                self.config.playLayer?.setCoverView(enable: true)
                self.to?.dismiss(animated: true, completion: nil)
            }
        }
    }

willSet + newValueを使う

    @IBOutlet weak var okButton: UIButton! {
        willSet {
            // OKボタンの修飾
            newValue.layer.borderColor = UIColor.white.cgColor
            newValue.layer.borderWidth = 1.0
            newValue.layer.cornerRadius = 5.0
        }
    }
18
22
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
22