如何删除UIImageView

时间:2016-08-15 13:34:51

标签: ios objective-c xcode uiimageview

我正在创建一个应用程序,用户添加和删除不同类型的UIImageview对象,以显示像头发等。我已经完成了向视图添加图像的问题我面临的问题是如何删除这些图像I && #39; m使用PanGestureRecognizer我想知道在拖到顶部时删除Imageview看到下面的图像。

enter image description here

3 个答案:

答案 0 :(得分:1)

只需按照。

self.imageView.image = nil;

答案 1 :(得分:1)

- (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");
}

让我知道它是否适合你。

答案 2 :(得分:0)

删除这里的错误字词。

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

[imageView removeFromSuperView];

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

imageView.image=nil;

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

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