简体   繁体   English

如何检测使用RubyMotion敲击了UISlider?

[英]How to detect that a UISlider has been tapped using RubyMotion?

I have UISlider: 我有UISlider:

slider = UISlider.alloc.initWithFrame([[20, 70)], [260, 40]])

I want to be able to detect when the user clicks on it and know where. 我希望能够检测用户何时单击它并知道在哪里。 I figure that I'd need to use UITapGestureRecognizer or something similar, but I am very new and clueless to all this. 我认为我需要使用UITapGestureRecognizer或类似的工具,但是我很新,对这一切一无所知。

I can get to find out when the slider is tapped with this: 我可以找出滑块何时被点击:

slider.addTarget(self, action:'sliderTaped', forControlEvents:UIControlEventTouchDown)

... but it only works on the thumb, so that's not very useful, I'd rather catch any tap on the slider. ...但是它只能用在拇指上,所以不是很有用,我宁愿抓住滑块上的任何轻按。

Any pointers or links to documentation would be appreciated. 任何指向文档的指针或链接将不胜感激。


Related question/answer, but with objective-c: Clickable UISlider 相关问题/解答,但带有Objective-c:可点击的UISlider

To get notified when the slider has been changed, you need to set up the target/action . 要在更改滑块后得到通知,您需要设置target/action The target is the object containing the method you want called, and the action is the method you want called. target是包含要调用的方法的对象,而action是要调用的方法。 I called my action sliderValueChanged: . 我称我的action sliderValueChanged: You can choose anything. 您可以选择任何东西。 Be sure to append the colon to the name because that tells it to send a pointer to the sender which is the UISlider object. 请确保在名称后加上冒号,因为它告诉它将一个指针发送到作为UISlider对象的发送者。 You need the sender so that you can ask for the value of the slider. 您需要发送者,以便您可以要求滑块的值。

If you don't set a minimumValue and maximumValue for the slider, the values will range from 0.0 to 1.0 . 如果您没有为滑块设置minimumValuemaximumValue ,则值的范围为0.01.0

def viewDidLoad
    slider = UISlider.alloc.initWithFrame([[20, 70], [260, 40]])
    slider.addTarget(self, action:'sliderValueChanged:', forControlEvents:UIControlEventValueChanged)
    self.view.addSubview(slider)
end

def sliderValueChanged(sender)
    puts sender.value
end

As shown, sliderValueChanged will get called continuously as the user moves the slider. 如图所示,随着用户移动滑块, sliderValueChanged将被连续调用。 To be notified only when the user stops touching the slider, set: 要仅在用户停止触摸滑块时得到通知,请设置:

slider.continuous = no

Now, if you want a clickable slider that moves the thumb to where you click, then you need to add a UITapGestureRecognizer to the slider. 现在,如果您想要一个可单击的滑块将拇指移动到单击的位置,则需要UITapGestureRecognizer滑块添加一个UITapGestureRecognizer This code demonstrates this: 此代码演示了这一点:

def viewDidLoad
    slider = UISlider.alloc.initWithFrame([[50, 50], [200, 40]])
    tapGestureRecognizer = UITapGestureRecognizer.alloc.initWithTarget(self, action:'sliderTapped:')
    tapGestureRecognizer.numberOfTapsRequired = 2
    slider.addGestureRecognizer(tapGestureRecognizer)

    slider.continuous = false
    slider.minimumValue = 77
    slider.maximumValue = 129
    slider.addTarget(self, action:'sliderValueChanged:', forControlEvents:UIControlEventValueChanged)
    self.view.addSubview(slider)
end

# This is called when the slider changes either from taps or from moving the thumb
def handle_new_slider_value(value)
    puts "New slider value: #{value}"
end

def sliderValueChanged(slider)
    handle_new_slider_value(slider.value)
end

# These were determined empirically by setting the thumb to the minimum and
# then clicking on the center and printing out gestureRecognizer.locationInView(slider).x
# and then repeating for the maximum.
SLIDER_MIN = 11
SLIDER_MAX = 189

def sliderTapped(gestureRecognizer)
    if gestureRecognizer.state == UIGestureRecognizerStateEnded
        slider = gestureRecognizer.view
        x = gestureRecognizer.locationInView(slider).x
        # Uncomment this to determine values for SLIDER_MIN/SLIDER_MAX
        # puts x
        x = SLIDER_MIN if x < SLIDER_MIN
        x = SLIDER_MAX if x > SLIDER_MAX
        slider_min_val = slider.minimumValue
        slider_max_val = slider.maximumValue

        # Convert from the location in the view to the slider value
        slider.value = slider_min_val + (x - SLIDER_MIN) / (SLIDER_MAX - SLIDER_MIN) * (slider_max_val - slider_min_val)
        handle_new_slider_value(slider.value)
    end
end

Note that this code moves the slider on a double tap. 请注意,此代码双击移动滑块。 I had trouble with just a single tap because if you move the thumb quickly, the code treats it as a tap and snaps the thumb back to its starting position. 我只单击一下就遇到了麻烦,因为如果您快速移动拇指,则代码会将其视为轻击并将拇指恢复到其初始位置。 Set tapGestureRecognizer.numberOfTapsRequired = 1 to play around with the single tap. 设置tapGestureRecognizer.numberOfTapsRequired = 1可以一次点击。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM