Trying to correctly move UIImageView using gestureRecognizer in iOS - ios

I have a UIImageView that I am trying to move using a UIPanGestureRecognizer object. The UIImageView is positioned over top of a UITableView, and serves as a scrollbar. I want this UIImageView to be moved by the user up or down the UITableView, not sideways. To accomplish this, I have implemented UIGestureRecognizerDelegate, and I have the following methods:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
- (void)panGestureDetected:(UIPanGestureRecognizer *)recognizer {
NSLog(#"Do I get called?");
//_startLocation is a property of type CGPoint that I declare in the .h file
_startLocation = [recognizer locationInView:_imageView];
NSLog(#"The point is: %d", _startLocation);
CGRect frame = [_imageView frame];
frame.origin.y += frame.origin.y - _startLocation.y;
[_imageView setFrame: frame];
_imageView.center = _startLocation;
}
The method, panGestureDetected is called from viewDidLoad as follows:
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panGestureDetected:)];
panGesture.maximumNumberOfTouches = 1;
panGesture.minimumNumberOfTouches = 1;
panGesture.delegate = self;
[_table addGestureRecognizer:panGesture];
Unfortunately, my UIImageView moves all over the place frantically on the screen when I try to move it. I want to see a smooth scrolling UIImageView go up/down while the user drags it. Can anyone see what I am doing wrong?

In your handler method just keep this line and remove all other.
_imageView.center.y = [recognizer locationInView:[_imageView superView]].y;
You need to get location in superView, not imageView itself. And just change the y value of center.

Related

Start UIGestureRecognizer programmatically

I want to start the UIPanGestureRecognizer right after adding it to a screenshot. Because the screenshot is created through code, when an item has been highlighted, the user won't press on the screen again. So... How do I start the recognizer programmatically?
UIView *snapshot = [cell snapshotViewAfterScreenUpdates:NO];
//use the cell to map the snapshot frame to the window because this does a perfect job of accounting for table offset, etc. Other methods put the view a little to the side or way off
CGRect newFrame = snapshot.frame;
newFrame.origin = [cell convertPoint:newFrame.origin toView:self.view.window];
[snapshot setFrame:newFrame];
[HelperMethods shadowForView:cell color:[UIColor blackColor] offset:CGSizeMake(1, 1) opacity:.7 radius:snapshot.frame.size.width/4];
//[self.view addSubview:snapshot];
newFrame.origin.y -=10;
//move the frame a little to let user know it can be moved
[UIView animateWithDuration:.2 animations:^{
[snapshot setFrame:newFrame];
}];
//add a long press that kills the tap if recognized
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(userCellDragged:)];
[pan setMinimumNumberOfTouches:1];
[pan setMaximumNumberOfTouches:1];
[cell addGestureRecognizer:pan];
You can always call the method on its own.
For example, you've added the selector
- (void) userCellDragged:(UIPanGestureRecognizer)sender;
For your pan gesture recognizer.
You could call this from anywhere in the view by simply adding
[self userCellDragged:nil];
Remember to add a parameter to this method something like:
if (sender == nil) {
// Triggered programmatically
}
else {
// proceed as normal
}

Drag and drop within UIScrollView

I am trying to design a view which contains a list of items to be ordered and placed into an array. The items are dynamically generated and thus cold be an amount that goes off the bottom of the screen.
For this reason my first view is a UIScrollview which takes the whole devices screen
Nested under this I have a label, explaining what the list is for and how to interact with it and then a UITableView with drag & drop with the delegate methods from http://b2cloud.com.au/how-to-guides/reordering-a-uitableviewcell-from-any-touch-point
The problem I am facing is that while the script works great when there are 1 or two rows, when the contentsize of the UIscrollview is larger than the screen it seems to take priority over the drag & drop leading to unpredictable behaviour.
Is there any way to make clicks on the table take priority to only edit the cells and allow the user to scroll by interacting elsewhere on the view?
Thanks
UPDATE
Based on the comment below I managed to get:
- (void)viewDidLoad
{
[super viewDidLoad];
//
//
//
UIPanGestureRecognizer *tapGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(wasPanned:)];
[self.view addGestureRecognizer:tapGesture];
}
-(void)wasPanned:(UIPanGestureRecognizer *)gesture
{
CGPoint point = [gesture locationInView:scrollView];
UIView *isTable = [scrollView hitTest:point withEvent:nil];
if (gesture.state == UIGestureRecognizerStateBegan)
{
if([[[isTable class] description] isEqualToString:#"UITableViewCellReorderControl"])
{
NSLog(#"Dragged from within table");
[scrollView setScrollEnabled: NO];
}
else
{
[scrollView setScrollEnabled:YES];
}
}
else{
[scrollView setScrollEnabled:YES];
}
}
Now, when the scrollview is not long enough to begin scrolling it NSLogs the message fine
However when the scrollview is longer it only recognises the gesture if the scrollview hasn't began scrolling first
Update
I now have the Console recognising touches in the table 100% of the time and disable scrolling. Disabling the scrolling however also stops the drag & drop functionality. Does anyone know why?
Extra code:
tapGesture.delegate = self;
#pragma mark UIGestureRecognizerDelegate
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
So my final (but not perfect) solution was to do this:
.h
<UIGestureRecognizerDelegate>
.m (viewDidLoad)
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(wasTapped:)];
[self.view addGestureRecognizer:tapGesture];
tapGesture.delegate = self;
.m
-(void)wasTapped:(UIPanGestureRecognizer *)gesture
{
CGPoint point = [gesture locationInView:scrollView];
UIView *isTable = [scrollView hitTest:point withEvent:nil];
if (gesture.state == UIGestureRecognizerStateBegan)
{
if([[[isTable class] description] isEqualToString:#"UITableViewCellReorderControl"])
{
NSLog(#"Dragged from within table");
[scrollView setScrollEnabled: NO];
}
else
{
[scrollView setScrollEnabled:YES];
}
}
else{
[scrollView setScrollEnabled:YES];
}
}
A tap gesture works better than a Pan gesture because a pan gesture seems to recognise press&hold, drag, stop, drag as two different gestures. The feature now works as it should but if you so much as move your finger before the animation for a cell moving has started it will scroll. You also have to wait for the view (can I call it overscroll?) animation to completely stop and the scrollbars disappear before it will grab cells.
I'd appreciate it if anyone can improve on it :)

Unable to call touchesBegan/touchesMoved method inside viewController in iOS

I have a viewController which contains a UITableView. This viewController implements UITableViewDelegate, and UITableViewDataSource, and I'm also trying to implement the following methods:
touchesBegan
touchesMoved
touchesEnded
However, these methods are not being called. I am trying to call these methods in response to the user touching a UIImageView that is inside a UITableView, but doing so only calls this method:
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.view bringSubviewToFront:_imageView];
[UIView animateWithDuration:.3 animations:^{
CGRect rect = [self.view convertRect:[tableView rectForRowAtIndexPath:indexPath] fromView:tableView];
CGFloat floatx = _imageView.frame.origin.x - rect.origin.x;
_imageView.frame = CGRectMake(rect.origin.x + floatx, rect.origin.y, _imageView.frame.size.width, _imageView.frame.size.height);
}];
NSLog(#"Table row %d has been tapped", indexPath.row);
}
What I am hoping is to continue to call this method, but also call:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == _imageView) {
//need to make sure user can only move the UIImageView up or down
CGPoint location = [touch locationInView:_imageView];
//ensure UIImageView does not move outside UIITableView
CGPoint pt = [[touches anyObject] locationInView:_imageView];
CGRect frame = [_imageView frame];
//Only want to move the UIImageView up or down, not sideways at all
frame.origin.y += pt.y - _startLocation.y;
[_imageView setFrame: frame];
_imageView.center = location;
return;
}
}
when the user is dragging the UIImageView inside the UITableView.
Can anyone see what I am doing wrong?
Thanks in advance to all who reply
I had a similar problem to this except I was trying to implement touch detection to an UIWebView. I eventually got over it by adding a UIPanGestureRecognizer to the UIWebView's .view property.
Implement this method:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
and then add the UIPanGestureRecognizer to the UITableView like so:
UIPanGestureRecognizer *panGesture;
panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panGestureDetected:)];
panGesture.maximumNumberOfTouches = 1;
panGesture.minimumNumberOfTouches = 1;
panGesture.delegate = self;
[self.tableView addGestureRecognizer:panGesture];
and then implement the method
- (void)panGestureDetected:(UIPanGestureRecognizer *)recognizer {
}
Now, every time the UIPanGestureRecognizer detects a pan gesture, it will call that method and you can get the location of the UIPanGestureRecognizer by calling the method [recognizer locationInView:self.tableView.view]; which will return a CGPoint. You can also get the state of the UIPanGestureRecognizer by calling [recognizer state] which returns a UIGestureRecognizerState.
Have you set userInteractionEnabled on your view that the touches will be inside?

UILongPressGestureRecognizer recognizes touches outside its view

I have a subview of imageview with a PanGestureRecognizer, and the main view has a LongPressGestureRecognizer. I have added the longpress only to the view like this:
screenRecognize = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(screenTaped:)];
screenRecognize.minimumPressDuration = 0.0;
self.userInteractionEnabled = YES;
[self addGestureRecognizer:screenRecognize];
And here's the imageview:
imageViewPanRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(imageViewPulled:)];
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(310, 50, 10, 40)];
imageView.image = [UIImage imageNamed:#"image.png"];
imageView.userInteractionEnabled = YES;
[imageView addGestureRecognizer:imageViewPanRecognizer];
[self addSubview:imageView];
When I touch the imageView, the UILongPressRecognizer is fired. Why is this?
The answer is already in your question. You set
screenRecognize.minimumPressDuration = 0.0;
that means, UILongPressGestureRecognizer will work as like UITapGestureRecognizer. By the line
[self addGestureRecognizer:screenRecognize];
you active this gesture on all over the self.
Now when you add imageView in the self, the imageView will also response to the UILongPressGestureRecognizer as well as UIPanGestureRecognizer which is only active on the imageView. As a result touching on the imageView is firing UILongPressGestureRecognizer.
To solve this problem you can try by increasing the minimumPressDuration value.
1) Why you use long gesture with minimumPressDuration=0? Can not properly use pan gesture?
2) If you want that gestures work together, try something like this:
longGesture.delegate = self;
...
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
3) If you whan if user pan to UIImageView long gesture disable, try something like this:
self.tag = 1;
longGesture.delegate = self;
...
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
return touch.view.tag == 1;
}

Capture only UIView 2 finger UIPanGestureRecognizer

I have a couple of UIScrollViews in my view controller. I want to overlay a view that captures a 2 finger swipe via UIPanGestureRecognizer which will not record the UIScrollView swipe gestures.
When I put a transparent view over my content with a 2 finger pan gesture, my taps and 1 finger swipes are not detected. I tried overwriting the pointInside: method to return NO
but then it doesn't record my 2 finger swipe.
The effect is similar to the 4 finger swipe to change apps.
You don't need an overlay view.
First implement UIPanGestureRecognizer that will handle 2 finger pan and assign it to your view that contains UIScrollViews
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]
initWithTarget:self
action:#selector(handlePan:)];
panGestureRecognizer.delegate = self;
panGestureRecognizer.minimumNumberOfTouches = 2;
panGestureRecognizer.maximumNumberOfTouches = 2;
[self.view addGestureRecognizer:panGestureRecognizer];
Use UIGestureRecognizerDelegate to handle 2 finger pan with UIScrollView pan gesture
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
And finally you are able to handle 2 fingers pan
- (void)handlePan:(UIPanGestureRecognizer *)gestureRecognizer
{
NSLog(#"pan");
}
If you want to stop scrolling UIScrollView when two finger pan is detected you can disable and enable UIScrollView pan recognizers
- (void)handlePan:(UIPanGestureRecognizer *)gestureRecognizer
{
if(gestureRecognizer.state == UIGestureRecognizerStateBegan)
{
_scrollView.panGestureRecognizer.enabled = NO;
}
if(gestureRecognizer.state == UIGestureRecognizerStateEnded)
{
_scrollView.panGestureRecognizer.enabled = YES;
}
NSLog(#"pan");
}
If you don't really need the overlay you can solve this with just gesture recognizers. I wrote this up as a test:
- (void)viewDidLoad {
[super viewDidLoad];
self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
_scrollView.contentSize = CGSizeMake(self.view.bounds.size.width * 2, self.view.bounds.size.height);
UIView *green = [[UIView alloc] initWithFrame:self.view.bounds];
[green setBackgroundColor:[UIColor greenColor]];
UIView *blue = [[UIView alloc] initWithFrame:CGRectOffset(self.view.bounds, self.view.bounds.size.width, 0)];
[blue setBackgroundColor:[UIColor blueColor]];
[_scrollView addSubview:green];
[_scrollView addSubview:blue];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(twoFingerPan:)];
[pan setMinimumNumberOfTouches:2];
[pan setMaximumNumberOfTouches:2];
[pan setDelaysTouchesBegan:YES];
[_scrollView addGestureRecognizer:pan];
[self.view addSubview:_scrollView];
}
- (void)twoFingerPan:(UIPanGestureRecognizer *)gesture {
switch (gesture.state) {
case UIGestureRecognizerStateBegan:
self.scrollView.scrollEnabled = NO;
break;
case UIGestureRecognizerStateCancelled:
case UIGestureRecognizerStateEnded:
case UIGestureRecognizerStateFailed:
self.scrollView.scrollEnabled = YES;
break;
default:
break;
}
NSLog(#"2 Fingers!");
}
I get the twoFingerPan: call back for when 2 fingers are used. The scroll view's panGestureRecognizer is still working at that point so I disable scrolling on the scroll view to handle the 2 finger pan. I've found this method work's pretty well. One sort of wonky thing is if the scroll view is decelerating the 2 finger gesture recognizer isn't called. Hope that helps!

Resources