ios - event - swift touchesbegan




UIButton按住動作和釋放動作 (4)

嘗試這個

添加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
 }

我想創建一個可以按下的UIButton,當它按下時,它會調用一次“hold down”操作。 當它被釋放時,調用“暫停釋放”動作。

此代碼無法正常工作,因為觸摸可以在按鈕內移動,並且事件不會以正確的順序觸發

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

句柄控制事件是基於UIButton +模塊實現的


從工具(窗格)拖動一個按鈕,並右鍵單擊它。一個列表將發生“找到裡面”,並點擊並拖動文本前面的圓點,並將其移動到viewcontroller.h並定義一個名稱,並在viewcontroller .m做到這一點。

nslog("clicked in"); 重複所有的操作,從列表中找到適當的事件


我自己也面對這個問題,而且大多數情況下我們使用這些事件:

/ /這個事件工作正常,並觸發

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

嘗試這個

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

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

    func holdRelease(sender:UIButton)
    {
       println("hold release")
    }




uitouch