Unable to use UIPanGestureRecognizer to move UIImageView

I am trying to create a program in which a user can drag and drop a UIImageView across the screen using UIPanGestureRecognizer. I tried several different ways, but I can not understand. I'm not sure if I need to create a sender / interrogator. I understand that a UIImageView needs to be placed in a UIView that can handle gestures. In my program, I have a view controller - AdvancedViewController1. I created a UIView with the name AdvancedView1. In the Builder interface, I inserted AdvancedView1 into AdvancedViewController 1. Below are my .h and .m files for the controller and view. I have included only the relevant code. Please let me know if I am close by and how to fix my code. Thanks in advance for your help.

 AdvancedViewController1.h #import <UIKit/UIKit.h> #import "AdvancedView1.h" @interface AdvancedViewController1 : UIViewController { UIWindow *window; AdvancedView1 *advancedView1; UIImageView * option1; } @property (retain) IBOutlet AdvancedView1 *advancedView1; @property (retain) IBOutlet UIImageView *option1; @end 

In IB, I linked IBOutlet with AdvancedView1 with UIView and option1 with UIImageView, which I want to be able to move.

 AdvancedViewController.m #import "AdvancedViewController1.h" #import "AdvancedView1.h" @implementation AdvancedViewController1 @synthesize advancedView1; @synthesize option1 - (void)viewDidLoad { UIGestureRecognizer *pangr = [[UIPanGestureRecognizer alloc] initWithTarget:self.advancedView1 action:@selector(pan:)]; pangr.delegate = self; [self.advancedView1 addGestureRecognizer:pangr]; [pangr release]; [super viewDidLoad]; } AdvancedView1.h #import <UIKit/UIKit.h> #import "AdvancedViewController1.h" @class AdvancedView1; @interface AdvancedView1 : UIView{ CGPoint* origin; } @property (nonatomic) CGPoint* origin; @end AdvancedView1.m #import "AdvancedView1.h" #import "AdvancedViewController1.h" @implementation AdvancedView1; @synthesize origin; - (void)pan:(UIPanGestureRecognizer *) gesture { if ((gesture.state == UIGestureRecognizerStateChanged) || (gesture.state == UIGestureRecognizerStateEnded)) { CGPoint translation = [gesture translationInView:self]; gesture.origin = CGPointMake(gesture.origin.x+translation.x, gesture.origin.y+translation.y); [gesture setTranslation:CGPointZero inView:self]; } 
+4
source share
1 answer

You should actually set the position of the UIView in your pan: selector. And since you are doing this in your AdvancedView class, and not outside, you need to get the view of the parent view ([self superview]), because the position is updated relative to the parent (containing) view. Try the following:

 - (void)pan:(UIPanGestureRecognizer *)gesture { if ((gesture.state == UIGestureRecognizerStateChanged) || (gesture.state == UIGestureRecognizerStateEnded)) { CGPoint location = [gesture locationInView:[self superview]]; [self setCenter:location]; } } 

Keep in mind that you need to make sure that your subclass representation allows the user to interact or recognize gestures will not work. Just enable it with [self setUserInteractionEnabled:YES] when you start browsing.

+12
source

Source: https://habr.com/ru/post/1333141/


All Articles