拖动到图像时删除UILabel [英] Removing a UILabel when dragged to an image

查看:85
本文介绍了拖动到图像时删除UILabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UILabel,我添加了一个平移手势识别器,我的视图中还有一个垃圾桶图像,使用UIImage视图。每次我将UILabel拖到垃圾桶图像时,我想从程序视图中删除UILabel。

I have a UILabel which i added a pan gesture recognizer to and I also have a trashcan image on my view using the UIImage view. I want to delete the UILabel from my program view every time i drag the UILabel to the trashcan image.

推荐答案

我假设你想做这样的事情:

I assume you want to do something like this:

我将告诉你如何实现它。

I'll show you how to implement that.

我们我需要一个标签的插座和垃圾桶视图的插座:

We're going to need an outlet for the label and an outlet for the trashcan view:

@interface ViewController ()

@property (strong, nonatomic) IBOutlet UIImageView *trashView;
@property (strong, nonatomic) IBOutlet UILabel *label;

@end

将这些链接到您的标签和垃圾桶视图。我们还需要两个实例变量:

Connect these to your label and your trashcan view. We'll also need two instance variables:

@implementation ViewController {
    CGPoint labelOriginalCenter;
    BOOL trashIsShowingPendingDropAppearance;
}

我们需要保存标签的原始位置,以便我们为其设置动画如果拖动被取消,则回到那里:

We need to save the original position of the label, so we can animate it back there if the drag is cancelled:

- (void)viewDidLoad {
    [super viewDidLoad];
    labelOriginalCenter = self.label.center;
}

现在让我们为平移手势识别器进行操作。我们需要根据手势移动标签。然后我们需要根据手势的状态采取行动。

Now let's make the action for the pan gesture recognizer. We need to move the label based on the gesture. Then we need to take action based on the state of the gesture.

- (IBAction)labelWasDragged:(UIPanGestureRecognizer *)recognizer {
    [self moveLabelForDrag:recognizer];

    switch (recognizer.state) {
        case UIGestureRecognizerStateChanged:
            [self labelDragDidChange:recognizer];
            break;
        case UIGestureRecognizerStateEnded:
            [self labelDragDidEnd:recognizer];
            break;
        case UIGestureRecognizerStateCancelled:
            [self labelDragDidAbort:recognizer];
            break;
        default:
            break;
    }
}

要移动标签,我们将其中心改为手势的翻译。我们还会在每次更改时将手势的转换重置为零。

To move the label, we change its center by the gesture's translation. We also reset the gesture's translation to zero each time it changes.

- (void)moveLabelForDrag:(UIPanGestureRecognizer *)sender {
    CGPoint translation = [sender translationInView:self.label];
    [sender setTranslation:CGPointZero inView:self.label];
    CGPoint center = self.label.center;
    center.x += translation.x;
    center.y += translation.y;
    self.label.center = center;
}

如果手势发生变化,我们希望更新垃圾桶的外观关于触摸是否在垃圾桶上:

If the gesture changed, we want to update the appearance of the trash can based on whether the touch is over the trash can:

- (void)labelDragDidChange:(UIPanGestureRecognizer *)recognizer {
    if ([self dragIsOverTrash:recognizer]) {
        [self updateTrashAppearanceForPendingDrop];
    } else {
        [self updateTrashAppearanceForNoPendingDrop];
    }
}

如果手势结束,我们想扔掉根据手势结束时触摸是否在垃圾桶上方标记或中止拖动:

If the gesture ended, we want to throw away the label, or abort the drag, based on whether the touch was over the trash can when the gesture ended:

- (void)labelDragDidEnd:(UIPanGestureRecognizer *)recognizer {
    if ([self dragIsOverTrash:recognizer]) {
        [self dropLabelInTrash];
    } else {
        [self abortLabelDrag];
    }
}

如果手势被取消,我们想要中止拖动:

If the gesture was cancelled, we want to abort the drag:

- (void)labelDragDidAbort:(UIPanGestureRecognizer *)recognizer {
    [self abortLabelDrag];
}

要检测手势触摸是否在垃圾桶上,我们会询问手势识别器在垃圾视图的坐标系中的位置。然后我们询问垃圾视图是否该位置在垃圾视图的边界内。

To detect whether the gesture's touch is over the trash can, we ask the gesture recognizer for its location in the trash view's coordinate system. Then we ask the trash view whether that location is inside the trash view's bounds.

- (BOOL)dragIsOverTrash:(UIPanGestureRecognizer *)recognizer {
    CGPoint pointInTrash = [recognizer locationInView:self.trashView];
    return [self.trashView pointInside:pointInTrash withEvent:nil];
}

我们可以通过多种不同的方式更新垃圾桶的外观。在这里,我们将使垃圾桶在垃圾桶上方摆动时摆动:

We could update the trash can's appearance in lots of different ways. Here, we'll make the trash can wiggle while the drag is over the trash can:

- (void)updateTrashAppearanceForPendingDrop {
    if (trashIsShowingPendingDropAppearance)
        return;
    trashIsShowingPendingDropAppearance = YES;
    self.trashView.transform = CGAffineTransformMakeRotation(-.1);
    [UIView animateWithDuration:0.15 delay:0 options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat animations:^{
        self.trashView.transform = CGAffineTransformMakeRotation(.1);
    } completion:nil];
}

当拖拽离开垃圾桶时,我们需要制作垃圾桶停止摆动:

When the drag moves off the trash can, we need to make the trash can stop wiggling:

- (void)updateTrashAppearanceForNoPendingDrop {
    if (!trashIsShowingPendingDropAppearance)
        return;
    trashIsShowingPendingDropAppearance = NO;
    [UIView animateWithDuration:0.15 animations:^{
        self.trashView.transform = CGAffineTransformIdentity;
    }];
}

当我们想要将标签丢弃在垃圾桶中时,我们需要做几个的东西。我们需要阻止垃圾桶摆动,我们需要将标签设置为垃圾桶动画,当动画结束时,我们需要完全删除标签。

When we want to drop the label in the trash, we need to do several things. We need to stop the trash can wiggling, we need to animate the label into the trash can, and when the animation ends, we need to remove the label entirely.

- (void)dropLabelInTrash {
    [self updateTrashAppearanceForNoPendingDrop];
    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        self.label.center = self.trashView.center;
        self.label.transform = CGAffineTransformMakeScale(0.1, 0.1);
    } completion:^(BOOL finished) {
        [self.label removeFromSuperview];
        self.label = nil;
    }];
}

如果拖动被中止,我们需要停止垃圾桶摆动和动画标签回到原来的位置:

If the drag was aborted, we need to stop the trash can wiggling and animate the label back to its original position:

- (void)abortLabelDrag {
    [self updateTrashAppearanceForNoPendingDrop];
    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.label.center = labelOriginalCenter;
    } completion:nil];
}

这就是全部!

这篇关于拖动到图像时删除UILabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆