Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Changing content in UIView based on slider

I created a Gauge using AppCode. Depending on the input, the gauge arrow in the middle rotates. I want to use a slider in Swift to determine how much the arrow in the gauge should rotate. If I hard code the input the arrow rotates to the proper position. For example, if I input 0.5, then the arrow points to the centre of the gauge.

My problem is that my UIView never updates the arrow depending on the new slider position. The arrow is always positioned depending on the UISlider's 'Current' value. How can I get my UIView to update in real time with respect to my UISlider.

I have a feeling am supposed to put my 'drawRect' function in the 'ratingSliderChanged' action function. When i try to do this,my gauge never appears.

  import Foundation
  import UIKit

   class RatingViewController: UIView {

  var sliderValue: CGFloat = 0.0

  @IBOutlet var newView:UIView!

  @IBAction func ratingSliderChanged(sender: UISlider){
   sliderValue = CGFloat(sender.value)
   println(sliderValue)
  }

 override func drawRect(rect: CGRect) {

  RatingGaugeStyleKit.drawCanvas1(frame: self.bounds, arrowAngle: sliderValue)

 }
}

Any Suggestions?

Thank You

1 Answer

So I managed to do it with the help of someone at AppCode.

Basically because the image is dynamic, I need to call ''' func setNeedsDisplay() '''

when the UISlider changes. This way, it makes sure to update my UIView when a parameter in my drawing changes. In this case, my parameter is the value from the UISlider.

Hope my explanation is clear for anyone needing this in the future.