Giter Club home page Giter Club logo

cpslider's Introduction

Description

CPSlider is a drop-in, subclass replacement for UISlider that allows varying scrubbing speeds as the user drags away from the slider thumb, emulating the slider used in the iOS iPod music player. It also includes delegate callbacks to allow an object to be notified of scrubbing speed changes.

CPSlider was created because the only other implementation I could find, Ole Begemann's OBSlider, while top notch, had trouble with my strange use case of starting with a fine scrubbing speed and increasing back to 1.0x as the user dragged down. I also was interested to see if there was a different method by which this could be implemented, without performing the position calculations based on the various thumb and track frames.

Usage

General

Create a CPSlider like you would a normal UISlider:

CPSlider *slider = [[CPSlider alloc] initWithFrame:CGRectMake(18, 166, 284, 23)];
slider.minimumValue = 0.0f;
slider.maximumValue = 1.0f;
[self.view addSubview:slider]
[self.slider addTarget:self action:@selector(sliderValueDidChange:) forControlEvents:UIControlEventValueChanged];

Then you need to set an array containing the Y positions at which the scrubbing speed should change. These are Y positions, measured in points away from the centerline of the slider's frame (positive or negative). They should be sequentially increasing and wrapped in NSNumber objects.

slider.scrubbingSpeedPositions = [NSArray arrayWithObjects:
                                   [NSNumber numberWithInt:0],
                                   [NSNumber numberWithInt:50], 
                                   [NSNumber numberWithInt:125],
                                   [NSNumber numberWithInt:175], nil];

The slider scrubbing speeds should then be set in a similar fashion. These are the rate at which the slider will move relative to the change in X position of the user's touch point. Typical values will be 0.0 to 1.0, although greater than 1.0 is possible. Again, these should be wrapped in an NSNumber.

slider.scrubbingSpeeds = [NSArray arrayWithObjects:
                           [NSNumber numberWithFloat:1.0f],
                           [NSNumber numberWithFloat:0.5f],
                           [NSNumber numberWithFloat:0.25f],
                           [NSNumber numberWithFloat:0.1f], nil];

The delegate can be set if desired, along with the two external options.

slider.delegate = self;
slider.accelerateWhenReturning = YES;  // Defaults to YES, see CPSlider.h for info
slider.ignoreDraggingAboveSlider = YES; // Defaults to YES, see CPSlider.h for info

And one or both of the delegate methods can be implemented.

- (void)slider:(CPSlider *)slider didChangeToSpeed:(CGFloat)speed whileTracking:(BOOL)tracking {
    // Do something with the speed change
    // 'tracking' is YES during touch and changes to NO on touch up
}

- (void)slider:(CPSlider *)slider didChangeToSpeedIndex:(NSUInteger)index whileTracking:(BOOL)tracking {
    // This version reports the index of the speed entry
}

Todo

  • Any ideas?

About

Charles Powell

Give me a shout if you're using this in your project!

cpslider's People

Contributors

cbpowell avatar

Watchers

James Cloos avatar Balázs Faludi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.