i want to do different operation in SwipeGesture and TouchMoved.But both are called
when we swipe.any help please?
Drecoginizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(handleSwipeFromD:)];
Drecoginizer.direction = UISwipeGestureRecognizerDirectionDown;
[self.view addGestureRecognizer:Drecoginizer];
As I understood you do not want the selector to react on swipe gesture if this swipe is outside of the circle. In this case, in the beginning of the swipe selector you should check if the swipe is over the circle something like so:
CGPoint lClick = [recognizer locationOfTouch:0 inView:self.view];
//Distance from the center of the circle to the taped point
int lDistance = sqrt(pow(lClick.x - lCircleCenter.x, 2) + pow(lClick.y - lCircleCenter.y, 2));
if ((int)lDistance > (int)lCircleRadius) {
return;
}
Hope it helps you
Related
I've been battling with this for about an hour. I am trying to have my UIView to pans the same way the iOS Facebook app pans the main UIView and shows a navigation table at the left. So when you swipe to the right, it will pan all the way to the right and show you the navigation table. You can slide it back to the left. So it's basically like a slider.
I have a UIPanGestureRecognizer assigned to the UIView. And here is my selector for the gesture:
- (void)swipeDetected:(UIPanGestureRecognizer *)recognizer
{
CGPoint newTranslation = [recognizer translationInView:self.view];
NSLog(#"%f", newTranslation.x + lastTranslation.x);
// only pan appropriately when view is within correct bounds
if (lastTranslation.x + newTranslation.x >= 0 && lastTranslation.x + newTranslation.x <= 255)
{
self.navController.view.transform = CGAffineTransformMakeTranslation(newTranslation.x, 0);
if (recognizer.state == UIGestureRecognizerStateEnded) {
// if navcontroller is at less than 145px, snap back to 0
if (newTranslation.x + lastTranslation.x <= 145)
self.navController.view.transform = CGAffineTransformMakeTranslation(0, 0);
// else if its at more than 145px, snap to 255
else if (newTranslation.x + lastTranslation.x >= 145)
self.navController.view.transform = CGAffineTransformMakeTranslation(255, 0);
lastTranslation.x += newTranslation.x;
}
}
}
This works perfectly when sliding the UIView to the right. It will then stick at 255px so a part of it is shown on the screen, and so it does not disappear. However, when it's at the position, when I slide it back to the left, it jumps all the way to the origin instead of following the pan gesture.
Why is that? How can I fix it?
Thanks
I have 6 UIImageViews each connected to UIPanGestureRecognizer and they are all connected to the same method. The method is:
- (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];
}
I am following Ray Wenderlich's tutorial on using GestureRecognizers. So, I was wondering how to detect collisions so that when one image collides with another image, some code is run. The code is different for each image.
Thanks
If you want move the image with the recognizer maybe you should attach the recognizer to your view.
Belonging to this, the fastest way to do this, is (in the method that change the frame at your UIImageView)
for (UIImageView *iv in _imageArray){
if (CGRectIntersectsRect(iv.frame, _selectedImageView.frame)) {
NSLog(#"Collision");
}
}
_selectedImageView is the image that your are moving and _imageArray is an array that contains all your UIImageView (in your case are 6).
I'm trying to create a knob-like behavior in one of my views with a UIRotationGestureRecognizer. This works, and positions the view as expected. However, every single time a gesture is performed, the rotation of the recognizer resets, so the knob starts at 0 every time.
How can I remember the last rotation of the UIRotationGestureRecognizer to let the user adjust the knob UIView without resetting it every single time?
I'm trying to make the recognizer start calculating rotation changes from the view's last known rotation:
knob starts at 0, recognizer is at 0
recognizer is rotated to 45 degrees
recognizer stops rotating
the knob is left at 45 degrees //this is already happening with the provided code snippet
next touch:
//this is what's is happening
recognizer starts at 0, rotates the knob back to 0
//I want it to be:
recognizer starts at 45, rotates the knob as in the example above.
- (IBAction)rotateView:(id)sender {
if([sender isKindOfClass:[UIRotationGestureRecognizer class]])
{
UIRotationGestureRecognizer* recognizer = sender;
CGAffineTransform transform = CGAffineTransformMakeRotation([recognizer rotation]);
rotatingView.transform = transform;
}
}
You should be able to get the current rotation of the rotatingView from it's transform property. Store this value into a savedRotation variable when the gesture begins. Make sure to assign a delegate to handle the gestureRecognizerShouldBegin callback.
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)recognizer
{
savedRotation = atan2(rotatingView.transform.b, rotatingView.transform.a);
return YES;
}
- (void)rotateView:(UIRotationGestureRecognizer*)recognizer
{
rotatingView.transform = CGAffineTransformMakeRotation(recognizer.rotation+savedRotation);
}
Transform the transform:
rotatingView.transform = CGAffineTransformRotate(rotatingView.transform, [recognizer rotation]);
You have to make sure that you reset the rotation after the transform. Otherwise they will stack on top of each other and you get the "interesting" behavior.
rotatingView.transform = CGAffineTransformScale(rotatingView.transform, recognizer.scale, recognizer.scale);
[recognizer setRotation:0]; // this line
You should also do this for any translation or scaling transformations that you may do when handling gestures. The methods there are:
[recognizer setScale:1];
[recognizer setTranslation:CGPointZero inView:recognizer.view.superview];
Is there a way to detect if the rotation done by the user is in clockwise or counter clockwise direct???
I searched for that but couldn't find an answer !
My code looks like this:
UIGestureRecognizer *recognizer;
recognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(spin)];
[self.rotating addGestureRecognizer:recognizer];
[recognizer release];
the rotation property will tell you the rotation in radians. A negative value would indicate the rotation is clockwise, and a positive value indicates counter-clockwise.
Example:
UIGestureRecognizer *recognizer;
recognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(spin:)]; // <-- Note the colon after the method name
[self.rotating addGestureRecognizer:recognizer];
[recognizer release];
- (void)spin:(UIGestureRecognizer *)gestureRecognizer {
CGFloat rotation = gestureRecognizer.rotation;
if (rotation < 0) {
// clockwise
} else {
// counter-clockwise
}
}
Swift version - imagine you've connected your UIRotationGestureRecognizer to an IBAction and are passing in the UIRotationGestureRecognizer as argument recognizer. You could very easily test direction with the following:
if recognizer.rotation < 0 {
print("Negative rotation value means anticlockwise rotation detected")
} else if recognizer.rotation > 0 {
print("Positive rotation value means clockwise rotation detected")
}
I am going to animate the road, simply calling 1 image again and again from top to bottom. Can anyone suggest how this is possible?
You'll want to look at a UIScrollView. Put a UIImageView in it, then use the following code:
-(void)scroll{
CGPoint currentPoint = scrollView.contentOffset;
CGPoint scrollPoint = CGPointMake(currentPoint.x, currentPoint.y + 1);
[scrollView setContentOffset:scrollPoint animated:NO];
if(scrollPoint.y == scrollView.contentSize.height+5)//If we've reached the bottom, reset slightly past top, to give ticker look
{
[scrollView setContentOffset:CGPointMake (0,-125)];
}
}
Run that code with a looping NSTimer and you'll be set :)