UIButton with hold down action and release action(具有按住动作和释放动作的 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:具有按住动作和释放动作的 UIButton
基础教程推荐
- Firebase 云消息传递令牌未生成 2022-01-01
- UINavigationBar 隐藏按钮文本 2022-01-01
- 从 UIWebView 访问元数据 2022-01-01
- EditText 中的 setHintTextColor() 2022-01-01
- 在视图控制器的宽度上水平均匀分布 UIButton 的最简单方法? 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- UINavigationItem 的持久 rightBarButtonItem 属性 2022-01-01
- 更改 UITableView 部分标题的颜色 2022-01-01
- 在 iOS 7 下 CCMenu 错位 2022-01-01
- Android - 如何在runOnUiThread 中将数据传递给Runnable? 2022-01-01