UIButton with hold down action and release action

2020-02-08 06:03发布

I want to create a UIButton that can be held down, when held down it calls the "hold down" action once. when it is released, calls the "hold was release" action.

This code isn't working correctly because the touch can move inside the button and the events aren't triggered in correct order

[button handleControlEvent:UIControlEventTouchDown withBlock:^{
        [self performMomentaryAction:PXActionTypeTouchDown];
    }];
[button handleControlEvent:UIControlEventTouchUpInside withBlock:^{
        [self performMomentaryAction:PXActionTypeTouchUp];
    }];

handle control events is based on UIBUtton+block implementation

7条回答
Animai°情兽
2楼-- · 2020-02-08 06:19

try this

  UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  aButton.frame = CGRectMake(xValue, yValue, 45, 45);
  [aButton addTarget:self action:@selector(holdDown) forControlEvents:UIControlEventTouchDown];
  [aButton addTarget:self action:@selector(holdRelease) forControlEvents:UIControlEventTouchUpInside];


 - (void)holdDown
  {
     NSLog(@"hold Down");
  }

  - (void)holdRelease
  {
      NSLog(@"hold release");

  }

for NSPratik's case: u can use the event UIControlEventTouchUpOutside If user long press button and after some time, instead of releasing the finger, user will move his/her finger out of the bounds of button. just add one more event.

  UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  aButton.frame = CGRectMake(xValue, yValue, 45, 45);
  [aButton addTarget:self action:@selector(holdDown) forControlEvents:UIControlEventTouchDown];
  [aButton addTarget:self action:@selector(holdRelease) forControlEvents:UIControlEventTouchUpInside];
  [aButton addTarget:self action:@selector(holdReleaseOutSide) forControlEvents:UIControlEventTouchUpOutside]; //add this for your case releasing the finger out side of the button's frame

 //add this method along with other methods 
  - (void)holdReleaseOutSide
  {
     NSLog(@"hold release out side");
  }

Swift Version

 var  aButton:UIButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
 aButton.frame = CGRectMake(xValue,yValue, 45, 45)
 aButton.setTitle("aButton", forState: UIControlState.Normal)
 aButton.backgroundColor = UIColor.greenColor()
 aButton.addTarget(self, action: Selector("holdRelease:"), forControlEvents: UIControlEvents.TouchUpInside);
 aButton.addTarget(self, action: Selector("HoldDown:"), forControlEvents: UIControlEvents.TouchDown)
 self.addSubview(aButton)

 //target functions   
 func HoldDown(sender:UIButton)
 {
    print("hold down")
 }

 func holdRelease(sender:UIButton)
 {
    print("hold release")
 }
查看更多
狗以群分
3楼-- · 2020-02-08 06:23

You need to connect following two events 1.Touch Down ,2.Touch Up Inside to it's IBAction Methods .It will be fun in Interface Builder. But you can do it programmatically also using addTarget: action: forControlEvents:

查看更多
聊天终结者
4楼-- · 2020-02-08 06:29

You can use Timer and and button touchup to handle this action


 var timer: Timer?

    @IBAction func dowm(_ sender: UIButton) {
        timer = Timer.scheduledTimer(withTimeInterval: 0.3, repeats: true, block: { (time) in
            print("im hold in ")
        })
    }

    @IBAction func up(_ sender: UIButton) {
        timer!.invalidate()

    }

this two @IBAction are for handle touch up and touch down button when button touch down timer is started and print something and when button is going u invalidate timer.

access to completed project from Github :

https://github.com/sadeghgoo/RunCodeWhenUIbuttonIsHeld

查看更多
叛逆
5楼-- · 2020-02-08 06:35
    **in Swift 3.0,**

btnPasswordShow.addTarget(self, action:#selector(btnShowPasswordClickHoldDown), for: .touchDown)

btnPasswordShow.addTarget(self, action:#selector(btnShowPasswordClickRelease), for: .touchUpInside)

 func btnShowPasswordClickHoldDown(){
    txtpassword.isSecureTextEntry = false
 }

 func btnShowPasswordClickRelease(){
     txtpassword.isSecureTextEntry = true
 }
查看更多
唯我独甜
6楼-- · 2020-02-08 06:36

drag a button from tools(pane) and right click on it.a list will occur find "touch up inside" and click and drag on circle point that on front of text and move it to viewcontroller.h and define a name and in viewcontroller.m do this.

nslog("clicked in"); repeat all action for touch out and find an appropriate event from list

查看更多
Explosion°爆炸
7楼-- · 2020-02-08 06:37

I have faced this problem myself, and mostly we use these events:-

// This event works fine and fires

[aButton addTarget:self action:@selector(holdDown) forControlEvents:UIControlEventTouchDown];

// This does not fire at all

[aButton addTarget:self action:@selector(holdRelease) forControlEvents:UIControlEventTouchUpInside];

Solution:-

Use Long Press Gesture Recognizer:-

 UILongPressGestureRecognizer *btn_LongPress_gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleBtnLongPressgesture:)];
[aButton addGestureRecognizer:btn_LongPress_gesture];

Implementation of gesture:-

- (void)handleBtnLongPressgesture:(UILongPressGestureRecognizer *)recognizer{


//as you hold the button this would fire

if (recognizer.state == UIGestureRecognizerStateBegan) {

    [self holdDown];
}

//as you release the button this would fire

if (recognizer.state == UIGestureRecognizerStateEnded) {

    [self holdRelease];
}
}
查看更多
登录 后发表回答