【问题标题】:How to remove a UIImageView如何删除 UIImageView
【发布时间】:2016-12-21 17:14:52
【问题描述】:

我正在制作一个应用程序,用户可以在其中添加和删除不同类型的 UIImageview 对象以显示,例如头发鼻子等。我已经将图像添加到视图中,我面临的唯一问题是如何删除这些图像我正在使用 PanGestureRecognizer我想知道拖到顶部时删除 Imageview 见下图。

【问题讨论】:

  • 为什么不直接从父视图中删除视图?
  • 共有三种不同的 UIView,一个在底部,一个在中间,一个在顶部
  • Stackoverflow 不是代码编写服务。如前所述,删除视图很简单。
  • 这张图只是一个原型,展示功能实际应用不同。
  • 我认为在这里您至少可以寻求建议,但像您这样的人永远不会仅仅因为我们自己学习而理解初学者的意义吗?

标签: ios objective-c xcode uiimageview


【解决方案1】:

这里的删除是错误的词。

您可以将它从它的超级视图中删除,如果没有其他对象保持对它的强引用,这将破坏 imageView。

[imageView removeFromSuperView];

或者你可以让它显示 nil 而不是图像,这将确保你的 imageView 仍然在这里但它不显示任何东西。

imageView.image=nil;

如果你想从一个 NSMutableArray 的 extraFeatures 中删除显示的图像,你可以:

[self.extraFeatures removeObject:imageView.image];

然后

[imageView removeFromSuperView];

** 编辑:平移手势

- (void)pan:(UIPanGestureRecognizer *)sender
{
    // move begins
    if(sender.state == UIGestureRecognizerStateBegan)
    {
      // Do something when the move begin???
      // Maybe record the initial position
    }

    // Move the view around during the pan
    else if (sender.state == UIGestureRecognizerStateChanged)
    {
        CGPoint move = [sender translationInView:self.view];
        sender.view.center = CGPointMake(sender.view.center.x + move.x, sender.view.center.y + move.y);
        [sender setTranslation:CGPointZero inView:self.view];

         // test if we should remove the view, viewCenter outside of viewbounds
        if(!CGRectContainsPoint(self.view.bounds, imageView.center))
        {
            [sender.view removeFromSuperView];
        }
    }

    // End of move
    else if (sender.state == UIGestureRecognizerStateEnded)
    {

        // test if we should remove the view, viewCenter outside of viewbounds
        if(!CGRectContainsPoint(self.view.bounds, imageView.center))
        {
            [sender.view removeFromSuperView];
        }

        // else maybe comming back to the original position
    }
}

【讨论】:

  • 是的,这是我的错误,我应该使用 remove,但我想要的只是 panGesture 的帮助,我知道如何从 superViews 中删除 subViews
  • 我编辑了我的答案,其中包含一些您可能需要帮助您完成平底锅的内容
  • 非常感谢您的回答。感谢您的时间:)
【解决方案2】:
- (void)pan:(UIPanGestureRecognizer *)sender
{

typedef NS_ENUM(NSUInteger, UIPanGestureRecognizerDirection) {
    UIPanGestureRecognizerDirectionUndefined,
    UIPanGestureRecognizerDirectionUp,
    UIPanGestureRecognizerDirectionDown,
    UIPanGestureRecognizerDirectionLeft,
    UIPanGestureRecognizerDirectionRight
};

static UIPanGestureRecognizerDirection direction = UIPanGestureRecognizerDirectionUndefined;

switch (sender.state) {

    case UIGestureRecognizerStateBegan: {

        if (direction == UIPanGestureRecognizerDirectionUndefined) {

            CGPoint velocity = [sender velocityInView:recognizer.view];

            BOOL isVerticalGesture = fabs(velocity.y) > fabs(velocity.x);

            if (isVerticalGesture) {
                if (velocity.y > 0) {
                    direction = UIPanGestureRecognizerDirectionDown;
                } else {
                    direction = UIPanGestureRecognizerDirectionUp;
                }
            }

            else {
                if (velocity.x > 0) {
                    direction = UIPanGestureRecognizerDirectionRight;
                } else {
                    direction = UIPanGestureRecognizerDirectionLeft;
                }
            }
        }

        break;
    }

    case UIGestureRecognizerStateChanged: {
        switch (direction) {
            case UIPanGestureRecognizerDirectionUp: {
                [self handleUpwardsGesture:sender];
                break;
            }
            case UIPanGestureRecognizerDirectionDown: {
                [self handleDownwardsGesture:sender];
                break;
            }
            case UIPanGestureRecognizerDirectionLeft: {
                [self handleLeftGesture:sender];
                break;
            }
            case UIPanGestureRecognizerDirectionRight: {
                [self handleRightGesture:sender];
                break;
            }
            default: {
                break;
            }
        }
    }

    case UIGestureRecognizerStateEnded: {
        direction = UIPanGestureRecognizerDirectionUndefined;   
        break;
    }

    default:
        break;
}

}

- (void)handleUpwardsGesture:(UIPanGestureRecognizer *)sender
{
    NSLog(@"Up");
}

- (void)handleDownwardsGesture:(UIPanGestureRecognizer *)sender
{
    NSLog(@"Down");
}

- (void)handleLeftGesture:(UIPanGestureRecognizer *)sender
{
    NSLog(@"Left");
}

- (void)handleRightGesture:(UIPanGestureRecognizer *)sender
{
    NSLog(@"Right");
}

让我知道它是否适合你。

【讨论】:

  • 没有朋友不是滑动手势我已经这样做了整个应用程序是关于拖放图像视图认为顶部视图作为垃圾箱图标视图只会在拖动到该顶部视图时被删除
  • 再次检查这个答案我已经使用 PanGesture 对所有方向进行了编辑。使用欲望之一并删除其余部分。
  • 嘿,这将在每次用户将图像拖到顶部时删除图像,但我想要一个特定的位置(顶视图),在拖动时图像被删除。
  • 当用户将图像拖到灰色的 UIView 的特定区域时,您想要吗?或者当用户在灰色的 Top UIView 上放置手势时,您想删除图像?
  • 对于案例 1:我的意思是当用户将图像从他当前的位置拖到顶部灰色 UIView.. 您必须获取当前和最终坐标并检查最终坐标是否在UIView 与否,如果是,则删除图像,否则什么也不做。
【解决方案3】:

简单地做。

self.imageView.image = nil;

【讨论】:

  • 我的意思是在顶视图上拖动时?
  • 对 panGesture 有帮助吗?
  • 你想使用panGesture/Swipe Gesture吗?
  • PanGesture 分配给 imageview 对象,当拖到顶视图上时应该删除图像
  • ok 只需分配并初始化目标和操作。在您的操作方法中编写我提供给您的代码以删除。并将方向设置为向上。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
相关资源
最近更新 更多