<i id='qfUFb'><tr id='qfUFb'><dt id='qfUFb'><q id='qfUFb'><span id='qfUFb'><b id='qfUFb'><form id='qfUFb'><ins id='qfUFb'></ins><ul id='qfUFb'></ul><sub id='qfUFb'></sub></form><legend id='qfUFb'></legend><bdo id='qfUFb'><pre id='qfUFb'><center id='qfUFb'></center></pre></bdo></b><th id='qfUFb'></th></span></q></dt></tr></i><div id='qfUFb'><tfoot id='qfUFb'></tfoot><dl id='qfUFb'><fieldset id='qfUFb'></fieldset></dl></div>

<small id='qfUFb'></small><noframes id='qfUFb'>

    1. <tfoot id='qfUFb'></tfoot>

          <bdo id='qfUFb'></bdo><ul id='qfUFb'></ul>
      1. <legend id='qfUFb'><style id='qfUFb'><dir id='qfUFb'><q id='qfUFb'></q></dir></style></legend>
      2. 具有按住动作和释放动作的 UIButton

        时间:2023-07-08
          • <bdo id='JU389'></bdo><ul id='JU389'></ul>
          • <tfoot id='JU389'></tfoot>
            <legend id='JU389'><style id='JU389'><dir id='JU389'><q id='JU389'></q></dir></style></legend>
              <tbody id='JU389'></tbody>
            <i id='JU389'><tr id='JU389'><dt id='JU389'><q id='JU389'><span id='JU389'><b id='JU389'><form id='JU389'><ins id='JU389'></ins><ul id='JU389'></ul><sub id='JU389'></sub></form><legend id='JU389'></legend><bdo id='JU389'><pre id='JU389'><center id='JU389'></center></pre></bdo></b><th id='JU389'></th></span></q></dt></tr></i><div id='JU389'><tfoot id='JU389'></tfoot><dl id='JU389'><fieldset id='JU389'></fieldset></dl></div>

              <small id='JU389'></small><noframes id='JU389'>

                  本文介绍了具有按住动作和释放动作的 UIButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

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

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

                  handle control events is based on UIBUtton+block implementation

                  推荐答案

                  试试这个

                    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 如果用户长按按钮并在一段时间后,而不是松开手指,用户会将他/她的手指移出按钮的边界.只需再添加一个事件.

                  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 版本

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

                  这篇关于具有按住动作和释放动作的 UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 iPhone 中单击 UIButton 时打开相机? 下一篇:Xcode 8 UIButtons 约束未显示

                  相关文章

                  1. <i id='Zc5ac'><tr id='Zc5ac'><dt id='Zc5ac'><q id='Zc5ac'><span id='Zc5ac'><b id='Zc5ac'><form id='Zc5ac'><ins id='Zc5ac'></ins><ul id='Zc5ac'></ul><sub id='Zc5ac'></sub></form><legend id='Zc5ac'></legend><bdo id='Zc5ac'><pre id='Zc5ac'><center id='Zc5ac'></center></pre></bdo></b><th id='Zc5ac'></th></span></q></dt></tr></i><div id='Zc5ac'><tfoot id='Zc5ac'></tfoot><dl id='Zc5ac'><fieldset id='Zc5ac'></fieldset></dl></div>
                  2. <small id='Zc5ac'></small><noframes id='Zc5ac'>

                    <tfoot id='Zc5ac'></tfoot>
                      <bdo id='Zc5ac'></bdo><ul id='Zc5ac'></ul>

                      <legend id='Zc5ac'><style id='Zc5ac'><dir id='Zc5ac'><q id='Zc5ac'></q></dir></style></legend>