Get the Direction of Swipe Before Scrolling a UIScrollView - ios5

Is there a way to get the direction of swipe before the scrolling the UIScrollView?
I tried to add swipe gesture but no luck.
Here is the event I want to handle. If I swipe fast sideways, left or right, and down-ish, I want it scroll else don't scroll. Also I want to get the interval of swipe. Is there a possible way to do those things? Thanks.

You can use UISwipeGestureRecognizer control.Use UISwipeGestureRecognizerDirectionLeft for gettting Direction.
For that have look at this code below:
UISwipeGestureRecognizer *oneFingerSwipeLeft = [[[UISwipeGestureRecognizer alloc]
initWithTarget:self
action:#selector(oneFingerSwipeLeft:)] autorelease];
[oneFingerSwipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[[self view] addGestureRecognizer:oneFingerSwipeLeft];
In oneFingerSwipeLeft function you can have the direction. Follow the same process for right swipe.

Related

How to Resize iOS UI component same as Interface Builder

I'm working on a iOS app where in a screen, user can have multiple UI component. Also, user can drag and reposition those component.
Now, I want all UI component to be resized by user through finger touch(same as we can resize in Interface Builder with mouse).
EDIT:
As above screen shot of IB, we can hold any tiny squares around button with mouse and resize the button.Same thing I need to implement in my App by finger gesture (probably by UIPanGestureRecognizer).
I recommend you that you add a pinch gesture in those view to user can make zoom.
UIPinchGestureRecognizer *twoFingerPinch =
[[[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scaleObject:)] autorelease];
[[self view] addGestureRecognizer:zoomGesture];
and the method:
- (void)scaleObject:(UIPinchGestureRecognizer *)recognizer
{
NSLog(#"Object scale: %f", recognizer.scale);
}
and change the object size with that scale. Moreover you can use other gestures like:
UITapGestureRecognizer
UIPinchGestureRecognizer
UIRotationGestureRecognizer
UISwipeGestureRecognizer
UIPanGestureRecognizer
UILongPressGestureRecognizer
If you reject solution with pinch recognizer. I would suggest creating custom wrapper for UI components, that would create little squares in corners of the frame and in the middle of every side. Every square should have UIPanGestureRecognizer to control size of the object.
Guys thanks to this Beautiful Example, I'm able to achieve what I wanted to.
Now I am able to put editing handles & resize any UI component easily.
After searching about a week I got this solution, so thought of sharing.

UITableview only allow swipe to delete in one direction

I have a tableview in which Edit mode is enabled, so the user can swipe in any direction on a cell to bring up the delete button. However, I have a gesture on the whole tableview for right swipes to do something else, unrelated to the deletion. I would like it if the swipe to delete only worked on a left swipe, so that my gesture recognizer would be called on the right swipe. Right now, the tableview swipe completely overrides my gesture recognizer.
Is there any way to fix this?
Thanks,
You just need to add a SwipeGesture to tableView and important thing is do not set the delegate of swipeGesture and do not implement gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer function.
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(callYourMehod:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[self.table addGestureRecognizer:swipeRight];
It should work as I have implemented and used it in my code
This is how table cells will not go in edit mode on right swipe.

How can I capture a sideways swipe gesture for a tableView that is inside of a pageControl?

I have a tableView that is one of the views inside a pageControl. I want to be able to capture and respond to a sideways swipe gesture if it's within the tableView, and have the pageControl respond if it is outside of the tableView.
How can I do this?
As I see it, since swiping the table for an action and swiping the table to change pages is pretty much the same motion, you would just have to disable scrolling on the scrollview, scrollview.scrollEnabled = NO; and create a gesture recognizer for your table view, UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget: self action: #selector(myAction:)]; I haven't tested this code so please let me know if you run into any problems.

UIGestureRecognizers for view in a scroll view

I'm using a few UIGestureRecognizers to pan, rotate and scale a view, which resides inside a scroll view.
My problem is that sometimes the scroll view eats the touches before the gesture recognizers do, so when this happens I end up zooming the scroll view instead of dragging the view. (It doesn't happen all the time. I still can't describe how to reproduce this behavior).
I'm pretty sure this can be solved in some way. For example MPMoviePlayerController doesn't have this problem: in fact, you can put it in a scroll view, and when you pinch it, it works just fine (i.e. it doesn't zoom the outer scroll view too). Does anyone know how does MPMoviePlayerController achieve this?
I've already searched for answers on SO, with no results. Thanks!
Disclaimer: This is just an idea, I haven't tested this.
UIGestureRecognizerDelegate defines gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:. You could try implementing this on your gesture recognizer delegate. This won't prevent the scroll view from zooming. To prevent zooming you could try temporary setting maximumZoomScale minimumZoomScale to zoomScale in the recognizer action method.
At a guess your rotation is conflicting with zoom - both probably require two touches?
In which case try creating a one touch rotation.
Or disable zooming on the scrollview and implement zooming/scaling via pinch gesture recognizer.
see:
http://www.icodeblog.com/2010/10/14/working-with-uigesturerecognizers/
Could you not set scrollEnabled(NO) on your UIScrollView?
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIScrollView_Class/Reference/UIScrollView.html
If the value of this property is YES , scrolling is enabled, and if it
is NO , scrolling is disabled. The default is YES.
When scrolling is disabled, the scroll view does not accept touch
events; it forwards them up the responder chain.
Things like zoomToRect:animated: don't say anything about being disabled by this flag; I'd assume that would keep working when you pan/zoom/scroll in response to your gesture events.
I think you can differentiate zoom in/zoom out and scrolling functionality by identify number of tap option on UIScrolView. Here i do same things as you want. you may try it.
- (void)viewDidLoad {
[super viewDidLoad];
imgview=[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"2.png"]];
view1=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 400)];
view1.backgroundColor=[UIColor greenColor];
[view1 addSubview:imgview];
objscrollview.contentMode=UIViewContentModeScaleToFill;
objscrollview.contentSize=CGSizeMake(300, 400);
objscrollview.minimumZoomScale=1;
objscrollview.maximumZoomScale=10;
[objscrollview addSubview:view1];
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap)];
singleFingerTap.numberOfTapsRequired = 1;
[objscrollview addGestureRecognizer:singleFingerTap];
[singleFingerTap release];
}
//Single tap on scrollview call below method
-(void)handleSingleTap
{
NSLog(#"Singletap identify");
}
//While perform zoom in/zoom out action on scroll view it's delegate method call
//In this method, we are return view that want to zoom in/Zoom out..
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView1
{
NSLog(#"hi++++++++++++++++++");
return imgview;
}

Attach a pan gesture recognizer an image

Could someone guide in what I need to do? I need to attach a pan gesture recognizer to an image which rotate clockwise and anticlockwise as we move.
Thanks
You'll want to attach a UIPanGestureRecognizer to the image and when the action method is called you can ask for the current translation and velocity of the gesture. You'd then rotate the image according to these values. Since this is a continuous gesture you'll be notified when the pan gesture changes, allowing you to update the rotation of the image.
UIPanGestureRecognizer Class Reference
--
Update
I've reread your question and if you want to rotate the image with your fingers by doing the typical rotate gesture, you should use the UIRotationGestureRecognizer instead of the UIPanGestureRecognizer. If you want to do a pan gesture moving your fingers left or right, you'll indeed have to use a UIPanGestureRecognizer. I just wanted to complete the answer.
Adding the rotation gesture recognizer:
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(handleGesture:)];
[self.myView addGestureRecognizer:rotationRecognizer];
[rotationRecognizer release];
Handling the gesture and rotating the image accordingly could look like this (in its simplest form):
- (void)handleGesture:(UIRotationGestureRecognizer *)sender {
self.myView.transform = CGAffineTransformMakeRotation(sender.rotation);
}