Make UIView stay on screen - ios

I make a UIView that is movable by the user on the screen, it is a round rectangle that is 40x40, and the screen that it is on has a nav bar at the top and right now it can be moved under the nav bar, and then it is gone and can't come back.
Is there a way to make it so that it can't be moved off the screen?
I make it moveable like this:
- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer;
- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
}
So my question is how can I make it so it can be moved off the screen.
Thanks for the help.

Related

How to know the last x & y values at the end of a dragNdrop?

So I have a view with an UITableView on which I am able to add some objects (they come from my nib: SelectPostIt.xib).
These objects are draggable and what I want to do later is, when I'll drag then drop the object on a row of the TableView , it will add the object into the good row etc.
So I thought about many solutions and the one who seems to me the best is to catch the last coordinates of where I dropped my finger (the rest of the problem is another story :') ).
Here's my question, do I need to add something in my method who handle the dragNdrop of my object to know where I dropped it?
Here's my method:
-(IBAction)handlePan:(UIPanGestureRecognizer *)recognizer{
CGPoint translation = [recognizer translationInView:self];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self]; }
Every help will be welcome, thanks in advance!
Check for the state property of UIGestureRecognizer, if its UIGestureRecognizerStateEnded that means your panning has been ended, so you can choose that point of location as drop point.
-(IBAction)handlePan:(UIPanGestureRecognizer *)recognizer{
CGPoint translation = [recognizer translationInView:self];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y);
if(recognizer.state == UIGestureRecognizerStateEnded){
// Just take the Coordintate
CGPoint centerPointOfView = [recognizer locationInView:self.view];
NSLog(#"X - %f, Y - %f",centerPointOfView.x,centerPointOfView.y);
}
[recognizer setTranslation:CGPointMake(0, 0) inView:self];
}

How to swipe to remove a label?

How can I perform a swipe do remove effect like this one:
I'v tried using a UIPanGestureRecognizer but It didnt work.
This is how I set it up:
- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
}
in the view controller .h I had this method signature, and in the nib file I added the required connections.
Do you recommend some other way to accomplish this? or using a UIPanGestureRecognizer is the right way but I'm doing it wrong?
Thanks ahead!

UIPanGestureRecognizer: Offset view under finger

I have a scrollView with some UIViews as subviews that I drag around. The problem is that the subviews is rather small, and when dragging them, you can't see them because the finger is in the way.
How do I offset the view a little bit upwards while dragging?
Thanks
if (recognizer.state == UIGestureRecognizerStateChanged)
{
CGPoint translation = [recognizer translationInView:self];
draggingView.center = CGPointMake(draggingView.center.x + translation.x,
draggingView.center.y + translation.y);
[recognizer setTranslation:CGPointZero inView:self];
}
Try following code
CGPoint touchPoint = [recognizer locationInView:self];
try changing touchPoint

Limiting my UIPanGestureRecognizer

I have the following code and it works just fine, but I can't really explain it to myself. My goal was to drag my UIView from the bottom and have it stop moving when the bottom of the UIView reaches the center of the screen. The code does just that, but I'm still confused by how the limit i set (newCenter.y >= 0 && newCenter.y <= 284) works.
My perception of whats happening is that when I begin dragging the UIView from the very bottom towards the top of the screen, my newCenter.y is constantly changing (decreasing) as I drag upwards. But what makes it stop when the bottom of the view being dragged reaches the center of the screen? The view stops dragging way before I even get close to 0.
- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {
CGPoint translation = [recognizer translationInView:self.view];
CGPoint newCenter = CGPointMake(self.view.bounds.size.width / 2,
recognizer.view.center.y + translation.y);
if (newCenter.y >= 0 && newCenter.y <= 284) {
recognizer.view.center = newCenter;
[recognizer setTranslation:CGPointZero inView:self.view];
}
}

Restricting UIPanGestureRecognizer movement

I have a UIView object that can be dragged around using UIPanGestureRecognizer, but I only want it to be able to move up 3/4 of the screen. I don't want to it be clipped, but to get to a certain point and not be able to be dragged any further. What I have so far allows it to only move on the Y axis (which is desired).
- (IBAction)panGesture:(UIPanGestureRecognizer *)recognizer
{
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x,
recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
}
Thanks for any help.
So just check whether the new Y coordinate would be too small. Don't change the view if it would be too small:
- (IBAction)panGesture:(UIPanGestureRecognizer *)recognizer
{
CGPoint translation = [recognizer translationInView:self.view];
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
CGPoint center = recognizer.view.center;
center.y += translation.y;
if (center.y < self.yMin)
return;
recognizer.view.center = center;
}
Its work fine for me.
CGPoint currentTouchPoint = [gesture translationInView:self.bottomView];
if (fabsf(currentTouchPoint.x) > fabsf(currentTouchPoint.y)) {
// avoid horizontal movement of pan geuture.
return;
}
thanks,
Naveen Shan
Implement the following gesture delegate and check your condition inside it. Returning YES or NO from this delegate will make the gesture active and inactive.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;

Resources