iPhone:Programming UISlider to position at clicked location

后端 未结 6 451
北荒
北荒 2020-12-09 13:59

How to set the slider to clicked position and get the slider value on the clicked location on UISlider in iPhone programming. i know we can drag the slider to that position

6条回答
  •  囚心锁ツ
    2020-12-09 14:14

    I have used this code. Get from: http://imagineric.ericd.net/2012/11/15/uislider-touch-to-set-value/

    UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sliderTapped:)];
    [slider addGestureRecognizer:gr];
    
    
    
    - (void)sliderTapped:(UIGestureRecognizer *)g {
            UISlider* s = (UISlider*)g.view;
            if (s.highlighted)
                return; // tap on thumb, let slider deal with it
            CGPoint pt = [g locationInView: s];
            CGFloat percentage = pt.x / s.bounds.size.width;
            CGFloat delta = percentage * (s.maximumValue - s.minimumValue);
            CGFloat value = s.minimumValue + delta;
            [s setValue:value animated:YES];
        }
    

    Hope this can help you.

提交回复
热议问题