Search code examples
iosxcodeswiftuislider

how to add a double thumbed slider in iOS? and why this double slider library no longer works in xcode 7


I have an app that has a double slider, the project works fine in swift 1.2 / XCode 6, the plugin I use is UIXRangeSlider

but the problem is it shows weird errors when using in Swift 2 / XCode 7.2

enter image description here

I tried fixing the errors but they puzzle me, I tried adding override and removing it, with no chance, how to fix these errors? or do you know any good alternative?

EDIT

fixed some errors, I just need help with this please

enter image description here


Solution

  • There are several issues with the code you've linked to:

    1. NSCoding's init(coder: NSCoder) is a failable initializer, so this needs to also be a failable initializer:

      required init?(coder: NSCoder)
      {
          super.init(coder: coder)
          self.commonInit()
      }
      
    2. The methods setInactiveBarImage, setActiveBarImage, setRightThumbImage, setMiddleThumbImage, and setLeftThumbImage conflict with the property setters for the identically named variables in the class (e.g middleThumbImage). The compiler generates methods with a signature matching setVariable: for use in Objective-C. The best way to solve this is to move the method bodies into didSet handlers for each variable, e.g:

      var activeBarImage:UIImage = UIImage() {
          didSet {
              self.activeBarView.removeFromSuperview()
              self.activeBarView = UIImageView(image: self.activeBarImage)
              self.activeBarView.userInteractionEnabled = false
              self.addSubview(self.activeBarView)
              self.orderSubviews()
              self.setNeedsLayout()
          }
      }
      
    3. The touch tracking methods are written incorrectly. The superclass interface accepts UITouch? and UIEvent? - note that both arguments are optionals. To fix the error, change the arguments and handle the optionals:

      override func beginTrackingWithTouch(touch: UITouch?, withEvent event: UIEvent?) -> Bool
      {
          guard let location = touch?.locationInView(self) else { return false }
      
          previousLocation = location
      
          // Hit test the thumb layers
          if leftThumbView.frame.contains(previousLocation)
          {
              trackedElement = .LeftThumb
          }
          else if rightThumbView.frame.contains(previousLocation)
          {
              trackedElement = .RightThumb
          }
          else if middleThumbView.frame.contains(previousLocation)
          {
              trackedElement = .MiddleThumb
          }
      
          return trackedElement != .None
      }
      
      override func continueTrackingWithTouch(touch: UITouch?, withEvent event: UIEvent?) -> Bool
      {
          guard let location = touch?.locationInView(self) else { return false }
      
          // 1. Determine by how much the user has dragged
          let deltaLocation = Double(location.x - previousLocation.x)
          let deltaValue = (Double(maximumValue) - Double(minimumValue)) * deltaLocation / Double(bounds.width /*- thumbWidth*/)
      
          switch trackedElement
          {
          case .LeftThumb:
              handleLeftThumbMove(location, delta: deltaValue)
          case .MiddleThumb:
              handleMiddleThumbMove(location, delta: deltaValue)
          case .RightThumb:
              handleRightThumbMove(location, delta: deltaValue)
          default:
              break
          }
      
          previousLocation = location
      
          return true
      }
      
      override func endTrackingWithTouch(touch: UITouch?, withEvent event: UIEvent?)
      {
          trackedElement = .None
      }
      

    Hope this fixes the problem!