iPhone:编程 UISlider 以定位在点击的位置
Posted
技术标签:
【中文标题】iPhone:编程 UISlider 以定位在点击的位置【英文标题】:iPhone:Programming UISlider to position at clicked location 【发布时间】:2010-05-24 06:21:30 【问题描述】:如何在 iPhone 编程中的 UISlider 上设置滑块到点击位置并获取点击位置上的滑块值。 我知道我们可以将滑块拖动到那个位置,但我不想这样做。你能告诉我如何将滑块设置到点击位置吗?这可以吗?
【问题讨论】:
【参考方案1】:这是“留作用户练习”的部分:
- (void) tapped: (UITapGestureRecognizer*) 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];
【讨论】:
【参考方案2】:我这样做的方法是对滑块进行子类化并签入touchesBegan
。如果用户点击拇指按钮区域(我们跟踪)然后忽略点击,但我们会在轨迹栏上的任何其他位置:
:
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self];
// if we didn't tap on the thumb button then we set the value based on tap location
if (!CGRectContainsPoint(lastKnownThumbRect, touchLocation))
self.value = self.minimumValue + (self.maximumValue - self.minimumValue) * (touchLocation.x / self.frame.size.width);
[super touchesBegan:touches withEvent:event];
- (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value
CGRect thumbRect = [super thumbRectForBounds:bounds trackRect:rect value:value];
lastKnownThumbRect = thumbRect;
return thumbRect;
【讨论】:
如果您使用自定义 -setThumbImage,请在 -touchesBegan 中使用self.value = self.minimumValue + (self.maximumValue - self.minimumValue) * ((touchLocation.x - self.currentThumbImage.size.width/2)/ (self.frame.size.width-self.currentThumbImage.size.width));
。【参考方案3】:
您可以简单地向滑块添加一个 UITapGestureRecognizer,然后拉出与其关联的 UIEvent 和 Touches,以找出沿着 UISlider 发生点击的位置。然后将滑块的值设置为此计算值。
更新:
首先,设置您的滑块并向其添加手势识别器。
UISlider *slider = [[[UISlider alloc] init] autorelease];
…
<slider setup>
…
UITapGestureRecognizer *gr = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sliderTapped:)] autorelease];
[slider addGestureRecognizer:gr];
然后实现选择器
- (void)sliderTapped:(UIGestureRecognizer *)gestureRecognizer
<left as a user excercise*>
*提示:Read the docs 找出如何推断 locationInView 并找出滑块应该是什么
【讨论】:
能否大致介绍一下如何在slider中引入UITapGestureRecognizer?【参考方案4】:似乎只是继承 UISlider 并始终返回 true 到 beginTracking 会产生所需的效果。
iOS 10 和 Swift 3
class CustomSlider: UISlider
override func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool
return true
【讨论】:
【参考方案5】:我使用子类 UISlider 代码来监听 ios6 中的 ValueChanged 事件> 以实现对齐网格类型的函数。
这在升级到 iOS7 时被破坏,因为它不再触发 UIControlEventsValueChanged。对于遇到相同问题的任何人,可以通过在 if() 中添加一行来解决,如下所示:
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self];
// if we didn't tap on the thumb button then we set the value based on tap location
if (!CGRectContainsPoint(lastKnownThumbRect, touchLocation))
self.value = self.minimumValue + (self.maximumValue - self.minimumValue) * (touchLocation.x / self.frame.size.width);
[self sendActionsForControlEvents:UIControlEventValueChanged];
[super touchesBegan:touches withEvent:event];
- (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value
CGRect thumbRect = [super thumbRectForBounds:bounds trackRect:rect value:value];
lastKnownThumbRect = thumbRect;
return thumbRect;
希望它可以帮助某人:)
【讨论】:
【参考方案6】:我已经使用了这个代码。获取自: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];
希望对你有帮助。
【讨论】:
以上是关于iPhone:编程 UISlider 以定位在点击的位置的主要内容,如果未能解决你的问题,请参考以下文章
以编程方式在 UITableViewCells 中嵌入多个 UISlider 和 UILabel 控件