Mar*_*ell 15 iphone uiscrollview uislider
我有一个UISlider视图的一部分加载到UIScrollView启用分页.我注意到了一个意想不到的行为.如果用户尝试快速使用滑块(即按下并移动),它会"激活"滚动视图,从而导致页面切换.但是,如果按住一秒钟滑块"激活",则可以调整滑块值.这种行为是不可取的.
UISlider加载到响应中时,响应的最佳方法是UIScrollView什么?我已经考虑添加一个"阻止"视图,只是吃掉滑块下面的触摸事件,但不确定这是否是最好的方法.
小智 44
这方面没有必要进行点击测试UIScrollView.因为延迟是由它UIScrollView自己设定的.子类化和实现自定义hitTest:withEvent:将无济于事,因为它仍然会被延迟触发.
我搜索了几个小时的优雅解决方案,因为我想在ios应用程序切换器中模拟苹果自己的volumelider.
诀窍:
yourScrollView.delaysContentTouches = NO;
Run Code Online (Sandbox Code Playgroud)
不幸的是,这会禁用沿UISliders轨道的事件,因此对于这部分你UIScrollView不会触发任何touchevents,因为它们首先被滑块捕获.
要传递除UISliders拇指矩形之外的touchevent,你必须子类UISlider并添加以下内容:
// get the location of the thumb
- (CGRect)thumbRect
{
CGRect trackRect = [self trackRectForBounds:self.bounds];
CGRect thumbRect = [self thumbRectForBounds:self.bounds
trackRect:trackRect
value:self.value];
return thumbRect;
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
CGRect thumbFrame = [self thumbRect];
// check if the point is within the thumb
if (CGRectContainsPoint(thumbFrame, point))
{
// if so trigger the method of the super class
NSLog(@"inside thumb");
return [super hitTest:point withEvent:event];
}
else
{
// if not just pass the event on to your superview
NSLog(@"outside thumb");
return [[self superview] hitTest:point withEvent:event];
}
}
Run Code Online (Sandbox Code Playgroud)
让您的滚动视图检测滑块占用的区域上的触摸(覆盖hitTest:withEvent:,您可能需要子类化UIScrollView)。如果检测到对所述区域的触摸,请告诉您的滚动视图立即将触摸传递到滑块。
| 归档时间: |
|
| 查看次数: |
9777 次 |
| 最近记录: |