Way to make a UIButton continuously fire during a press-and-hold situation?

后端 未结 4 1248
伪装坚强ぢ
伪装坚强ぢ 2020-11-29 19:21

You know how Mario just keeps running to the right when you press and hold the right-button on the D-Pad? In the same manner, I want my UIButton to continuously fire its act

4条回答
  •  忘掉有多难
    2020-11-29 20:12

    For me the following works:

    1. Create a button.
    2. Create 2 methods (stop touching and start touching) either in view controller or to a subclass.
    3. Add 3 Control Events. Touch Up Inside and Touch Drag Exit that both of them go to stop touching method and Touch Down goes with start touching method.
    4. When start touching method invokes we should start an NSTimer with interval approximately 0.2 (it's up to you how fast you would like to be invoked), repeat true and as a selector a method that you want to be invoked (having the actual stuff you want to execute when user hits the button).
    5. When stop touching method invokes we should invalidate timer (.invalidate()) and assign timer as nil.

    That's all!

提交回复
热议问题