Skip to content

Commit

Permalink
added tick marks to the newReleasesSlider UISlider and snap functiona…
Browse files Browse the repository at this point in the history
…lity (#16)

open issue #8 UISlider snap functionality and tick marks
  • Loading branch information
ThasianX authored Sep 29, 2021
2 parents 46ba8cf + ae449f5 commit f80d9bb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ RecentlyPlayedTracksViewModelOutput {
self.dataManager = dataManager
self.safariService = safariService

// Initializing utputs
// Initializing outputs
title = Observable.just("Your Recently Played Tracks")

trackCollections = sessionService.getRecentlyPlayedTracks(limit: 50)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ final class NewReleasesViewController: ViewControllerWithSideMenu, BindableType
configureNavigationBar()
configureTableView()
setUpView()
createTickView()
}

deinit {
Expand All @@ -63,6 +64,10 @@ final class NewReleasesViewController: ViewControllerWithSideMenu, BindableType
newReleasesSlider.trailingAnchor.constraint(equalTo: layoutGuide.trailingAnchor, constant: -Constraints.outerMargins).isActive = true
newReleasesSlider.heightAnchor.constraint(equalToConstant: Constraints.height).isActive = true

// adds target to method snap functionality on the newReleasesSlider UISlider
newReleasesSlider.addTarget(self, action: #selector(sliderChanged), for: .touchUpInside)
newReleasesSlider.isContinuous = false

tableView.topAnchor.constraint(equalTo: newReleasesSlider.bottomAnchor).isActive = true
tableView.leadingAnchor.constraint(equalTo: layoutGuide.leadingAnchor).isActive = true
tableView.trailingAnchor.constraint(equalTo: layoutGuide.trailingAnchor).isActive = true
Expand Down Expand Up @@ -93,6 +98,24 @@ final class NewReleasesViewController: ViewControllerWithSideMenu, BindableType
)
}

// creates the tick mark view and inserts it on top of the newReleasesSlider UISlider
private func createTickView() {
var tick : UIView
let tickArray = Array(1...12)
for i in 0..<tickArray.count {
tick = UIView(frame: CGRect(x: (newReleasesSlider.frame.size.width / 3) * CGFloat(i), y: (newReleasesSlider.frame.size.height - 13) / 2, width: 2, height: 13))
tick.backgroundColor = ColorPreference.tertiaryColor
newReleasesSlider.insertSubview(tick, belowSubview: newReleasesSlider)
}
}

// provides the snap functionality on the newReleasesSlider UISlider
@objc func sliderChanged(){
let step:Float = 1
let roundedStepValue = round(newReleasesSlider.value / step) * step
newReleasesSlider.value = roundedStepValue
}

func bindViewModel() {
let input = viewModel.input
let output = viewModel.output
Expand Down Expand Up @@ -143,7 +166,8 @@ final class NewReleasesViewController: ViewControllerWithSideMenu, BindableType
newReleasesSlider.rx.value
.debounce(.milliseconds(500), scheduler: MainScheduler.instance)
.bind(onNext: { [unowned self] value in
self.sliderTimeRange.text = "New releases within the past \(Int(value)) months"
// value.rounded() makes sure sliderTimeRange.text is accurate after thumbnail snaps to closest Int
self.sliderTimeRange.text = "New releases within the past \(Int(value.rounded())) months"
input.sliderValue.accept(value)
})
.disposed(by: disposeBag)
Expand Down

0 comments on commit f80d9bb

Please sign in to comment.