我想通过点击而不是拖动滑块拇指在 UISlider 上的特定点点击时触发一个动作。
如何在某个点点击 UISlider 来设置滑块值?
一段代码将不胜感激。
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];
}
如果 v1 是一个 uiview,在那个地方 uislider\ 后面有纯背景,请尝试 uiview touchesbegan 函数
- (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 帧 x 值 =50 且 i = 5 表示 10%,然后将百分比值乘以滑块值以更改滑块值