How to limit pan gesture area? - iphone

I am having my UIImageView onto which I am having another UIView rectangle. By applying pan gesture to UIView rectangle it gets outside of UIImageView also. I don't want to be drag outside of UIImageView
I have tried below code but it is not working that way
-(void)handleMovementView:(UIPanGestureRecognizer *)recognizer
{
CGPoint movement;
if(recognizer.state == UIGestureRecognizerStateBegan || recognizer.state == UIGestureRecognizerStateChanged || recognizer.state == UIGestureRecognizerStateEnded)
{
CGRect rec = recognizer.view.frame;
CGRect imgvw = self.imgViewCrop.frame;
if((rec.origin.x >= imgvw.origin.x && (rec.origin.x + rec.size.width <= imgvw.origin.x + imgvw.size.width)))
{
CGPoint translation = [recognizer translationInView:recognizer.view.superview];
movement = translation;
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointZero inView:recognizer.view.superview];
[self handleMovementForHandlers:movement];
}
}
}
If i apply Pan slowly it applies this condition but when i go fast it went outside of ImageView

Instead of manually computing whether the points are within the view's bounds, use CGRectContainsPoint(rect, point). This is what works for me, and I like it because it's shorter and more readable:
func handlePan(pan: UIPanGestureRecognizer) {
switch pan.state {
case .Began:
if CGRectContainsPoint(self.pannableView.frame, pan.locationInView(self.pannableView)) {
// Gesture started inside the pannable view. Do your thing.
}
}

Try This
-(void)handleMovementView:(UIPanGestureRecognizer *)recognizer
{
CGPoint movement;
if(recognizer.state == UIGestureRecognizerStateBegan || recognizer.state == UIGestureRecognizerStateChanged || recognizer.state == UIGestureRecognizerStateEnded)
{
CGRect rec = recognizer.view.frame;
CGRect imgvw = self.imgViewCrop.frame;
if((rec.origin.x >= imgvw.origin.x && (rec.origin.x + rec.size.width <= imgvw.origin.x + imgvw.size.width)))
{
CGPoint translation = [recognizer translationInView:recognizer.view.superview];
movement = translation;
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y);
rec = recognizer.view.frame;
if( rec.origin.x < imgvw.origin.x )
rec.origin.x = imgvw.origin.x;
if( rec.origin.x + rec.size.width > imgvw.origin.x + imgvw.size.width )
rec.origin.x = imgvw.origin.x + imgvw.size.width - rec.size.width;
recognizer.view.frame = rec;
[recognizer setTranslation:CGPointZero inView:recognizer.view.superview];
[self handleMovementForHandlers:movement];
}
}
}

Expanding on #Matt Quiros' answer, and in Swift 3 / 4:
func shouldRespondToGesture(_ gesture: UIGestureRecognizer, in frame: CGRect) -> Bool {
return gesture.state == .began && frame.contains(gesture.location(in: self.view))
}

First get the coordinate (CGRect) of recognizer(Pan Gesture recognizer) then get coordinate (CGRect) of ImageView(Your ImageView).
And then compare this coordinate according to your requirement.
If you are still facing any problem then log the x and y value of image view and recognizer.

Related

UIPanGestureRecognizer - view dissappears on drag

I have a strange problem with using UIPanGestureRecognizer to move a view from the top of the screen. Everything works perfect in simulator but on the actual device the view dissappears when I touch it.
- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {
CGPoint start;
if (recognizer.state == UIGestureRecognizerStateBegan) {
start = [recognizer translationInView:self.view];
originCenter = recognizer.view.center.y;
}
if (recognizer.state == UIGestureRecognizerStateChanged) {
CGPoint translation = [recognizer translationInView:self.view];
if (down) {
if (originCenter + translation.y < self.view.center.y && self.view.center.y + translation.y < self.view.center.y) {
CGPoint move = CGPointMake(self.view.frame.size.width/2, originCenter-(start.y-translation.y));
[recognizer.view setCenter:move];
}
}else{
if (originCenter + translation.y < self.view.center.y && originCenter + translation.y > originCenter) {
CGPoint move = CGPointMake(self.view.frame.size.width/2, originCenter-(start.y-translation.y));
[recognizer.view setCenter:move];
}
}
}
if([recognizer state] == UIGestureRecognizerStateEnded || [recognizer state] == UIGestureRecognizerStateCancelled ){
if(recognizer.view.center.y > -40){
[recognizer.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
down = TRUE;
}else{
[recognizer.view setFrame:CGRectMake(0, 80-self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height)];
down = FALSE;
}
}
}
There is one thing that comes to my mind that there might be problem with calculating the position on the device and the view is moved outside the screen. Is that possible?
If nothing else, you should make start a static or an ivar. Otherwise, when you hit UIGestureRecognizerStateChanged, start will not have preserved the value it had during UIGestureRecognizerStateBegan.

How to set minimum and maximum zoom scale using UIPinchGestureRecognizer

I want to zoom in and zoom out an image view and i dont want to use UIScrollView for that.
so for this i used UIPinchGestureRecognizer and here is my code -
[recognizer view].transform = CGAffineTransformScale([[recognizer view] transform], [recognizer scale], [recognizer scale]);
recognizer.scale = 1;
this is working fine for zoom in and zoom out.
But problem is that i want to zoom in and zoom out in specific scale like in UIScrollView we can set the maxZoom and minZoom. i could not found any solution for that, every tutorial about UIPinchGestureRecognizer just describe the same code.
Declare 2 ivars CGFloat __scale and CGFloat __previousScale in the interface of the class that handles the gesture. Set __scale to 1.0 by overriding one of the init functions (make sure to call the super constructor here).
- (void)zoom:(UIPinchGestureRecognizer *)gesture {
NSLog(#"Scale: %f", [gesture scale]);
if ([gesture state] == UIGestureRecognizerStateBegan) {
__previousScale = __scale;
}
CGFloat currentScale = MAX(MIN([gesture scale] * __scale, MAX_SCALE), MIN_SCALE);
CGFloat scaleStep = currentScale / __previousScale;
[self.view setTransform: CGAffineTransformScale(self.view.transform, scaleStep, scaleStep)];
__previousScale = currentScale;
if ([gesture state] == UIGestureRecognizerStateEnded ||
[gesture state] == UIGestureRecognizerStateCancelled ||
[gesture state] == UIGestureRecognizerStateFailed) {
// Gesture can fail (or cancelled?) when the notification and the object is dragged simultaneously
__scale = currentScale;
NSLog(#"Final scale: %f", __scale);
}
}
– (void)handlePinchGesture:(UIPinchGestureRecognizer *)gestureRecognizer
{
if([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
// Reset the last scale, necessary if there are multiple objects with different scales
lastScale = [gestureRecognizer scale];
}
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan ||
[gestureRecognizer state] == UIGestureRecognizerStateChanged) {
CGFloat currentScale = [[[gestureRecognizer view].layer valueForKeyPath:#”transform.scale”] floatValue];
// Constants to adjust the max/min values of zoom
const CGFloat kMaxScale = 2.0;
const CGFloat kMinScale = 1.0;
CGFloat newScale = 1 – (lastScale – [gestureRecognizer scale]); // new scale is in the range (0-1)
newScale = MIN(newScale, kMaxScale / currentScale);
newScale = MAX(newScale, kMinScale / currentScale);
CGAffineTransform transform = CGAffineTransformScale([[gestureRecognizer view] transform], newScale, newScale);
[gestureRecognizer view].transform = transform;
lastScale = [gestureRecognizer scale]; // Store the previous scale factor for the next pinch gesture call
}
}
I had similar situation. My requirement was imageView will bounce back to its last transformation if imageView is smaller than a minimum size or bigger than a certain maximum size.
if ((self.frame.size.width > IMAGE_MIN_SIZE) && (self.frame.size.height > IMAGE_MIN_SIZE) && (self.frame.size.width < IMAGE_MAX_SIZE) && (self.frame.size.height < IMAGE_MAX_SIZE)) {
lastSizeTransform = self.transform;
}else {
self.transform = lastSizeTransform;
}
Here self is the imageView.
If you logged view.transform while you are pinching, you can see your image coordination which zoomed in and out. So, this solutions doesn't work as i expect. I made my solution like that;
Obj-C Version
- (void)handlePinchGesture:(UIPinchGestureRecognizer *)recognizer {
[recognizer.view setTransform:CGAffineTransformScale(recognizer.view.transform,
recognizer.scale, recognizer.scale)];
if (recognizer.view.transform.a > 1.6) {
CGAffineTransform fooTransform = recognizer.view.transform;
fooTransform.a = 1.6; // this is x coordinate
fooTransform.d = 1.6; // this is y coordinate
recognizer.view.transform = fooTransform;
}
if (recognizer.view.transform.a < 0.95) {
CGAffineTransform fooTransform = recognizer.view.transform;
fooTransform.a = 0.95; // this is x coordinate
fooTransform.d = 0.95; // this is y coordinate
recognizer.view.transform = fooTransform;
}
recognizer.scale = 1.0;
}
Swift Version
func handlePinchGesture(recognizer: UIPinchGestureRecognizer) {
if let view = recognizer.view {
view.transform = CGAffineTransformScale(view.transform,
recognizer.scale, recognizer.scale)
if CGFloat(view.transform.a) > 1.6 {
view.transform.a = 1.6 // this is x coordinate
view.transform.d = 1.6 // this is x coordinate
}
if CGFloat(view.transform.d) < 0.95 {
view.transform.a = 0.95 // this is x coordinate
view.transform.d = 0.95 // this is x coordinate
}
recognizer.scale = 1
}
}

Rotating an Image flips suddenly

I am using the following code to rotate an image by starting gesture on left and right arrows.While moving arrows along the rotation.
- (void)handleRotation:(UIPanGestureRecognizer *)recognizer
{
UIView* cview = self.superview.superview;
UIView* lView = self.superview;
CGPoint origin = [lView convertPoint: self.center toView: cview];
if (recognizer.state == UIGestureRecognizerStateBegan)
{
if (recognizer.view == leftImage)
{
initialP= [self convertPoint: leftImage.center toView: cview];
}
else
{
initialP = [self convertPoint: rightImage.center toView: cview];
}
}
CGPoint currentP = [recognizer translationInView:cview];
currentP.x += initialP.x;
currentP.y += initialP.y;
CGFloat angle = 0;
CGFloat a = initialP.x - origin.x;
CGFloat b = initialP.y - origin.y;
CGFloat c = currentP.x - origin.x;
CGFloat d = currentP.y - origin.y;
angle = acosf( ((a*c) + (b*d)) / ((sqrt(a*a + b*b)) * (sqrt(c*c + d*d))));
angle += lastReleasedAngle;
if (currentP.y < (currentP.x *(initialP.y - origin.y) + (initialP.x * origin.y - initialP.y * origin.x)) / (initialP.x - origin.x))
{
angle = -angle;
}
if (recognizer.view == leftImage)
{
angle = -angle;
}
if(recognizer.state == UIGestureRecognizerStateBegan ||
recognizer.state == UIGestureRecognizerStateChanged || recognizer.state == UIGestureRecognizerStateEnded)
{
[self setTransform: CGAffineTransformMakeRotation(angle)];
}
if (recognizer.state == UIGestureRecognizerStateEnded)
{
lastReleasedAngle = angle;
}
First time when starts rotating it works perfectly fine.Say,right arrow is at 0 degree now..when i start rotation from here and release here it works fine.When i release the rotation anywhere else then tries to rotate ,image starts flipping exact to 180 degree after moving 180 degree.
check out the example over here of rotation with left ,right and depend on position wise download from here

How to make smooth rotation of image?

Currently i am using UIRotationGestureRecognizer to rotate my image and my image currently rotating smoothly.
The code which i am using is
CGFloat imageRotationDegree;
if ([gesture state] == UIGestureRecognizerStateBegan || [gesture state] == UIGestureRecognizerStateChanged)
{
[gesture view].transform = CGAffineTransformRotate([[gesture view] transform], [gesture rotation]);
[gesture setRotation:0];
imageRotationDegree=[gesture rotation];
}
NSLog(#"Rotation in Degrees: %f", imageRotationDegree);
So the problem is it is always printing rotation degree as zero.So that i am unable to save the current rotation degree.
Also if i change [gesture setRotation:0]; to some other degree then rotation is not smooth.
So How can i print different rotation degree with smooth rotation.
try with
CGFloat imageRotationDegree;
if ([gesture state] == UIGestureRecognizerStateBegan || [gesture state] == UIGestureRecognizerStateChanged)
{
imageRotationDegree = [gesture rotation];
[gesture view].transform = CGAffineTransformRotate([[gesture view] transform], imageRotationDegree);
[gesture setRotation:0];
}
NSLog(#"Rotation in Degrees: %f", imageRotationDegree);
[Export("RotateImage")]
void RotateImage (UIRotationGestureRecognizer gestureRecognizer)
{
AdjustAnchorPointForGestureRecognizer (gestureRecognizer);
if (gestureRecognizer.State == UIGestureRecognizerState.Began || gestureRecognizer.State == UIGestureRecognizerState.Changed)
{
gestureRecognizer.View.Transform *= CGAffineTransform.MakeRotation (gestureRecognizer.Rotation);
// Reset the gesture recognizer's rotation - the next callback will get a delta from the current rotation.
gestureRecognizer.Rotation = 0;
}
}
void AdjustAnchorPointForGestureRecognizer (UIGestureRecognizer gestureRecognizer)
{
if (gestureRecognizer.State == UIGestureRecognizerState.Began)
{
var image = gestureRecognizer.View;
var locationInView = gestureRecognizer.LocationInView (image);
var locationInSuperview = gestureRecognizer.LocationInView (image.Superview);
image.Layer.AnchorPoint = new PointF (locationInView.X / image.Bounds.Size.Width, locationInView.Y / image.Bounds.Size.Height);
image.Center = locationInSuperview;
}
}
[Export("RotateImage")]
void RotateImage (UIRotationGestureRecognizer gestureRecognizer)
{
AdjustAnchorPointForGestureRecognizer (gestureRecognizer);
if (gestureRecognizer.State == UIGestureRecognizerState.Began || gestureRecognizer.State == UIGestureRecognizerState.Changed)
{
gestureRecognizer.View.Transform *= CGAffineTransform.MakeRotation (gestureRecognizer.Rotation);
// Reset the gesture recognizer's rotation - the next callback will get a delta from the current rotation.
gestureRecognizer.Rotation = 0;
}
}
// Zoom the image by the current scale
[Export("ScaleImage")]
void ScaleImage (UIPinchGestureRecognizer gestureRecognizer)
{
AdjustAnchorPointForGestureRecognizer (gestureRecognizer);
if (gestureRecognizer.State == UIGestureRecognizerState.Began || gestureRecognizer.State == UIGestureRecognizerState.Changed)
{
gestureRecognizer.View.Transform *= CGAffineTransform.MakeScale (gestureRecognizer.Scale, gestureRecognizer.Scale);
// Reset the gesture recognizer's scale - the next callback will get a delta from the current scale.
gestureRecognizer.Scale = 1;
}
}
// Shift the image's center by the pan amount
[Export("PanImage")]
void PanImage (UIPanGestureRecognizer gestureRecognizer)
{
gestureRecognizer.Enabled = true;
AdjustAnchorPointForGestureRecognizer (gestureRecognizer);
var image = gestureRecognizer.View;
if (gestureRecognizer.State == UIGestureRecognizerState.Began || gestureRecognizer.State == UIGestureRecognizerState.Changed)
{
var translation = gestureRecognizer.TranslationInView (this.window);
gestureRecognizer.View.Center = new PointF (gestureRecognizer.View.Center.X + translation.X, gestureRecognizer.View.Center.Y + translation.Y);
//image.Center = new PointF (image.Center.X + translation.X, image.Center.Y + translation.Y);
// Reset the gesture recognizer's translation to {0, 0} - the next callback will get a delta from the current position.
gestureRecognizer.SetTranslation (PointF.Empty, image);
}
}

UIPinchGestureRecognizer problem

I am using UIPinchGestureRecognizer.can i write two action for pinch in and pinch out..is there any specific Method(delegate)?I have written only one it is called when i pinched in...
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(handlePinchGesture:)];
[self.view addGestureRecognizer:pinchGesture];
[pinchGesture release];
You could check the gesture's .scale in -handlePinchGesture:. If it is < 1, it is a pinch-in, otherwise is a pinch-out.
// Zoom the image by the current scale
[Export("ScaleImage")]
void ScaleImage (UIPinchGestureRecognizer gestureRecognizer)
{
AdjustAnchorPointForGestureRecognizer (gestureRecognizer);
if (gestureRecognizer.State == UIGestureRecognizerState.Began || gestureRecognizer.State == UIGestureRecognizerState.Changed)
{
gestureRecognizer.View.Transform *= CGAffineTransform.MakeScale (gestureRecognizer.Scale, gestureRecognizer.Scale);
// Reset the gesture recognizer's scale - the next callback will get a delta from the current scale.
gestureRecognizer.Scale = 1;
}
}
void AdjustAnchorPointForGestureRecognizer (UIGestureRecognizer gestureRecognizer)
{
if (gestureRecognizer.State == UIGestureRecognizerState.Began)
{
var image = gestureRecognizer.View;
var locationInView = gestureRecognizer.LocationInView (image);
var locationInSuperview = gestureRecognizer.LocationInView (image.Superview);
image.Layer.AnchorPoint = new PointF (locationInView.X / image.Bounds.Size.Width, locationInView.Y / image.Bounds.Size.Height);
image.Center = locationInSuperview;
}
}
// Zoom the image by the current scale
[Export("ScaleImage")]
void ScaleImage (UIPinchGestureRecognizer gestureRecognizer)
{
AdjustAnchorPointForGestureRecognizer (gestureRecognizer);
if (gestureRecognizer.State == UIGestureRecognizerState.Began || gestureRecognizer.State == UIGestureRecognizerState.Changed)
{
gestureRecognizer.View.Transform *= CGAffineTransform.MakeScale (gestureRecognizer.Scale, gestureRecognizer.Scale);
// Reset the gesture recognizer's scale - the next callback will get a delta from the current scale.
gestureRecognizer.Scale = 1;
}
}