UIButton具有按住操作和释放操作

时间:2013-10-07 12:54:04

标签: ios objective-c uibutton uitouch

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

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

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

句柄控制事件基于UIBUtton +块实现

8 个答案:

答案 0 :(得分:47)

试试这个

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

Swift版本

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

答案 1 :(得分:7)

试试这个

添加TouchDown,触摸内部,触摸外部事件到您的按钮

enter image description here

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

答案 2 :(得分:7)

我自己也遇到过这个问题,主要是我们使用这些事件: -

//此事件正常工作并触发

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

//这根本不会发射

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

解决方案: -

使用长按手势识别器: -

 UILongPressGestureRecognizer *btn_LongPress_gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleBtnLongPressgesture:)];
[aButton addGestureRecognizer:btn_LongPress_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];
}
}

答案 3 :(得分: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
 }

答案 4 :(得分:0)

从工具(窗格)拖动按钮并右键单击它。将出现一个列表,找到“内部触摸”,然后单击并拖动文本前面的圆点并将其移动到viewcontroller.h并定义名称并在viewcontroller.m中执行此操作。

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

答案 5 :(得分:0)

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

以编程方式完成

答案 6 :(得分: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用于手柄上移和下移按钮 当按钮触地计时器启动并打印某些东西时,以及按钮按下时u使计时器无效。

从Github访问完成的项目:

https://github.com/sadeghgoo/RunCodeWhenUIbuttonIsHeld

答案 7 :(得分: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"); }