-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feat/#21-OnboardingSwipeView' into develop
- Loading branch information
Showing
2 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
HMH_iOS/HMH_iOS/Presentation/Onboarding/Views/OnboardingSwipeView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |