ios - Get the current value of UISlider in drawRect: method -


i'm trying move point drawn in uiview based on value of uislider. code below uiview (subview ?) custom class (windowview) on uiviewcontroller.

windowview.h

#import <uikit/uikit.h>  @interface windowview : uiview  - (ibaction)slidervalue:(uislider *)sender;  @property (weak, nonatomic) iboutlet uilabel *windowlabel;   @end 

windowview.m

#import "windowview.h"  @interface windowview () {     float myval; // thought solution using ivar think wrong }  @end  @implementation windowview  @synthesize windowlabel; - (id)initwithframe:(cgrect)frame {     self = [super initwithframe:frame];     if (self) {         // initialization code     }     return self; }  - (void)slidervalue:(uislider *)sender {     myval = sender.value;     windowlabel.text = [nsstring stringwithformat:@"%f", myval]; }  - (void)drawrect:(cgrect)rect {     // need current value of slider in drawrect: , update position of circle slider moves     uibezierpath *circle = [uibezierpath bezierpathwithovalinrect:cgrectmake(myval, myval, 10, 10)];     [circle fill]; }  @end 

ok, need store slider value in instance variable , force view redraw.

windowview.h:

#import <uikit/uikit.h>  @interface windowview : uiview {     float _slidervalue;   // current value of slider }  // should called slidervaluechanged - (ibaction)slidervalue:(uislider *)sender;  @property (weak, nonatomic) iboutlet uilabel *windowlabel; @end 

windowview.m (modified methods only):

// should called slidervaluechanged - (void)slidervalue:(uislider *)sender {     _slidervalue = sender.value;     [self setneedsdisplay];   // force redraw }  - (void)drawrect:(cgrect)rect {     uibezierpath *circle = [uibezierpath bezierpathwithovalinrect:cgrectmake(_slidervalue, _slidervalue, 10, 10)];     [circle fill]; } 

you want initialise _slidervalue useful in view's init method.

also _slidervalue isn't name want choose; perhaps _circleoffset or such.


Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -