Ron*_*nak 6 iphone uislider ios
我想通过点击而不是通过拖动滑块拇指来点击UISlider上的特定点来触发动作.
如何在某个点上点击UISlider来设置滑块值?
一段代码将不胜感激.
Ron*_*nak 36
UISlider *slider = [[[UISlider alloc] init] autorelease];
…
Run Code Online (Sandbox Code Playgroud)
滑块设置
…
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];
}
Run Code Online (Sandbox Code Playgroud)
如果 v1 是一个 uiview,在该位置 uislider\ 后面有纯背景,请尝试 uiview Touchsbegan 函数
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == v1)
{
CGPoint location = [touch locationInView:v1];
NSInteger i = location.x;
}
Run Code Online (Sandbox Code Playgroud)
然后乘以 v1 帧得到 xpoint 的百分比,即如果 v1 帧 x 值 = 50 且 i = 5 表示 10%,然后将百分比值乘以滑块值以更改滑块值
| 归档时间: |
|
| 查看次数: |
6766 次 |
| 最近记录: |