Avn*_*arr 16 objective-c uibutton uitouch ios
我想创建一个可以按下的UIButton,当按下它时会调用"hold down"动作一次.当它被释放时,调用"hold was release"动作.
此代码无法正常工作,因为触摸可以在按钮内移动,并且事件不会按正确的顺序触发
[button handleControlEvent:UIControlEventTouchDown withBlock:^{
[self performMomentaryAction:PXActionTypeTouchDown];
}];
[button handleControlEvent:UIControlEventTouchUpInside withBlock:^{
[self performMomentaryAction:PXActionTypeTouchUp];
}];
Run Code Online (Sandbox Code Playgroud)
句柄控制事件基于UIBUtton +块实现
Sha*_* BS 48
试试这个
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");
}
Run Code Online (Sandbox Code Playgroud)
对于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");
}
Run Code Online (Sandbox Code Playgroud)
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")
}
Run Code Online (Sandbox Code Playgroud)
我自己也遇到过这个问题,大多数情况下我们都会使用这些事件: -
//这个事件运行正常并且开火
[aButton addTarget:self action:@selector(holdDown) forControlEvents:UIControlEventTouchDown];
Run Code Online (Sandbox Code Playgroud)
//这根本不会发射
[aButton addTarget:self action:@selector(holdRelease) forControlEvents:UIControlEventTouchUpInside];
Run Code Online (Sandbox Code Playgroud)
解:-
使用长按手势识别器: -
UILongPressGestureRecognizer *btn_LongPress_gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleBtnLongPressgesture:)];
[aButton addGestureRecognizer:btn_LongPress_gesture];
Run Code Online (Sandbox Code Playgroud)
手势的实现: -
- (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];
}
}
Run Code Online (Sandbox Code Playgroud)
试试这个
添加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
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
29503 次 |
| 最近记录: |