Summary of UISlider Slider assembly usage in IOS _ios

Source: Internet
Author: User
Tags uicontrol

The slider on the PC is very ugly, because we can only drag him through the mouse. But when jobs transplanted it to iOS it became cool because we could drag it through our fingers, and that was a good feeling.
The slider provides a visible way for the user to make range adjustments, and the user can change its value by dragging a slider, and it can be configured to fit in a different domain. You can set the range of the slider value, or you can add pictures at both ends, and make a variety of adjustments to make it more beautiful. Sliders are ideal for use in a wide range of (but imprecise) values, such as volume setting, sensitivity control, and so on.
First, create
the slider is a standard uicontrol. We can create by code, as the width and height of the switch (Uiswitch) are ignored. The height of the slider is ignored (but not the width):

Copy Code code as follows:

uislider* Myslider = [[UISlider alloc] Initwithframe:cgrectmake (20.0,10.0,200.0,0.0)];//height is set to 0.

Ii. set range and default value
At the end of the creation we have to set the slider range, if you do not set, then use the default value between 0.0 to 1.0. UISlider provides two properties to set the range: Mininumvalue and Maxinumvalue:
Copy Code code as follows:

Myslider.mininumvalue = Lower 0.0;//
Myslider.maxinumvalue = 50.0;//Upper Limit

You can also set a default value for the slider:
Copy Code code as follows:

Myslider.value = 22.0;

Three, add pictures at both ends
The slider can display an image in any section. Adding an image causes the slider to shorten, so remember to increase the width of the slider to fit the image when you create it.
Copy Code code as follows:

[Myslider setmininumtrackimage: [uiimage applicationimagenamed:@ "Min.png"] forstate:uicontrolstatenormal];
[Myslider setmaxinumtrackimage: [uiimage applicationimagenamed:@ "Max.png"] forstate:uicontrolstatenormal];

You can display different images depending on the various states of the slider. The following are the available states:
Copy Code code as follows:

UIControlStateNormal
uicontrolstatehighlighted
Uicontrolstatedisabled
Uicontrolstatedisabled
uicontrolstateselected

Four, display control
Copy Code code as follows:

[Parentview Addsubview:myslider];//Add to Parent view

Or
Copy Code code as follows:

[Self.navigationItem.titleView addsubview:myslider];//add to navigation bar

v. Reading control values
Copy Code code as follows:

float value = myslider.value;

VI. Notification
To receive notification when the slider value changes, you can add an action to the Uicontroleventvaluechanged event using the Addtarget method of the Uicontrol class.
Copy Code code as follows:

[Myslider addtarget:self Action: @selector (slidervaluechanged:) forcontroleventvaluechanged];

As long as the slider park (note is parked, if you want to trigger in the drag, please look at the text) to the new location, your action method will be called:
Copy Code code as follows:

-(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 in the drag, you need to set the Continuos property of the slider:
Copy Code code as follows:

myslider.continuous = YES;

The simplest example of this notice is to display the value of the slider in real time, and it's strange that the value of the slider is a proprietary API (Setshowvalue), private is private, I don't need to be. Can we use a uilabel to display the value and change the value of the label each time the above method is triggered, so that we can display it in real time? Of course we can do more than this, there are more cool things can be achieved, it depends on your means and imagination.

Seven, UISlider solution to the gap between the two sides
I've done a player before and there's a gap on both sides of the volume bar. When I went to Sina for an interview, the interviewer mentioned the knowledge point. I've been on the StackOverflow for a long time and finally found a way to share it with iOS beginners.
This method of rewriting UISlider

Copy Code code as follows:

-(CGRect) Thumbrectforbounds: (cgrect) Bounds Trackrect: (cgrect) Rect value: (float) value
{
rect.origin.x = rect.origin.x-10;
Rect.size.width = Rect.size.width +20;
Return Cgrectinset ([Super Thumbrectforbounds:bounds Trackrect:rect Value:value], 10, 10);
}

Of course, there are several ways to rewrite the uislider.
Copy Code code as follows:

-(CGRect) Trackrectforbounds: (CGRect) bounds
{
bounds.origin.x=15;
BOUNDS.ORIGIN.Y=BOUNDS.SIZE.HEIGHT/3;
BOUNDS.SIZE.HEIGHT=BOUNDS.SIZE.HEIGHT/5;
bounds.size.width=bounds.size.width-30;
return bounds;
}
-(CGRect) Minimumvalueimagerectforbounds: (CGRect) bounds;
-(CGRect) Maximumvalueimagerectforbounds: (CGRect) bounds;

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.