How to make UISlider's “track” invisible?

前端 未结 8 634
感动是毒
感动是毒 2021-02-05 07:44

I\'m trying to emulate Apple\'s \"Slide to Unlock\" feature in my application. I get to this point (image below), but as you can see the UISlider\'s \"track\" is visible and is

8条回答
  •  清酒与你
    2021-02-05 08:15

    There probably isn't a way to hide the track; the "slide to unlock" doesn't behave like a UISlider and is probably a custom control. You might be able to hack the slider control, maybe by setting opacity low (0 will make it hidden and it won't receive touches), but if you go that route you will probably have something break after an OS update. Apple doesn't bend over backwards for compatibility like Microsoft does.

    The right way to do this is with a custom control. It may seem like more work than using a UISlider, but it's not if you compare it against all the time you have spent and/or will spend hacking a UISlider.

    To do it: subclass UIControl. Write your own drawing code to make it look right (you can probably reuse some of whatever you are doing now). Then register for touch events to move the slider handle:

    • UIControlEventTouchDown: if it's on the handle, set a "moving" flag
    • UIControlEventTouchDragInside: if the moving flag is set, move the handle to the touch position; you can just update an instance variable, call setNeedsDisplay to draw it in the new position.
    • UIControlEventTouchUpInside: if moving flag is set, and handle is at end, unlock

    If you want to mimic the real unlock handle, play around with it and see how it behaves. You might need to respond to the events differently (what happens if you drag outside the slider path). But you should be able to get the gist of it.

提交回复
热议问题