Используйте пользовательскую кнопку в качестве метки, а затем использовать этот код, как:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:@selector(btnTouch:withEvent:) forControlEvents:UIControlEventTouchDown];
button.tag = -1;
button.titleLabel.text = @"Move this";
[button addTarget:self action:@selector(btnTouch:withEvent:) forControlEvents:UIControlEventTouchDragInside];
[self.view addSubview:button];
Тогда вы можете перемещайте buttol везде, где хотите, отвечая на событие UIControlEventTouchDragInside, например:
- (IBAction) btnTouch:(id) sender withEvent:(UIEvent *) event
{
CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
UIControl *control = sender;
control.center = point;
//Here use this to check when intersects and check if the frame of the item you are moving intersects with the frame from on of your subviews
for (UIView *anotherBtn in self.view.subviews) {
if (CGRectIntersectsRect(control.frame, anotherBtn.frame)) {
// Do something
[anotherBtn setTitle:control.titleLabel.text];
}
}
}
Надеюсь, это поможет вам.
Вы пробовали ['UIPanGestureRecognizer'] (https://www.cocoanetics.com/2010/11/draggable-buttons-labels/) с вашей меткой? – Amar
@Amar Да, я использовал UIPanGestureRecognizer – iosLearner