gpt4 book ai didi

ios - 如何在拖动另一个按钮时检测 View 中的 UIButton?

转载 作者:行者123 更新时间:2023-11-29 13:03:59 25 4
gpt4 key购买 nike

我在 ViewController 中实现了一个可拖动的 UIButton。除了这个可拖动的按钮,我还有两个 UIButton,当我将一个 Button 拖到它们上面并更改静态按钮的标题时,它们应该被检测到。我怎样才能实现这个?

这就是可拖动按钮的实现方式。

@interface ButtonAnimationViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIButton *firstButton; // dragable button
@property (weak, nonatomic) IBOutlet UIButton *oneButton; //normal button
@property (weak, nonatomic) IBOutlet UIButton *twoButton; // normal button


@implementation ButtonAnimationViewController

- (void)viewDidLoad
{
[super viewDidLoad];
[self.view addSubview:self.firstButton];
[self.view addSubview:self.oneButton];
[self.view addSubview:self.twoButton];

UIPanGestureRecognizer *panGesture
= [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragging:)];
[self.firstButton addGestureRecognizer:panGesture];

UITapGestureRecognizer *tapGesture
= [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dropping:)];
tapGesture.cancelsTouchesInView = NO;
[self.oneButton addGestureRecognizer:tapGesture];
[self.twoButton addGestureRecognizer:tapGesture];


-(void)dragging:(UIPanGestureRecognizer*)panGesture {

if (panGesture.view != self.firstButton)
{
return;
}
if (panGesture.state == UIGestureRecognizerStateBegan ||
panGesture.state == UIGestureRecognizerStateChanged)
{
CGPoint delta = [panGesture translationInView:self.view];
CGPoint center = self.firstButton.center;
center.x += delta.x;
center.y += delta.y;
self.firstButton.center = center;
[panGesture setTranslation:CGPointZero inView:self.view];
}
if (panGesture.view == self.oneButton) {
// I tried this to change the button title.
NSString *buttonTitle = self.firstButton.titleLabel.text;
self.oneButton.titleLabel.text = buttonTitle;
return;
}
//if (panGesture.state == UIGestureRecognizerStateEnded) {
// self.firstButton.center = center;
//[panGesture setTranslation:CGPointZero inView:self.view];
}

最佳答案

诀窍是使用 CGRectIntersectsRect .. 这是了解一个 View 是否与另一个 View 相交的基本构建 block 。看看here有关使用该功能的相关示例。

这里是示例代码:

  if (panGesture.state == UIGestureRecognizerStateBegan || 
panGesture.state == UIGestureRecognizerStateChanged)
{
CGPoint delta = [panGesture translationInView:self.view];
CGPoint center = self.firstButton.center;
center.x += delta.x;
center.y += delta.y;
self.firstButton.center = center;
[panGesture setTranslation:CGPointZero inView:self.view];
}

// check if there is an overlap
NSString *draggableButtonTitle = self.firstButton.titleLabel.text;
if (CGRectIntersectsRect(self.firstButton.frame, self.oneButton.frame)) {
self.oneButton.titleLabel.text = draggableButtonTitle;
// i donno if you wanna return here or continue dragging.. up to you
} else if (CGRectIntersectsRect(self.firstButton.frame, self.twoButton.frame)) {
self.twoButton.titleLabel.text = draggableButtonTitle;
}

关于ios - 如何在拖动另一个按钮时检测 View 中的 UIButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19052064/

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