L-j-h-c / TIL

CS, Swift, Java, C++, 개발 관련 공부한 내용 정리
11 stars 0 forks source link

유용한 코드 스타일 정리 #77

Open L-j-h-c opened 1 year ago

L-j-h-c commented 1 year ago

아래처럼 셀을 리턴 받을 수도 있네.

private var selectedMusicCell: EditOperationAudioCell? {
    return musicContainer.selectedCell as? EditOperationAudioCell
}

// 각 셀들은 EditOperationCell이라는 프로토콜을 따른다
public var selectedCell: EditOperationCell?

guard let model = self.selectedCaptionCell?.model as? EditOperationCaptionCellModel else { return }

특정 슈퍼뷰 내부의 서브뷰들 중에서 원하는 객체의 이벤트를 받아오는 방법

fileprivate class EditToolContentView: UIView {

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        //분할 버튼에 대한 우선 순위 응답
        for view in subviews {
            if let button: EditToolSplitButton = view as? EditToolSplitButton {
                if button.frame.contains(point) {
                    return button
                }
            }
        }
        return super.hitTest(point, with: event)
    }
}

셀과 클로저의 사용

    private lazy var video_CollectionView: EditToolImageThumbView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        let view = EditToolImageThumbView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: EDIT_THUMB_CELL_SIZE_Height), collectionViewLayout: layout)
        view.isScrollEnabled = false
        view.layer.cornerRadius = 4
        view.itemCountClosure = { [unowned self] in
            return self.presenter.frameCount()
        }
        view.itemModelClosure = { [unowned self] (item: Int) -> EditToolImageCellModel in
            return self.presenter.thumbModel(at: item)
        }
        view.backgroundColor = .clear
        return view
    }()

class EditToolImageThumbView: UICollectionView {

    public var itemCountClosure: (() -> Int)?
    public var itemModelClosure: ((_ item: Int) -> EditToolImageCellModel)?

    private let queuePool = DispatchQueuePool(name: "ImageThumbView.LoadImages", queueCount: 6, qos: .userInteractive)

    public func loadImages() {
        let items = indexPathsForVisibleItems
        items.forEach {
            let c = cellForItem(at: $0) as! EditToolImageCell
            if itemModelClosure != nil {
                let model = itemModelClosure!($0.item)
                loadImage(at: model.time) { (image) in
                    c.imageLayer.contents = image?.cgImage
                }
            }
        }
    }
}