How to make UISlider's “track” invisible?

前端 未结 8 655
感动是毒
感动是毒 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:00

    Answer by @Arjay Waran is the best in my opinion.

    Heres the Swift 3.0 Version of it.

    slider.minimumTrackTintColor = UIColor.clear
    slider.maximumTrackTintColor = UIColor.clear
    

    Cheers

    0 讨论(0)
  • 2021-02-05 08:01

    Looks like just setting the tint color to clear does it...

        [slider setMinimumTrackTintColor:[UIColor clearColor]];
        [slider setMaximumTrackTintColor:[UIColor clearColor]];
    
    0 讨论(0)
  • 2021-02-05 08:10

    In answer to the stated question, you can set a transparent 1px png for the minimum and maximum track images.

    0 讨论(0)
  • 2021-02-05 08:11

    Actually I just figured it out. Here's what I did:

    UIImage *sliderMinimum = [[UIImage imageNamed:@"clearTrack.png"] stretchableImageWithLeftCapWidth:4 topCapHeight:0];
    [slider setMinimumTrackImage:sliderMinimum forState:UIControlStateNormal];
    UIImage *sliderMaximum = [[UIImage imageNamed:@"clearTrack.png"] stretchableImageWithLeftCapWidth:4 topCapHeight:0];
    [slider setMaximumTrackImage:sliderMaximum forState:UIControlStateNormal];
    

    clearTrack.png is just a clear slider image I made.

    Now I have this: yay! alt text

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-05 08:17

    here's an even easier way. No need to create images, just instantiate an empty UIImage class :P

    UIImage *clearImage = [[UIImage alloc] init];
    [self.slider setMinimumTrackImage:clearImage forState:UIControlStateNormal];
    [self.slider setMaximumTrackImage:clearImage forState:UIControlStateNormal];
    
    0 讨论(0)
提交回复
热议问题