UISlider events

2019-03-14 23:40发布

I'm using an UISlider, its updated automatically except the user is touching the sliderbutton. therefore i did this in the function which updates automatically by an NSTimer:

if (!isSliderTouched) {
    [progressSlider setValue: progressValue];
}

How do I track the ending of this event, when the user releases his finger. I want to set isSliderTouched as long as the user interacts with this control.

EDIT: this should be the plot:

  1. user beginns draging/touching UISlider => isSliderTouched = YES
  2. user releases/untouch the UISlider => isSliderTouched = NO

Solution:

UIControlEventTouchDown
UIControlEventTouchUpInside
[progressSlider addTarget:self action:@selector(sliderMoveStart) forControlEvents:UIControlEventTouchDown];

cheers endo

4条回答
Explosion°爆炸
2楼-- · 2019-03-14 23:59

Better way to track if the user is done interacting with UISlider is to use continuous flag.

If you want to trigger slider action only after the action is complete and user has moved his finger off it then use

[blurSlider  addTarget:self action:@selector(blurSliderChanged) forControlEvents:UIControlEventValueChanged];
    blurSlider.continuous = NO;

Otherwise by default its set to YES.

[blurSlider  addTarget:self action:@selector(blurSliderChanged) forControlEvents:UIControlEventValueChanged];
    blurSlider.continuous = YES;
查看更多
小情绪 Triste *
3楼-- · 2019-03-15 00:13

when user touchs the slider you could set a boolean variable flag and make it NO. and in the update method check if the flag is YES or NO.

查看更多
家丑人穷心不美
4楼-- · 2019-03-15 00:17

You can use:

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents

to detect when the touchDown and touchUp events occur. You can set your flag accordingly.

I'm guessing from your code snippet that you are using a UISlider as a progress meter. Have you considered using a UIProgressView instead?

查看更多
时光不老,我们不散
5楼-- · 2019-03-15 00:22

This is marked solved, and yet I'd like to add another solution to this that I feel is cleaner.

if (!progressSlider.highlighted) {
    [progressSlider setValue: progressValue];
}
查看更多
登录 后发表回答