diff --git a/HMH_iOS/HMH_iOS.xcodeproj/project.pbxproj b/HMH_iOS/HMH_iOS.xcodeproj/project.pbxproj index 4b2e43f..85ebac4 100644 --- a/HMH_iOS/HMH_iOS.xcodeproj/project.pbxproj +++ b/HMH_iOS/HMH_iOS.xcodeproj/project.pbxproj @@ -24,7 +24,10 @@ 0B8A89BC2B369F2D00688BA6 /* UIView+.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B8A89BB2B369F2D00688BA6 /* UIView+.swift */; }; 0B8A89C42B369FA000688BA6 /* F.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B8A89C32B369FA000688BA6 /* F.swift */; }; 0B8A89C62B369FA800688BA6 /* H.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B8A89C52B369FA800688BA6 /* H.swift */; }; + + 0BC0EBD42B494459003EF5D4 /* OnboardingSwipeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0BC0EBD32B494459003EF5D4 /* OnboardingSwipeView.swift */; }; 0BC0EBD22B493B6B003EF5D4 /* OnboardingProgressView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0BC0EBD12B493B6B003EF5D4 /* OnboardingProgressView.swift */; }; + 174AF48E2B447B1F00450D07 /* MyPageCells.swift in Sources */ = {isa = PBXBuildFile; fileRef = 174AF48D2B447B1F00450D07 /* MyPageCells.swift */; }; 174AF4902B447B3C00450D07 /* MyPageModels.swift in Sources */ = {isa = PBXBuildFile; fileRef = 174AF48F2B447B3C00450D07 /* MyPageModels.swift */; }; 174AF4922B447B4600450D07 /* MyPageViews.swift in Sources */ = {isa = PBXBuildFile; fileRef = 174AF4912B447B4600450D07 /* MyPageViews.swift */; }; @@ -144,7 +147,10 @@ 0B8A89BB2B369F2D00688BA6 /* UIView+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIView+.swift"; sourceTree = ""; }; 0B8A89C32B369FA000688BA6 /* F.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = F.swift; sourceTree = ""; }; 0B8A89C52B369FA800688BA6 /* H.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = H.swift; sourceTree = ""; }; + + 0BC0EBD32B494459003EF5D4 /* OnboardingSwipeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingSwipeView.swift; sourceTree = ""; }; 0BC0EBD12B493B6B003EF5D4 /* OnboardingProgressView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingProgressView.swift; sourceTree = ""; }; + 174AF48D2B447B1F00450D07 /* MyPageCells.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MyPageCells.swift; sourceTree = ""; }; 174AF48F2B447B3C00450D07 /* MyPageModels.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MyPageModels.swift; sourceTree = ""; }; 174AF4912B447B4600450D07 /* MyPageViews.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MyPageViews.swift; sourceTree = ""; }; @@ -244,6 +250,7 @@ isa = PBXGroup; children = ( 0B2C2D3E2B4559E10023CCFA /* OnboardingButton.swift */, + 0BC0EBD32B494459003EF5D4 /* OnboardingSwipeView.swift */, 0BC0EBD12B493B6B003EF5D4 /* OnboardingProgressView.swift */, ); path = Views; @@ -829,6 +836,7 @@ 0B8A89BC2B369F2D00688BA6 /* UIView+.swift in Sources */, 0B8A89AF2B369E4300688BA6 /* HomeModel.swift in Sources */, 3666C87A2B45F4F900564874 /* SelectedBlocker.swift in Sources */, + 0BC0EBD42B494459003EF5D4 /* OnboardingSwipeView.swift in Sources */, 36A3D9C02B409CBD007EA272 /* Font.swift in Sources */, 174AF4902B447B3C00450D07 /* MyPageModels.swift in Sources */, 174AF4982B447CF100450D07 /* ChanllengeModels.swift in Sources */, diff --git a/HMH_iOS/HMH_iOS/Presentation/Onboarding/Views/OnboardingSwipeView.swift b/HMH_iOS/HMH_iOS/Presentation/Onboarding/Views/OnboardingSwipeView.swift new file mode 100644 index 0000000..ecd400f --- /dev/null +++ b/HMH_iOS/HMH_iOS/Presentation/Onboarding/Views/OnboardingSwipeView.swift @@ -0,0 +1,111 @@ +// +// OnboardingSwipeView.swift +// HMH_iOS +// +// Created by Seonwoo Kim on 1/6/24. +// + +import UIKit + +import SnapKit +import Then + +final class OnboardingSwipeView: UIView { + private var timer: Timer? + private var currentPage: Int = 0 + + let scrollView = UIScrollView().then { + $0.isPagingEnabled = true + $0.showsHorizontalScrollIndicator = false + $0.translatesAutoresizingMaskIntoConstraints = false + $0.contentSize = CGSize(width: (375 * 3).adjustedWidth, height: 380.adjustedHeight) + } + + let pageControl = UIPageControl().then { + $0.translatesAutoresizingMaskIntoConstraints = false + $0.numberOfPages = 3 + $0.currentPage = 0 + } + + override init(frame: CGRect) { + super.init(frame: frame) + + setUI() + setDelegate() + startAutoScroll() + setImageView() + } + + deinit { + stopAutoScroll() + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + private func setDelegate() { + scrollView.delegate = self + } + + private func setUI() { + setHierarchy() + setConstraints() + } + + private func setHierarchy() { + self.addSubviews(scrollView, pageControl) + } + + private func setConstraints() { + scrollView.snp.makeConstraints { + $0.width.equalToSuperview() + $0.height.equalTo(380.adjustedHeight) + } + + pageControl.snp.makeConstraints { + $0.centerX.equalToSuperview() + $0.bottom.equalToSuperview() + } + } + + private func setImageView() { + for i in 0..<3 { + let pageView = UIImageView().then { + $0.image = [ImageLiterals.TabBar.icHome, ImageLiterals.TabBar.icChallenge, ImageLiterals.TabBar.icChallengeSelected][i] + $0.translatesAutoresizingMaskIntoConstraints = false + $0.contentMode = .scaleAspectFit + } + + scrollView.addSubview(pageView) + + pageView.snp.makeConstraints { + $0.top.equalTo(scrollView.snp.top) + $0.size.equalToSuperview() + $0.leading.equalTo(scrollView.snp.leading).offset(375.adjusted * CGFloat(i)) + } + } + } + + private func startAutoScroll() { + timer = Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: #selector(nextPage), userInfo: nil, repeats: true) + } + + private func stopAutoScroll() { + timer?.invalidate() + timer = nil + } + + @objc private func nextPage() { + currentPage = (currentPage + 1) % 3 + scrollView.setContentOffset(CGPoint(x: (scrollView.frame.width.adjusted * CGFloat(currentPage)), y: 0), animated: true) + pageControl.currentPage = currentPage + } +} + +extension OnboardingSwipeView: UIScrollViewDelegate { + func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { + currentPage = Int(scrollView.contentOffset.x.adjusted / scrollView.frame.width.adjusted) + pageControl.currentPage = currentPage + } +}