Ios 带按住和释放操作的UIB按钮
我想创建一个可以按住的UIButton,当按住它时,它会调用一次“按住”操作。 当它被释放时,调用“hold was release”操作 此代码无法正常工作,因为触摸可能会在按钮内部移动,并且事件的触发顺序不正确Ios 带按住和释放操作的UIB按钮,ios,objective-c,uibutton,uitouch,Ios,Objective C,Uibutton,Uitouch,我想创建一个可以按住的UIButton,当按住它时,它会调用一次“按住”操作。 当它被释放时,调用“hold was release”操作 此代码无法正常工作,因为触摸可能会在按钮内部移动,并且事件的触发顺序不正确 [button handleControlEvent:UIControlEventTouchDown withBlock:^{ [self performMomentaryAction:PXActionTypeTouchDown]; }]; [button h
[button handleControlEvent:UIControlEventTouchDown withBlock:^{
[self performMomentaryAction:PXActionTypeTouchDown];
}];
[button handleControlEvent:UIControlEventTouchUpInside withBlock:^{
[self performMomentaryAction:PXActionTypeTouchUp];
}];
句柄控制事件基于UIBUtton+块实现
将触地、触地内、触地外事件添加到ur按钮中
-(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 *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的情况:如果用户长按按钮,并且在一段时间后,用户不会松开手指,而是将其手指移出按钮的边界,则可以使用事件uicontrolEventTouchOutside
。只需再添加一个事件
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")
}
从工具(窗格)中拖动一个按钮并右键单击。将出现一个列表,找到“内部润色”,单击并拖动文本前面的圆点,将其移动到viewcontroller.h并定义名称,然后在viewcontroller.m中执行此操作
nslog(“点击进入”)代码>
重复触碰的所有操作,并从列表中找到合适的事件。您需要连接以下两个事件1.触碰,2.触碰内部的iAction方法。在Interface Builder中会很有趣。但是,您也可以使用addTarget:action:forControlEvents:以编程方式执行此操作。我自己也遇到过这个问题,我们主要使用这些事件:-
//此事件运行良好,并引发火灾
[aButton addTarget:self action:@selector(holdDown) forControlEvents:UIControlEventTouchDown];
//这根本不着火
[aButton addTarget:self action:@selector(holdRelease) forControlEvents:UIControlEventTouchUpInside];
解决方案:-
-(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
}
使用长按手势识别器:-
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];
}
}
您可以使用计时器和按钮修补来处理此操作
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()
}
这两个@iAction用于手柄触碰和触碰按钮
当按下按钮时,定时器启动并打印一些内容,当按下按钮时,定时器失效
从Github访问已完成的项目:
我的最佳解决方案是将以下内容复制到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"); }
这种方法是我在stackoverflow(iOS 7.1.,Xcode 5.1.)上找到的所有相同答案中唯一有效的方法。它对我有效。但是,有一种情况下,这是行不通的。如果用户长按按钮,一段时间后,用户不会松开手指,而是将手指移出按钮的边界。在这种情况下,holdRelease
将不会被调用。。。