0

I'm sub classing an UIView to make its always floating on my app's screen. I'm using some method of NSResponder like touchesMove, touchesEnd... to make this view can movable.
When I'm long press on this floating view another view called "Close View" will appear at bottom of screen.
Now I want whenever I'm drag & drop this floating view to Close View this will be remove from superview (this will look like Android launcher when you delete an app from screen). But I don't know how to calculate when 2 view was reached to remove the floating view.
Can someone help me to solve this problem? I've put a picture for imagination.
Update:
Here is my code:

- (BOOL)isTrashViewReached {
CGFloat distanceFromTrashView = sqrt(pow(self.center.x - self.trashView.center.x, 2) + pow(self.center.y - self.trashView.center.y, 2));
return distanceFromTrashView < self.bounds.size.width / 2;
}

-(void)updateTrashView {
    UIColor *trashColor = self.isTrashViewReached ?  [UIColor whiteColor] : [UIColor colorWithRed:174/255.0 green:0 blue:0 alpha:1];
    [self.deleteButton setTitleColor:trashColor forState:UIControlStateNormal];
}

enter image description here

Bad_Developer
  • 537
  • 5
  • 20

3 Answers3

0

From Drag an UIImageView from UIscrollView to another view , it will be a great example for you.

Try it:

This is an alternative, using UIGraphicsGetImageFromCurrentImageContext()

You can grab an image from currentScreen on click or after some events. Next renderize it to screen and apply a Pan gesture to move around the screen.

UIView *_DraggableView;
UIImageView *_imgvcChild1;

sample code:

- ( void ) onScrollviewClickOrOtherEvent
{
  UIGraphicsBeginImageContext ( _DraggableView.frame.size );
  [ _DraggableView.layer renderInContext:UIGraphicsGetCurrentContext() ];

  UIImage *grab = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();

  if ( _imgvcChild1 ) [ _imgvcChild1 removeFromSuperview ];
  _imgvcChild1 = [[ UIImageView alloc ] initWithImage:grab ];
  _imgvcChild1.frame = _DraggableView.frame;
  _imgvcChild1.userInteractionEnabled = YES;
  [ self.view addSubview:_imgvcChild1 ];

  UIPanGestureRecognizer *pan = [[ UIPanGestureRecognizer alloc ] initWithTarget:self action:@selector(moveObject:) ];
  [ pan setMinimumNumberOfTouches:1 ];
  [ _imgvcChild1 addGestureRecognizer:pan ];
}

- (void) moveObject:(UIPanGestureRecognizer *)pan
{
  _imgvcChild1.center = [ pan locationInView:_imgvcChild1.superview ];
}

A sample project on github: https://github.com/elpsk/Shopping-Cart

Community
  • 1
  • 1
Jamshed Alam
  • 12,424
  • 5
  • 26
  • 49
0

You can convert a point from one views coordinates to another with the UIView.convert family of methods. You can test if a rect contains a point or if two rects intersect with the methods on CGRect. Here I check if the center of the dragging view is inside of the target rect:

    let convertedCenter = targetView.convert(draggingView.center, from: draggingView)
    if targetView.frame.contains(convertedCenter) {

    }
Josh Homann
  • 15,933
  • 3
  • 30
  • 33
0

you can use CGRectIntersectsRect ,CGRectContainsRect or CGRectContainsPoint founction to do this.

if (CGRectContainsRect(deleteView.frame, selectedView.frame)) {
    NSLog(@"true");

}else{
    NSLog(@"false");

}
Roy Wang
  • 48
  • 7