简体   繁体   English

如何删除UIImageView

[英]How to remove a UIImageView

I'm making an application where user add and remove different type of UIImageview ojects to display like hair nose etc. I have done adding images to views only problem i'm facing is how to delete these images I'm using PanGestureRecognizer I want to know to delete the Imageview when dragged to the top see the image Below. 我正在制作一个应用程序,其中用户添加和删除不同类型的UIImageview对象,以显示像鼻毛等。我已经完成了向视图中添加图像的操作,唯一的问题是我面临的问题是如何删除这些图像,而我正在使用PanGestureRecognizer知道拖动到顶部时删除Imageview时,请参见下面的图像。

在此处输入图片说明

只需这样做。

self.imageView.image = nil;
- (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");
}

Let me know if it works for you or not. 让我知道它是否对您有用。

deleting is the wrong word here. 这里删除是错误的词。

You can remove it from it's superview, which will destroy the imageView IF no other object keep a strong reference to it. 您可以将其从其超级视图中删除,如果没有其他对象对其进行强烈引用,则它将破坏imageView。

[imageView removeFromSuperView];

Or you can make it display nil istead of an image which will insure your imageView is still here but it doesn't display anything. 或者,您可以使其显示为nil而不是图像,以确保您的imageView仍在此处,但不显示任何内容。

imageView.image=nil;

if you wish to delete the displayed image from say a NSMutableArray of extraFeatures, you can : 如果您希望从显示的NSMutableArray of ExtraFeatures中删除显示的图像,则可以:

[self.extraFeatures removeObject:imageView.image];

and then 接着

[imageView removeFromSuperView];

** EDIT: pan gesture **编辑:平移手势

- (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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM