gpt4 book ai didi

ios - 移除 UIButton 的手势

转载 作者:行者123 更新时间:2023-12-01 17:53:20 25 4
gpt4 key购买 nike

我正在使用 UIGestureRecognizer平移、旋转、捏合。但我正在申请整个 View 。我需要删除 subview 以外的按钮的手势。但是当我使用平移按钮也会影响。如何限制按钮从 self.view 移动。我将以下代码用于 UIPanGestureRecognizer .

UIPanGestureRecognizer *dbpan = [[UIPanGestureRecognizer alloc] initWithTarget:self
action:@selector(ondbPan:)];

[self.view addGestureRecognizer:dbpan];

[closeButton removeGestureRecognizer:dbpan];

平底锅:
- (void)ondbPan:(UIPanGestureRecognizer *)gesture
{
if ((gesture.state == UIGestureRecognizerStateChanged) ||
(gesture.state == UIGestureRecognizerStateEnded)) {


CGPoint offset = [gesture translationInView:self.view];

CGPoint center = gesture.view.center;
center.x += offset.x;
center.y += offset.y;
gesture.view.center = center;

[gesture setTranslation:CGPointZero inView:self.view];

}
}

最佳答案

尝试使用 UIGestureRecognizer 的代表的波纹管代码如果该 subview 是一种 UIButton 类,则返回 FALSE。还设置委托(delegate)dbpan.delegate = self;在您设置和添加 UIPanGestureRecognizer 时.

- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view isKindOfClass:[UIButton class]])
{
return FALSE;
}
else
{

return TRUE;
}
}

更新:-

我不知道为什么这在您的端不起作用我也使用以下代码测试了这个创建一个演示:-

.h 文件
@interface myviewcontroller : UIViewController<UIGestureRecognizerDelegate>

和 .m 类
- (void)pan:(UIPanGestureRecognizer *)gesture
{
if ((gesture.state == UIGestureRecognizerStateChanged) ||
(gesture.state == UIGestureRecognizerStateEnded)) {

CGPoint location = [gesture locationInView:self.view];

[demoView setCenter:location];
}
}

- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view isKindOfClass:[UIButton class]])
{
return FALSE;
}
else
{

return TRUE;
}
}

- (void)viewDidLoad
{


UIPanGestureRecognizer *dbpan = [[UIPanGestureRecognizer alloc] initWithTarget:self
action:@selector(pan:)];
dbpan.delegate=self;
[self.view addGestureRecognizer:dbpan];
[super viewDidLoad];
}

-(IBAction)B1called
{

NSLog(@"This is called button 1");

}
-(IBAction)B2called
{
NSLog(@"This is called button 2");

}

它的输出是

enter image description here

关于ios - 移除 UIButton 的手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22980883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com