Uiviewcontroller.m
1 #import "ViewController.h"2 #import "CustomerSlider.h"3 @interfaceViewcontroller ()4 5 @end6 7 @implementationViewcontroller8 9- (void) Viewdidload {Ten [Super Viewdidload]; One ACustomerslider *slider=[customersliderNew]; -Slider.frame=cgrectmake (0, the, the, $); - //is it possible to touch more? theSlider.multipletouchenabled=YES; -Slider.backgroundcolor=[Uicolor Orangecolor]; - [Self.view Addsubview:slider]; -}
CUSTOMERSLIDER.M (inheriting UIView)
1 #import "CustomerSlider.h"2 3 @implementationCustomerslider4 5-(instancetype) init6 {7 return[self initwithframe:cgrectzero];8 }9-(Instancetype) initWithFrame: (CGRect) FrameTen { One if(self=[Super Initwithframe:frame]) { A //Create a new slider with view and set the position and size -UIView *progress = [[UIView alloc] Initwithframe:cgrectmake (0,0, Frame.size.width,Ten)]; - //take the center point of the screen the Cgpoint Center; - //take the center point of the x-axis -center.x=Cgrectgetmidx (frame); - //take the center point of the y-axis +center.y=Cgrectgetmidy (frame); - //set the position of the slider to the center point of the screen +Progress.center=Center; A //Set Background color atProgress.backgroundcolor=[Uicolor Graycolor]; - //set Label to -progress.tag= -; - //Add to view - [self addsubview:progress]; - in //new slider with button to set the type of button -UIButton *thumb=[UIButton Buttonwithtype:uibuttontypecustom]; to //set the tab to 101; +thumb.tag=101; - //Set Background color theThumb.backgroundcolor=[Uicolor Redcolor]; * //set position and size $Thumb.frame=cgrectmake (0,0, -, -);Panax Notoginseng //Set rounded corners -thumb.layer.cornerradius= -/2; the //set the slider as the center point +Thumb.center=Center; A //Add to view the [self addsubview:thumb]; + } - returnSelf ; $ } $ --(void) Setframe: (CGRect) Frame - { the [Super Setframe:frame]; - //get the above slider barWuyiUIView *progress=[self Viewwithtag: -]; the //set position and size -Progress.frame=cgrectmake (0,0, Frame.size.width,Ten); Wu Cgpoint Center; -center.x=Cgrectgetmidx (self.bounds); Aboutcenter.y=Cgrectgetmidy (self.bounds); $Progress.center=Center; - //Get Slider -UIView *thumb=[self Viewwithtag:101]; - //Set Center point 7 AThumb.center=Center; + } the //click Method --(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event $ { the //You can click on any object theUitouch *touch=[touches anyobject]; the //when setting the Click Object for itself, get the coordinate position of the click theCgpoint p=[Touch locationinview:self]; - //gets the center point of the slider at this time inCgpoint center=[self Viewwithtag:101].center; the the //determines whether the y-coordinate of the click Point is more than 15 from the slider, if more than 15 returns, the slider does not move, if not more than 15, the slider moves on the slider bar About //the absolute value of the macro defines ABS () (the upper or lower part of the slider, and the other side is negative, so use the absolute value) the if(ABS (P.Y-CENTER.Y) > the ) { the return; the } + //The y-coordinate of the click Point is not more than 15 from the slider to assign the x-coordinate of the click Point to the X-coordinates of the slider (the slider moves according to the x-axis change) -center.x=p.x; the[Self Viewwithtag:101].center=Center;Bayi //Output Touch points theNSLog (@"begin,touches ct=%d", touches.count); the - } - //Sliding Method the-(void) touchesmoved: (Nsset *) touches withevent: (Uievent *)Event the { the //Output Touch points theNSLog (@"moved,touches ct=%d", touches.count); -Uitouch *touch=[touches anyobject]; theCgpoint p=[Touch locationinview:self]; theCgpoint center=[self Viewwithtag:101].center; the 94 //absolute value of macro definition abs () the if(ABS (P.Y-CENTER.Y) > the ) { the return; the }98center.x=p.x; About[Self Viewwithtag:101].center=Center; - 101 }102 //method to end sliding103-(void) touchesended: (Nsset *) touches withevent: (Uievent *)Event104 { the //Output Touch points106NSLog (@"end,touches ct=%d", touches.count);107 }108 @end
Customizing the UISlider point of touch