As part of the following method, i can ask recorgnier object of type UISwipeGestureRecognizer*
- (void) handleSwipeFrom:(UISwipeGestureRecognizer*)recognizer
About its current location
CGPoint currentLocation = [recognizer locationInView:self];
According to documentation, i should also be able to get
CGPoint previousLocation = [recognizer previousLocationInView:self];
This method, however, is not available. What am i missing please?
You can obtain previous location by using
[recognizer locationOfTouch:<#(NSUInteger)#> inView:<#(UIView *)#>]
I think that previousLocation is deprecated for GR, or exactly for SwipeGR
I too Faced the same problem..
I used UITouch object to get the previousLocationInView
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint pre_moveLocation ;
UITouch *theTouch =[touches anyObject];
pre_moveLocation =[theTouch previousLocationInView:imageView;
}
Related
I am creating an Augmented Reality based iOS app using vuforia.I have integrated Vuforia SDK in my project.I need to shows some objects over the target image while scanning the target image. It works fine. I also need to show some messages over the screen when the user touch any of the object. How can i identify which object the user have touched? How the touch events works when the device get zoom in and zoom out?Please help me.
Try to combine ImageTarget and Dominoes sample. Touch event handle in Dominoes starts from EAGLView.mm in the dominoes sample:
// Pass touch events through to the Dominoes module
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch* touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
dominoesTouchEvent(ACTION_DOWN, 0, location.x, location.y);
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch* touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
dominoesTouchEvent(ACTION_CANCEL, 0, location.x, location.y);
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch* touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
dominoesTouchEvent(ACTION_UP, 0, location.x, location.y);
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch* touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
dominoesTouchEvent(ACTION_MOVE, 0, location.x, location.y);
}
Understand how they handle these touches and try to do the same. For more reference see Vuforia developer forum https://developer.vuforia.com/forum/ios/getting-touch-event-3d-model
I feel strange, I have dark view(darkScreenView) and green view.
I using the
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchViewPoint = [touch locationInView:touch.view];
NSLog(#"[self.darkScreenView pointInside:touchViewPoint withEvent:event]:%d",[self.darkView pointInside:touchViewPoint withEvent:event]);
}
When I click on the dark view or green view,
The Log still show 1. why?
I click on the green view should show log 0, but I still get the log result 1.
I am using xcode 6.3.1.
Have anyone know what problem in this situation?
Thank you~
This will work
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchViewPoint = [touch locationInView:touch.view];
CGPoint pointOfDarkView = [touch.view convertPoint:touchViewPoint toView:self.darkview];
NSLog(#"[self.darkScreenView pointInside:touchViewPoint withEvent:event]:%d",[self.darkview pointInside:pointOfDarkView withEvent:event]);
Your problem:
touchViewPoint is relative to touch.view coordinate
You have to convert point to darkview coordinate first,then you can use pointInside:pointOfDarkView
Try to get touch for different views..
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
if (touch.view==GREEN_VIEW)
{
//Write your code for green view here
}
else if(touch.view==DARK_VIEW)
{
//Write your code for dark view here
}
}
Your touchViewPoint's location is relative to the touched view. So get location relative to self.darkView like below.
CGPoint touchViewPoint = [touch locationInView: self.darkView];
Here is the problem change to View you are referring to.
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchViewPoint = [touch locationInView:darkView]; <----- Problem
NSLog(#"[self.darkScreenView pointInside:touchViewPoint withEvent:event]:%d",[self.darkView pointInside:touchViewPoint withEvent:event]);
}
I’m intend to customize a UILabel and put it move with touch gestures, so I created a class named CustomLabel that inherits from UILabel and achieved the following two methods for handing touch events. But, the CustomLabel can’t move anyway ! However, when I change the father class of CustomLabel with UIView, UIbutton and etc. it’s works well. So anyone who can tell me the reason about this phenomen.
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event
{
if(!dragEnable)
{return;}
UITouch *touch = [touches anyObject];
beginPoint = [touch locationInView: self];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if(!dragEnable)
{return;}
UITouch *touch = [touches anyObject];
CGPoint nowPoint = [touch locationInView:self];
float offsetX = nowPoint.x - beginPoint.x;
float offsetY = nowPoinr.y - beginPoint.y;
self.center = CGPointMake(self.center.x+offsetX, self.center+offsetY);
}
I apologize if any of my questions are so basic.
Any help or advice would be much appreciated.
I apologize for my questions are so basic, and I have found the solution. I forgot to set userInteractionEnabled to YES for the label object. I'm very sorry for this mistake.
I have been trying to get a UIImageView to rotate around a point when I touch and drag on it. I am having the problem where it jumps back to its original position if I try and rotate it a second time. I have tried the solution here, but that makes the image spin crazily!
What am I doing wrong? Here's my code:
Updated code with fixes:
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
if (CGRectContainsPoint(Game10Wheel.frame, location)) {
Game10Angle = atan2([Game10Wheel center].y - location.y, [Game10Wheel center].x - location.x);
Game10WheelTouched = true;
}
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if (Game10WheelTouched) {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
float theAngle = atan2([Game10Wheel center].y - location.y, [Game10Wheel center].x - location.x);
[Game10Wheel setTransform: CGAffineTransformRotate([Game10Wheel transform], theAngle - Game10Angle)];
Game10Angle = theAngle;
}
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
Game10WheelTouched = false;
}
-(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
Game10WheelTouched = false;
}
Thanks!
Your problem in calculation of the angle of rotation for further transformation. Try use code below.
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
float theAngle = atan2([Game10Wheel center].y - location.y, [Game10Wheel center].x - location.x);
[Game10Wheel setTransform: CGAffineTransformRotate([Game10Wheel transform], theAngle - Game10Angle)];
Game10Angle = theAngle;
}
You need to modify the current transformation of the object, otherwise you are just setting it to an initial value. Using CGAffineTransformMakeRotation creates a new transform object with only the rotation specified applied to it. What you want to do is use CGAffineTransformRotate and pass the current transform of the Game10Wheel object to add the rotation to that current transform.
EDIT: In other words, when you create your rotation, it should start from the current transform of the Game10Wheel, not from a new transformation.
So I'm using cocos2d and trying to drag a sprite across the screen, it was working prior to this.. but I can't seem to figure out what I did to make it not work. I'm seeing the ccTouchBegan and ccTouchEnd methods being called but not the ccTouchMoved.
I tried converting over to ccTouchesBegan, ccTouchesMoved, and ccTouchesEnded but am still only seeing the Began and Ended methods being called. Obviously I've commented out or changed a setting somewhere. I also commented out [[CCTouchDispatch sharedDisptacher] ...]; because it error'd with the ccTouches methods because it's native touches I believe.
Anyone have any idea what's going on?
Did you add yourself as a delegate?
[[CCTouchDispatcher sharedDispatcher]addTargetedDelegate:self priority:0 swallowsTouches:NO];
And then try this:
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint location = [touch locationInView: [touch view]];
mySprite.position = location;
}
You can do the moving of the sprite using the ccTouchMoved Easily.
Please try the below code. I made a short demo for my understanding of the same concept of yours. It works for me.
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
NSSet *touches = [event allTouches];
for (int i = 0; i < touches.count; i++) {
UITouch *touch = [[touches allObjects] objectAtIndex:i];
CGPoint touchLocation = [touch locationInView: [touch view]];
CGPoint location = [[CCDirector sharedDirector]
convertToGL:touchLocation];
if(CGRectContainsPoint([sprite boundingBox], location))
sprite.position = ccp(location.x,location.y);
}
}