18

我想创建一个可以按住的 UIButton,当按住它时,它会调用一次“按住”动作。当它被释放时,调用“hold was release”动作。

此代码无法正常工作,因为触摸可以在按钮内移动并且事件未按正确顺序触发

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

处理控制事件基于UIBUtton+block实现

4

8 回答 8

50

尝试这个

  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");

  }

对于 NSPratik 的情况:你可以使用事件UIControlEventTouchUpOutside如果用户长按按钮并在一段时间后,而不是释放手指,用户会将他/她的手指移出按钮的边界。只需再添加一个事件。

  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");
  }

斯威夫特版本

 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")
 }
于 2013-10-07T13:03:04.977 回答
8

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];
}
}
于 2014-02-19T07:21:16.170 回答
8

尝试这个

将 TouchDown ,Touch Up Inside,Touch Up Outside 事件添加到您的按钮

在此处输入图像描述

 -(IBAction)theTouchDown:(id)sender
 {

    timer = [NSTimer scheduledTimerWithTimeInterval:0.2
                                             target:self
                                           selector:@selector(performFunctionality)
                                           userInfo:nil
  }
-(IBAction)theTouchUpInside:(id)sender
{
[timer invalidate];
timer = nil;
[self performFunctionality];

 }


 -(IBAction)theTouchUpOutside:(id)sender
 {
[timer invalidate];
timer = nil;
 }

-(void)performFunctionality
 {
 //write your logic
 }
于 2013-10-07T13:02:47.600 回答
1
    **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
 }
于 2017-05-18T06:22:45.513 回答
0

您需要连接以下两个事件 1.Touch Down ,2.Touch Up Inside 到它的 IBAction 方法。在 Interface Builder 中会很有趣。但是你也可以使用 addTarget: action: forControlEvents:

于 2013-10-07T16:18:10.177 回答
0

从工具(窗格)中拖动一个按钮并右键单击它。将出现一个列表找到“内部修饰”并单击并拖动文本前面的圆点并将其移动到 viewcontroller.h 并定义一个名称并在 viewcontroller .m 这样做。

nslog("clicked in"); 重复所有触摸操作并从列表中找到适当的事件

于 2013-10-07T13:08:01.540 回答
0

您可以使用计时器和按钮修饰来处理此操作


 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()

    }

这两个@IBAction 用于在按钮触摸计时器启动并打印某些内容时处理触摸向上和触摸向下按钮,并且当按钮运行时使计时器无效。

从 Github 访问已完成的项目:

https://github.com/sadeghgoo/RunCodeWhenUIbuttonIsHeld

于 2020-01-05T08:45:39.437 回答
0

我对此的最佳解决方案是将以下内容复制到您的 viewDidLoad 中:

// This will start your action.
hornTouchUp.addTarget(self, action: #selector(start), for: .touchDown)

// This will end your action.
hornTouchUp.addTarget(self, action: #selector(end), for: .touchUpInside)

并将这些函数放在你viewDidLoad的同一个文件中:

@objc func start() { print("start() triggered."); }
@objc func end() { print("end() triggered"); }
于 2021-02-01T07:29:06.953 回答