From e61ad67b39bdaf452f1faf595c978145ce823e4f Mon Sep 17 00:00:00 2001 From: "ir.__.si" Date: Tue, 24 Oct 2023 10:23:54 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Feat:=20#4=20-=20=EC=8E=94=EC=94=A8?= =?UTF-8?q?=20TouchArea=20=EC=A0=9C=EC=9E=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DesignSystem/Sources/CMCTouchArea.swift | 95 +++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 DesignSystem/Sources/CMCTouchArea.swift diff --git a/DesignSystem/Sources/CMCTouchArea.swift b/DesignSystem/Sources/CMCTouchArea.swift new file mode 100644 index 0000000..f2cefcc --- /dev/null +++ b/DesignSystem/Sources/CMCTouchArea.swift @@ -0,0 +1,95 @@ +// +// CMCTouchArea.swift +// DesignSystem +// +// Created by Siri on 2023/10/24. +// Copyright © 2023 com.centralMakeusChallenge. All rights reserved. +// +import Foundation + +import RxCocoa +import RxSwift + +import SnapKit + +import UIKit + +public final class CMCTouchArea: UIView{ + + public enum TouchAreaStyle: Int { + case normal = 0 + case selected = 1 + } + + // MARK: - UI + private lazy var imageView: UIImageView = { + let imageView = UIImageView() + imageView.image = image[style.rawValue] + return imageView + }() + + // MARK: - Properties + private var disposeBag = DisposeBag() + + private var style: TouchAreaStyle = .normal + private var image: [Int:UIImage] = [:] + + /// 터치 영역의 `image`를 설정합니다. + /// - Parameters: + /// - image: 터치 영역의 `image` + /// - Parameters (Optional): + /// - Parameters (Accessable): + // MARK: - Initializers + public init( + image: UIImage + ) { + self.image.updateValue(image, forKey: style.rawValue) + super.init(frame: .zero) + + self.setImage(image, for: .normal) + self.setImage(image, for: .selected) + + setAddSubView() + setConstraint() + bind() + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + // MARK: - Methods + + private func setAddSubView() { + self.addSubview(imageView) + } + + private func setConstraint() { + imageView.snp.makeConstraints { + $0.centerX.centerY.equalToSuperview() + } + } + + private func bind() { + self.rx.tapped() + .withUnretained(self) + .subscribe(onNext: { owner, _ in + owner.style = owner.style == .normal ? .selected : .normal + owner.imageView.image = owner.image[owner.style.rawValue] + }) + .disposed(by: disposeBag) + } + + public func setImage(_ image: UIImage, for style: TouchAreaStyle) { + self.image.updateValue(image, forKey: style.rawValue) + } + +} + +// MARK: - CMCTouchArea+RxSwift +extension Reactive where Base: CMCTouchArea { + public func tapped() -> ControlEvent { + let source = self.base.rx.tapGesture().when(.recognized).map { _ in } + return ControlEvent(events: source) + } +}