Search code examples
objective-ciosuislideruibezierpath

iOS - How to control UIBezierPath drawing with UISlider?


I want to control the drawing of my UIBezierPath using a UISlider.

The aim is to be able to scrub the timeline (UISlider) and cause the line (UIBezierPath) to draw based on the current position of the slider.

So that when I scrub forward the path renders forward & when I scrub back the path renders backwards.

I have the code to render the path drawing in both directions based on this sample code which I added reverse path to (https://github.com/ole/Animated-Paths)

The main thing I am struggling to understand is how can I have the path render when I move the slider since I cant really use animations as they would not update with the sliders movement..

Cheers Damien


Solution

  • So I found that I was unable to use animations to achieve what I wanted so I used a custom UIView subclass and implemented in its drawRect method the code to handle the animations

    Basically I set a index in the subclass that controlled how many of the points the drawrect method draws.

    This gave me the desired approach and allowed me to change the value based on the slider.

    For the drawing I used CGContextMoveToPoint & CGContextAddLineToPoint to render the paths

    Cheers