Rotating an object on a touch event in kivy

前端 未结 2 897
天命终不由人
天命终不由人 2021-01-02 07:43

I\'m working on making a circle that will spin kind of like a large dial. Currently, I have an arrow at the top to show which direction the dial is facing. I\'d like its b

2条回答
  •  太阳男子
    2021-01-02 08:02

    You can use GearTick from garden which is a rotating slider. It's not exactly what you need but can be adapted for your needs. "By default it allows rotation anti-clockwise you probably would need it to go clockwise"(Update: The widget now has a orientation property that can be set to 'clockwise' or 'anti-clockwise').

    You would need to manage the spring back and stopping at the "finger stop".

    The example at the ends manage spring back using animation, however you still need to manage/implement the finger stop functionality.

    https://github.com/kivy-garden/garden.geartick

    Usage::

    Python::

    from kivy.garden.geartick import GearTick
    parent.add_widget(GearTick(range=(0, 100)))
    

    kv::

    BoxLayout:
        orientation: 'vertical'
        GearTick:
            id: gear_tick
            zoom_factor: 1.1
            # uncomment the following to use non default values
            #max: 100
            #background_image: 'background.png'
            #overlay_image: 'gear.png'
            #orientation: 'anti-clockwise'
            on_release:
                Animation.stop_all(self)
                Animation(value=0).start(self)
        Label:
            size_hint: 1, None
            height: '22dp'
            color: 0, 1, 0, 1
            text: ('value: {}').format(gear_tick.value)
    

    enter image description here

    To install::

    pip install kivy-garden
    garden install geartick
    

    Working Example that you can copy paste::

    from kivy.lang import Builder
    from kivy.app import runTouchApp
    from kivy.garden.geartick import GearTick
    runTouchApp(Builder.load_string('''
    #:import Animation kivy.animation.Animation
    GridLayout:
        cols: 2
        canvas.before:
            Color:
                rgba: 1, 1, 1, 1
            Rectangle:
                size: self.size
                pos: self.pos
        BoxLayout:
            orientation: 'vertical'
            GearTick:
                id: gear_tick
                zoom_factor: 1.1
                # uncomment the following to use non default values
                #max: 100
                #background_image: 'background.png'
                #overlay_image: 'gear.png'
                #orientation: 'anti-clockwise'
                on_release:
                    Animation.stop_all(self)
                    Animation(value=0).start(self)
            Label:
                size_hint: 1, None
                height: '22dp'
                color: 0, 1, 0, 1
                text: ('value: {}').format(gear_tick.value)
    '''))
    

提交回复
热议问题