diff --git a/Example/HGCircularSlider/Base.lproj/Main.storyboard b/Example/HGCircularSlider/Base.lproj/Main.storyboard index aa6a9a1..8068231 100644 --- a/Example/HGCircularSlider/Base.lproj/Main.storyboard +++ b/Example/HGCircularSlider/Base.lproj/Main.storyboard @@ -1,28 +1,12 @@ - + - + + - - - - - - - - - - - - - - - - - @@ -32,45 +16,58 @@ - + + + + - + + + + + - + + + @@ -78,7 +75,8 @@ - + + @@ -87,16 +85,16 @@ - + - + - + - + @@ -104,13 +102,14 @@ - - + + @@ -122,6 +121,7 @@ + @@ -132,7 +132,137 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -142,6 +272,6 @@ - + diff --git a/Example/HGCircularSlider/PlayerViewController.swift b/Example/HGCircularSlider/PlayerViewController.swift new file mode 100644 index 0000000..d755211 --- /dev/null +++ b/Example/HGCircularSlider/PlayerViewController.swift @@ -0,0 +1,137 @@ +// +// PlayerViewController.swift +// HGCircularSlider +// +// Created by Hamza Ghazouani on 08/11/2016. +// Copyright © 2016 CocoaPods. All rights reserved. +// + +import UIKit +import AVFoundation + +import HGCircularSlider + +/* + KVO context used to differentiate KVO callbacks for this class versus other + classes in its class hierarchy. + */ +private var playerViewControllerKVOContext = 0 + + +class PlayerViewController: UIViewController { + + @IBOutlet weak var circularSlider: CircularSlider! + @IBOutlet weak var timerLabel: UILabel! + @IBOutlet weak var playerSegmentedControl: UISegmentedControl! + + let audioPlayer = AVPlayer() + + // date formatter user for timer label + let dateComponentsFormatter: NSDateComponentsFormatter = { + let formatter = NSDateComponentsFormatter() + formatter.zeroFormattingBehavior = .Pad + formatter.allowedUnits = [.Minute, .Second] + + return formatter + }() + + override func viewDidLoad() { + super.viewDidLoad() + + setupAudioPlayer() + + circularSlider.addTarget(self, action: #selector(pause), forControlEvents: .EditingDidBegin) + circularSlider.addTarget(self, action: #selector(play), forControlEvents: .EditingDidEnd) + circularSlider.addTarget(self, action: #selector(updateTimer), forControlEvents: .ValueChanged) + + NSNotificationCenter.defaultCenter().addObserver(self, + selector: #selector(playerItemDidReachEnd(_:)), + name: AVPlayerItemDidPlayToEndTimeNotification, + object: audioPlayer.currentItem) + } + + override func didReceiveMemoryWarning() { + super.didReceiveMemoryWarning() + // Dispose of any resources that can be recreated. + } + + @IBAction func togglePlayer(sender: UISegmentedControl) { + switch sender.selectedSegmentIndex { + case 0: + let currentTime = Float64(circularSlider.endPointValue) + let newTime = CMTimeMakeWithSeconds(currentTime, 600) + audioPlayer.seekToTime(newTime, toleranceBefore: kCMTimeZero, toleranceAfter: kCMTimeZero) + audioPlayer.play() + default: + audioPlayer.pause() + } + } + + func play() { + self.playerSegmentedControl.selectedSegmentIndex = 0 + togglePlayer(playerSegmentedControl) + } + + func pause() { + self.playerSegmentedControl.selectedSegmentIndex = UISegmentedControlNoSegment + togglePlayer(playerSegmentedControl) + } + + + /** + setup and play the sound of the local mp3 file + */ + func setupAudioPlayer() { + // TODO: load the audio file asynchronously and observe player status + guard let audioFileURL = NSBundle.mainBundle().URLForResource("StrangeZero", withExtension: "mp3") else { return } + let asset = AVURLAsset(URL: audioFileURL, options: nil) + let playerItem = AVPlayerItem(asset: asset) + audioPlayer.replaceCurrentItemWithPlayerItem(playerItem) + audioPlayer.actionAtItemEnd = .Pause + + let durationInSeconds = CMTimeGetSeconds(asset.duration) + circularSlider.maximumValue = CGFloat(durationInSeconds) + let interval = CMTimeMake(1, 4) + audioPlayer.addPeriodicTimeObserverForInterval(interval, queue: dispatch_get_main_queue()) { + [weak self] time in + let seconds = CMTimeGetSeconds(time) + self?.updatePlayerUI(withCurrentTime: CGFloat(seconds)) + } + + self.audioPlayer.play() + } + + + // update the slider position and the timer text + func updatePlayerUI(withCurrentTime currentTime: CGFloat) { + circularSlider.endPointValue = currentTime + let components = NSDateComponents() + components.second = Int(currentTime) + timerLabel.text = dateComponentsFormatter.stringFromDateComponents(components) + } + + func updateTimer() { + let components = NSDateComponents() + components.second = Int(circularSlider.endPointValue) + timerLabel.text = dateComponentsFormatter.stringFromDateComponents(components) + } + + // MARK: - Notification + + func playerItemDidReachEnd(notification: NSNotification) { + if let playerItem: AVPlayerItem = notification.object as? AVPlayerItem { + playerItem.seekToTime(kCMTimeZero) + playerSegmentedControl.selectedSegmentIndex = UISegmentedControlNoSegment + } + } + + /* + // MARK: - Navigation + + // In a storyboard-based application, you will often want to do a little preparation before navigation + override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { + // Get the new view controller using segue.destinationViewController. + // Pass the selected object to the new view controller. + } + */ +} diff --git a/Example/HGCircularSlider/StrangeZero.mp3 b/Example/HGCircularSlider/StrangeZero.mp3 new file mode 100644 index 0000000..d6c3163 Binary files /dev/null and b/Example/HGCircularSlider/StrangeZero.mp3 differ diff --git a/Example/Pods/Target Support Files/Pods-HGCircularSlider_Tests/Info.plist b/Example/Pods/Target Support Files/Pods-HGCircularSlider_Tests/Info.plist index 2243fe6..11db4b7 100644 --- a/Example/Pods/Target Support Files/Pods-HGCircularSlider_Tests/Info.plist +++ b/Example/Pods/Target Support Files/Pods-HGCircularSlider_Tests/Info.plist @@ -2,25 +2,25 @@ - CFBundleDevelopmentRegion - en - CFBundleExecutable - ${EXECUTABLE_NAME} - CFBundleIdentifier - ${PRODUCT_BUNDLE_IDENTIFIER} - CFBundleInfoDictionaryVersion - 6.0 - CFBundleName - ${PRODUCT_NAME} - CFBundlePackageType - FMWK - CFBundleShortVersionString - 1.0.0 - CFBundleSignature - ???? - CFBundleVersion - ${CURRENT_PROJECT_VERSION} - NSPrincipalClass - + CFBundleDevelopmentRegion + en + CFBundleExecutable + ${EXECUTABLE_NAME} + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + ${PRODUCT_NAME} + CFBundlePackageType + FMWK + CFBundleShortVersionString + 1.0.0 + CFBundleSignature + ???? + CFBundleVersion + ${CURRENT_PROJECT_VERSION} + NSPrincipalClass + diff --git a/HGCircularSlider/Classes/MidPointCircularSlider.swift b/HGCircularSlider/Classes/MidPointCircularSlider.swift index 596194c..cc799f1 100644 --- a/HGCircularSlider/Classes/MidPointCircularSlider.swift +++ b/HGCircularSlider/Classes/MidPointCircularSlider.swift @@ -69,7 +69,7 @@ public class MidPointCircularSlider: RangeCircularSlider { } set { let value = newValue - print("\(newValue)") + let interval = (endPointValue - startPointValue) / 2 startPointValue = value - interval endPointValue = value + interval diff --git a/HGCircularSlider/Classes/RangeCircularSlider.swift b/HGCircularSlider/Classes/RangeCircularSlider.swift index 57b7554..21fec2c 100644 --- a/HGCircularSlider/Classes/RangeCircularSlider.swift +++ b/HGCircularSlider/Classes/RangeCircularSlider.swift @@ -143,9 +143,7 @@ public class RangeCircularSlider: CircularSlider { * The default value of this property is 0.5 */ override public var endPointValue: CGFloat { - didSet { - print("endPointValue \(endPointValue)") - + didSet { if oldValue == endPointValue && distance <= 0 { return }