我想通过点击而不是通过拖动滑块来点击UISlider上的特定点来触发动作。
如何在一个点上点击UISlider来设置滑块值?
一段代码将不胜感激。
答案 0 :(得分:36)
UISlider *slider = [[[UISlider alloc] init] autorelease];
…
滑块设置
…
UITapGestureRecognizer *gr = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sliderTapped:)] autorelease];
[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];
}
答案 1 :(得分:2)
尝试使用uiview touchesbegan函数,如果v1是一个uiview,后面有一个简单的背景uislider \
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == v1)
{
CGPoint location = [touch locationInView:v1];
NSInteger i = location.x;
}
然后通过乘以v1帧获得xpoint的百分比,即v1 frame x value = 50和i = 5means 10%,然后将百分比值乘以滑块值以更改滑块值