UIButton按住动作和释放动作
我想创build一个可以按下的UIButton,当它按下时,它会调用一次“hold down”操作。 当它被释放时,调用“暂停释放”动作。
此代码无法正常工作,因为触摸可以在button内移动,并且事件不会以正确的顺序触发
[button handleControlEvent:UIControlEventTouchDown withBlock:^{ [self performMomentaryAction:PXActionTypeTouchDown]; }]; [button handleControlEvent:UIControlEventTouchUpInside withBlock:^{ [self performMomentaryAction:PXActionTypeTouchUp]; }];
句柄控制事件是基于UIButton +模块实现的
尝试这个
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
如果用户长按button,一段时间后,而不是释放手指,用户将他/她的手指移出button的界限。 只需添加一个事件。
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") }
尝试这个
添加TouchDown,Touch Up Inside,Touch Up Outside事件到你的button
-(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 }
我自己也面对这个问题,而且大多数情况下我们使用这些事件:
/ /这个事件工作正常,并触发
[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]; } }
**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 }
从工具(窗格)拖动一个button,并右键单击它。一个列表将发生“find里面”,并点击并拖动文本前面的圆点,并将其移动到viewcontroller.h并定义一个名称,并在viewcontroller .m做到这一点。
nslog("clicked in");
重复所有的操作,从列表中find适当的事件
您需要连接以下两个事件1.触摸下来,2.触摸它的IBAction方法。它将在Interface Builder中很有趣。 但是你也可以使用addTarget以编程的方式来实现:action:forControlEvents: