具有按住动作和释放动作的 UIButton
Posted
技术标签:
【中文标题】具有按住动作和释放动作的 UIButton【英文标题】:UIButton with hold down action and release action 【发布时间】:2013-10-07 12:54:03 【问题描述】:我想创建一个可以按住的 UIButton,当按住时它会调用一次“按住”动作。 当它被释放时,调用“hold was release”动作。
此代码无法正常工作,因为触摸可以在按钮内移动并且事件未按正确顺序触发
[button handleControlEvent:UIControlEventTouchDown withBlock:^
[self performMomentaryAction:PXActionTypeTouchDown];
];
[button handleControlEvent:UIControlEventTouchUpInside withBlock:^
[self performMomentaryAction:PXActionTypeTouchUp];
];
处理控制事件是基于UIBUtton+block实现
【问题讨论】:
【参考方案1】:试试这个
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");
斯威夫特版本
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")
【讨论】:
它对我有用。但是,有一种情况是行不通的。如果用户长按按钮并在一段时间后,而不是松开手指,用户会将他/她的手指移出按钮的边界。在那种情况下,holdRelease
将不会被调用...【参考方案2】:
试试这个
将 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
【讨论】:
【参考方案3】:我自己也遇到过这个问题,我们大多使用这些事件:-
// 这个事件可以正常工作并触发
[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];
【讨论】:
这种方法是唯一适用于我在 *** 上找到的所有相同答案的方法(ios 7.1.,Xcode 5.1.)。【参考方案4】: **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
【讨论】:
【参考方案5】:从工具(窗格)中拖动一个按钮并右键单击它。将出现一个列表,找到“内部修饰”并单击并拖动文本前面的圆点并将其移动到 viewcontroller.h 并定义一个名称并在 viewcontroller.m 中执行此操作。
nslog("clicked in");
重复所有触摸操作并从列表中找到合适的事件
【讨论】:
【参考方案6】:您需要连接以下两个事件 1.Touch Down ,2.Touch Up Inside 到它的 IBAction Methods 。在 Interface Builder 中会很有趣。但是您也可以使用 addTarget 以编程方式执行此操作: action: forControlEvents:
【讨论】:
【参考方案7】:您可以使用计时器和按钮修饰来处理此操作
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()
这两个@IBAction 用于手柄触摸向上和触摸向下按钮 当按钮按下计时器启动并打印一些东西时,当按钮正在运行时,您会使计时器无效。
从 Github 访问已完成的项目:
https://github.com/sadeghgoo/RunCodeWhenUIbuttonIsHeld
【讨论】:
【参考方案8】:我对此的最佳解决方案是将以下内容复制到您的 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");
【讨论】:
以上是关于具有按住动作和释放动作的 UIButton的主要内容,如果未能解决你的问题,请参考以下文章