Search code examples
iosobjective-cpopoveruislider

how to set popover position horizontally


i am making an app i which i am using slider and on changing the position of slider value of slider is also sliding as well, now i am stuck that i want to set when i change postion of slider in horizontall form then i need to change that popover postion also in horizontall form. I am attaching a screen shot so that u people better understand it and this is my project link https://www.dropbox.com/s/n0fl34h6t8jlazn/CustomSlider.zip?dl=0,i want this floating popover view in horizontal form

below is my sample code on 1 view and i am changing my uislider class to anpopoverslider:

-(void)constructSlider {
    _popupView = [[ANPopoverView alloc] initWithFrame:CGRectZero];
    _popupView.backgroundColor = [UIColor clearColor];
    _popupView.alpha = 0.0;
    [self addSubview:_popupView];
}

and below is my ANpopoverslider

 #import "ANPopoverSlider.h"

    @implementation ANPopoverSlider

    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code
            [self constructSlider];
        }
        return self;
    }

    -(id)initWithCoder:(NSCoder *)aDecoder {
        self = [super initWithCoder:aDecoder];
        if (self) {
            [self constructSlider];
        }
        return self;
    }

    #pragma mark - UIControl touch event tracking
    -(BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
        // Fade in and update the popup view
        CGPoint touchPoint = [touch locationInView:self];

        // Check if the knob is touched. If so, show the popup view
        if(CGRectContainsPoint(CGRectInset(self.thumbRect, -12.0, -12.0), touchPoint)) {
            [self positionAndUpdatePopupView];
            [self fadePopupViewInAndOut:YES];
        }

        return [super beginTrackingWithTouch:touch withEvent:event];
    }

    -(BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
        // Update the popup view as slider knob is being moved
        [self positionAndUpdatePopupView];
        return [super continueTrackingWithTouch:touch withEvent:event];
    }

    -(void)cancelTrackingWithEvent:(UIEvent *)event {
        [super cancelTrackingWithEvent:event];
    }

    -(void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
        // Fade out the popup view
        [self fadePopupViewInAndOut:NO];
        [super endTrackingWithTouch:touch withEvent:event];
    }


    #pragma mark - Helper methods
    -(void)constructSlider {
        CGRect frame = CGRectMake(150, 230, 300.0, 10.0);

        _popupView = [[ANPopoverView alloc] initWithFrame:frame];
        _popupView.backgroundColor = [UIColor clearColor];
        _popupView.alpha = 0.0;
        [self addSubview:_popupView];
    }

    -(void)fadePopupViewInAndOut:(BOOL)aFadeIn {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.5];
        if (aFadeIn) {
            _popupView.alpha = 1.0;
        } else {
            _popupView.alpha = 0.0;
        }
        [UIView commitAnimations];
    }

    -(void)positionAndUpdatePopupView {
        CGRect zeThumbRect = self.thumbRect;
        CGRect popupRect = CGRectOffset(zeThumbRect, 0, -floor(zeThumbRect.size.height * 1.5));
        _popupView.frame = CGRectInset(popupRect, -20, -10);
        _popupView.value = self.value;
    }


    #pragma mark - Property accessors
    -(CGRect)thumbRect {
        CGRect trackRect = [self trackRectForBounds:self.bounds];
        CGRect thumbR = [self thumbRectForBounds:self.bounds trackRect:trackRect value:self.value];
        return thumbR;
    }

    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    - (void)drawRect:(CGRect)rect
    {
        // Drawing code
    }
    */

    @end

and here is my popover class

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.font = [UIFont boldSystemFontOfSize:15.0f];

        UIImageView *popoverView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sliderlabel.png"]];
        [self addSubview:popoverView];

         textLabel = [[UILabel alloc] init];
        textLabel.backgroundColor = [UIColor clearColor];
        textLabel.font = self.font;
        textLabel.textColor = [UIColor colorWithWhite:1.0f alpha:0.7];
        textLabel.text = self.text;
        textLabel.textAlignment = NSTextAlignmentCenter;
        textLabel.frame = CGRectMake(0, -2.0f, popoverView.frame.size.width, popoverView.frame.size.height);
        [self addSubview:textLabel];

    }
    return self;
}

-(void)setValue:(float)aValue {
    _value = aValue;
    self.text = [NSString stringWithFormat:@"%4.2f", _value];
    textLabel.text = self.text;
    [self setNeedsDisplay];
}

Solution

  • I tried the sample project and work with simple function

    @property (strong, nonatomic) IBOutlet UISlider *slider;  // make the getter and setter for Slider
    @property (strong, nonatomic) IBOutlet UILabel *lblText;  // make the getter and setter for label
    
    
    
    - (void)viewDidLoad {
    [super viewDidLoad];
    
    // set verticical of UIslider
    
    CGAffineTransform trans = CGAffineTransformMakeRotation(M_PI * 0.5);
    self.slider.transform = trans;
    
    // get the events of UISlider   
    [self.slider addTarget:self
                  action:@selector(sliderDidEndSliding:)
        forControlEvents:(UIControlEventTouchUpInside | UIControlEventTouchUpOutside)];
    
    }
    // this method used for hide the label after  changed the value
    - (void)sliderDidEndSliding:(NSNotification *)notification {
      NSLog(@"Slider did end sliding...");
      [self.lblText removeFromSuperview];
    }
    
    
    
    
    // the slider value change method
    - (IBAction)slider:(UISlider*)sender
    {
    // add the subview the lable to slider
    [self.slider addSubview:self.lblText];
    self.lblText.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"sliderlabel.png"]];
    self.lblText.text = [NSString stringWithFormat:@"%4.2f",sender.value];
    
    // change the label position depend upon slider move
    self.lblText.center = CGPointMake(self.slider.value*self.slider.bounds.size.width,80);
    [self.lblText setTransform:CGAffineTransformMakeRotation(-M_PI / 2)];
    
    }
    

    here I attached the sample project for UISlider the download link