2016-08-15 116 views
-2

我正在做一个应用程序,用户添加和删除不同类型的UIImageview的对象显示像头发鼻子等我已经添加图像视图只有问题我面临的是如何删除这些图像我正在使用PanGestureRecognizer,我想知道在拖到顶部时删除Imageview在下面看到图像。如何删除UIImageView

enter image description here

+2

为什么不只是从其超视图中删除视图? – Eiko

+0

有三个不同的UIViews,其中一个在底部,一个在顶部,一个在顶部,我希望用户将一个项目拖到顶端,如果他想删除它。 – dreamBegin

+1

Stackoverflow不是一个代码写入服务。如上所述,删除视图是微不足道的。 – Eiko

回答

1

删除在这里是错误的词。

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

[imageView removeFromSuperView]; 

或者你可以让它显示nil istead的图像,这将确保你的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 
    } 
} 
+0

是的,这是我的错误,我应该使用删除,但我想要的是帮助panGesture,我知道如何从superviews删除subViews – dreamBegin

+0

我编辑我的答案,你可能需要帮助你的一块你的答案 –

+0

你的答案是真正的赞赏。感谢你的时间:) – dreamBegin

1

只要做到这一点。

self.imageView.image = nil; 
+0

我的意思是当拖到顶视图时? – dreamBegin

+0

任何帮助panGesture? – dreamBegin

+0

你想使用panGesture /滑动手势? –

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

让我知道它是否适合你与否。

+0

没有朋友不是滑动手势我已经这样做了整个应用程序是关于拖放图像视图认为顶视图作为垃圾箱图标视图将只被删除时拖动到该顶视图 – dreamBegin

+0

再次检查此答案我已经使用PanGesture编辑它与所有方向。 使用欲望者并删除其余的。 –

+0

嘿这将删除图像时,每次用户拖动图像到顶部,但我想要一个特定的地方(顶视图)图像被删除时拖动时。 – dreamBegin