Reputation: 142
A very basic version of what I want to do is have a slider and a view within a view controller. The slider signifies the number of sides on a polygon, as you move the slider, a shape is drawn in the subview.
A very simple push in the right direction is all I need. I just am unsure of how the interaction works between view controllers and views.
Upvotes: 0
Views: 109
Reputation: 154583
Here you go. You create a custom view to draw a polygon. When its sides
property is set, it calls setNeedsDisplay
on itself which tells iOS to call drawRect
.
class PolygonView: UIView {
var sides: Int = 0 {
didSet {
self.setNeedsDisplay()
}
}
override func drawRect(rect: CGRect) {
// Draw polygon using sides
}
}
In your ViewController
, update sides
on the polygonView
when the slider value changes:
class ViewController: UIViewController {
@IBOutlet weak var polygonView: PolygonView!
@IBAction func sliderChanged(sender: UISlider) {
let sides = Int(sender.value)
polygonView.sides = sides
}
}
So, when the slider
is moved, sliderChanged
will be called. The value of the slider
is read and saved to the sides
property of the polygonView
. didSet
gets called when sides
is set, and the polygonView
calls setNeedsDisplay
on itself. This tells iOS that the view needs to be drawn, so iOS calls drawRect
on the polygonView
.
Upvotes: 1