It seems like this should be simple, but it is apparently not so.
I am using Storyboards, with my first view controller defined as LogbookFirstViewController.
The contents of this controller are inside of a UIControl. That way I can detect taps.
However, I can see no easy way to determine when a user has started swiping across the screen. All I want to do is get the touches x-coordinate. Essentially track it.
I dropped a UIPanGestureRecognizer inside LogbookFirstViewController, and attached it’s outlet too:
In .h
@property (assign) IBOutlet UIGestureRecognizer *gestureRecognizer;
Of course, I then synthesized it and set the delegate:
In .m
[gestureRecognizer setDelegate:self];
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touchLoc = [touches anyObject];
CGPoint beginCenter = self.view.center;
CGPoint touchPoint = [touchLoc locationInView:self.view];
deltaX = touchPoint.x - beginCenter.x;
deltaY = touchPoint.y - beginCenter.y;
NSLog(@"X = %f & Y = %f", deltaX, deltaY);
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
UITouch * touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
// Set the correct center when touched
touchPoint.x -= deltaX;
touchPoint.y -= deltaY;
self.view.center = touchPoint;
}
However, this does nothing. It doesn’t even detect -(void)touchesBegan
What am I missing? Thanks in advanced.
Those methods are not delegate methods, they’re only for subclassing
UIGestureRecognizer.Typically you instantiate a gesture recognizer and specify a selector (a method) to call when that gesture is recognized and then you assign it to a view, for example:
Then in your
panmethod you can query information from teh gesture:I’ve never done it with a StoryBoard but I suppose if you have a property already in your view controller you could call
addTarget:action:and attach it to a view in the view controllersviewDidLoadmethod.