Attach a pan gesture recognizer an image - iphone

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);
}

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.

Get the Direction of Swipe Before Scrolling a UIScrollView

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.

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;
}

touchesBegan method not called when scrolling in UIScrollView

I noticed touchesBegan method is not called in UIScrollView if i immediately place my finger on it and scroll. touchesBegan only gets called after i place my finger for a certain time duration before scrolling. Shouldn't touchesBegan always be called whenever there is a touch on the UIScrollView?
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hideKeyBoard:)];
[scrollView addGestureRecognizer:gestureRecognizer];
-(void) hideKeyBoard:(id) sender
{
// Do whatever such as hiding the keyboard
}
I believe that UIScrollView intercepts these events, for the purpose of figuring out if you are going to be scrolling the containing view. Actually, it looks like it gets them first (which is opposite normal processing, where the deepest subview gets them first) so that it can figure out if there is a scroll or pinch gesture. See How does UIScrollView steal touches from its subviews?

gesture recognizer for whole UIView

I want to find a way to animate a whole UIView which contains some ui components within it. Here's my code snippet:
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotatePiece:)];
[piece addGestureRecognizer:rotationGesture];
[rotationGesture release];
the "piece" is a uiview here which I want to rotate and it works perfectly fine. But when I add some other UI components (like UIbutton) inside it (piece view), gestures are not recognized properly when user touches on those UI components. Essentially, I want the parent to listen to the gestures even when user touches the child object.
Any ideas?
you just need to addGestureRecognizer to your subviews you add to your view. As in the rotate piece method you might be rotating your original view only,so it will work fine.
You can add a transparent UIView to cover the whole area and add the gesture recognizer there.