UIButton具有按住操作和释放操作

编程入门 行业动态 更新时间:2024-10-28 06:30:40
本文介绍了UIButton具有按住操作和释放操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想创建一个可以按下的UIButton,当按下它时会调用hold down动作一次。 发布后,调用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 +块实现

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

:你可以使用 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具有按住操作和释放操作

本文发布于:2023-10-13 17:05:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1488569.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:操作   UIButton

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!