【问题标题】:Get event on UIslider track在 UIslider 轨道上获取事件
【发布时间】:2012-12-17 12:42:24
【问题描述】:

如何在 UISlider 轨道上获取事件?我能够在 UISlider 的按钮上获得事件,但不在轨道上。我该怎么办?

谢谢

【问题讨论】:

  • 您能告诉我们您所说的 Slider track 是什么意思吗?您想要滑块或其他事件的事件吗?
  • 滑块移动的轨道。滑块有两部分拇指和轨道。我正在寻找一个事件,同时点击轨道而不是拇指。
  • 我不认为你可以直接获取这些事件,除非你继承 UISlider 类或者创建你自己的类似于 UISlider 的控制器。

标签: iphone objective-c ios ipad uislider


【解决方案1】:

为此,您需要继承 UISlider 并实现 touches 事件,例如:touchesBegan、touchesEnd、touchesCancelled、touchesMoved 等。

@interface yourSlider:UISlider
@end

@implementation yourSlider
  - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
    }

  - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
    }

  - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
    }

  - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
    {
    }
@end

【讨论】:

  • 嘿。感谢大家的回复。我尝试了子类化,但发现它有点长,虽然有用,方法。我所做的是让拇指图像变大,两边的透明区域相等(等于未使用轨道的长度)。这扩大了触摸区域并解决了我的问题。
【解决方案2】:

继承UISlider 并覆盖其touchesBegan:withEvent: 方法。从触摸事件中获取点值,并通过点的 .x 值相对于滑块的宽度计算它的百分比。

【讨论】:

    【解决方案3】:

    答案是正确的,但这里有一个解决方法,您可以简单地在滑块上​​添加一个等于滑块坐标的清除按钮并检测其上的触摸点,然后将 X 位置转换为滑块值。

        - (IBAction)buttonPressed:(id)sender forEvent:(UIEvent*)event
         {
           UIView *button = (UIView *)sender;
           UITouch *touch = [[event touchesForView:button] anyObject];
           CGPoint location = [touch locationInView:button];
           NSLog(@"Location in button: %f, %f", location.x, location.y); \\ use this x to determine slider's value
    
          }
    

    【讨论】:

      【解决方案4】:

      另一种解决方法:

          UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self             action:@selector(sliderTapped:)];
      gr.delegate=self;
      [Slider addGestureRecognizer:gr];
      
      -(void)sliderTapped:(UIGestureRecognizer*)g{
      UISlider* s = (UISlider*)g.view;
      if (s.highlighted)
          return; 
      CGPoint pt = [g locationInView: s];
      CGFloat value = s.minimumValue + pt.x / s.bounds.size.width * (s.maximumValue - s.minimumValue);
      [s setValue:value animated:YES];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-05-08
        • 1970-01-01
        • 1970-01-01
        • 2011-05-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多