带有按下和释放动作的 UIButton

20

我希望创建一个可以按住并触发“按住”动作,松开时会触发“释放按住”动作的UIButton。

由于触摸可以在按钮内部移动且事件顺序不正确,因此此代码无法正常工作。

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

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

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

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

对我来说它可以工作。但是,有一种情况下它将不起作用。如果用户长按按钮,在一段时间后,用户没有释放手指,而是把手指移出了按钮范围,则 holdRelease 将不会被调用... - NSPratik

8

试试这个

为您的按钮添加按下、内部松开,外部松开事件

图片描述输入在此

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

8

我自己也遇到过这个问题,通常我们使用以下事件:

// 这个事件很好用并会触发

[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];
}
}

这种方法是我在stackoverflow上找到的所有相同答案中唯一有效的方法(iOS 7.1.,Xcode 5.1.)。 - MB_iOSDeveloper

1
我提供的最佳解决方案是将以下内容复制到您的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"); }

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
 }

0

从工具面板中拖动一个按钮并右键单击。会出现一个列表,找到“touch up inside”,然后点击并拖动文本前面的圆点,并将其移动到viewcontroller.h中定义一个名称,在viewcontroller.m中执行以下操作。

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


0
你需要将以下两个事件1.Touch Down、2.Touch Up Inside连接到它的IBAction方法中。在Interface Builder中这会很有趣,但你也可以通过使用addTarget: action: forControlEvents:来以编程方式实现。

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接