Uislider slider control-iOS development

Source: Internet
Author: User
Tags uicontrol
Statement

You are welcome to repost this article, but please respect the author's Labor achievements. repost this article and keep the statement in this box. Thank you.
Article Source: http://blog.csdn.net/iukey

The slider on the PC is ugly, because we can only drag it with the mouse. However, when jobs transplanted it to iOS, everything became cool, because we could drag it with our fingers. This was a wonderful feeling.

The slider provides a visible range adjustment method. You can drag a slider to change its value and configure it to fit different value ranges. You can set the range of the slider value, add slices at both ends, and make various adjustments to make it more beautiful. The slider is very suitable for selecting values that are in a large range (but not accurate), such as volume setting and sensitivity control.

I. Create

The slider is a standard uicontrol. We can create it through code. The width and height of the slider will be ignored, and the height of the slider will be ignored (but the width will not ):

Uislider * myslider = [[uislider alloc] initwithframe: cgrectmake (20.0, 10.0, 200.0, 0.0)]; // set the height to 0.

Ii. Set the range and default value

When the slider is created, we need to set the slider range. If you do not set it, the default value between 0.0 and 1.0 will be used. Uislider provides two attributes to set the range: mininumvalue and maxinumvalue:

Myslider. mininumvalue = 0.0; // lower limit myslider. maxinumvalue = 50.0; // Upper Limit

You can also set a default value for the slider:

mySlider.value = 22.0;

3. Add images at both ends

The slider can display images in any segment. After adding an image, the slide bar will be shortened, so remember to add the slider width when creating the image to adapt to the image.

[ mySlider setMininumTrackImage: [ UIImage applicationImageNamed:@"min.png" ] forState: UIControlStateNormal ];[ mySlider setMaxinumTrackImage: [ UIImage applicationImageNamed:@"max.png" ] forState: UIControlStateNormal ];

You can display different images based on different statuses of the slider. The following shows the available status:

Uicontrolstatenormal

Uicontrolstatehighlighted

Uicontrolstatedisabled

Uicontrolstatedisabled

Uicontrolstateselected

Iv. display controls

[Parentview addsubview: myslider]; // Add to parent view or [self. navigationitem. titleview addsubview: myslider]; // Add to navigation bar

5. Read control values

float value = mySlider.value;

Vi. Notification

To receive a notification when the slider value changes, you can use the addtarget method of the uicontrol class to add an action for the uicontroleventvaluechanged event.

[ mySlider addTarget:self action:@selector(sliderValueChanged:) forControlEventValueChanged ];

As long as the slider is parked (note that it is parked, if you want to trigger it in the drag, please refer to the following article) to a new position, your action method will be called:

-(Void) slidervaluechanged :( ID) sender {uislider * control = (uislider *) sender; If (control = myslider) {float value = control. value;/* add your own processing code */}}

If you want to trigger the slider during dragging, you need to set the continuos attribute of the slider:

mySlider.continuous = YES ;

The simplest example of this notification is to display the slider value in real time. It is strange that the slider value displayed by Apple is a private API (setshowvalue). Private is private, and I don't need it anymore. We can use a uilabel to display the value. Every time the above method is triggered, the label value is changed. Isn't it displayed in real time? Of course, we can do more than just this. There are more cool things that can be implemented, depending on your means and imagination.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.