Adding a gesture recognizer to pan a UIView

After looking at some examples, I am trying to create a window in which the image can be moved using PanGestureRecognizer. I do not understand what is missing.

  • I created and initialized a UIView object
  • I also created a UIGestureRecognizer for panning for both views.
  • I created a method that will be selected when Gesture is recognized.

If you have an idea of ​​what is missing, I would be grateful for reading it;

Here is my code:

View Controller.h

#import <UIKit/UIKit.h> #import "bouton.h" @interface ATSViewController : UIViewController { boutonHome *bouton; } @property (retain, nonatomic) boutonHome *bouton; -(IBAction)handlePanGesture:(UIPanGestureRecognizer*)sender; @end 

View Controller.m

 - (void)viewDidLoad { [super viewDidLoad]; UIImage *image = [UIImage imageNamed:@"back.png"]; CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height); // Set self frame to encompass the image bouton.frame = frame; bouton.boutonImage = image; [self.view addSubview:bouton]; UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)]; [bouton setUserInteractionEnabled:YES]; [bouton addGestureRecognizer:panGesture]; [self.view setUserInteractionEnabled:YES]; [self.view addGestureRecognizer:panGesture]; // Do any additional setup after loading the view, typically from a nib. } -(IBAction)handlePanGesture:(UIPanGestureRecognizer *)sender { CGPoint translate = [sender translationInView:self.view]; CGRect newFrame = bouton.frame; newFrame.origin.x += translate.x; newFrame.origin.y += translate.y; sender.view.frame = newFrame; if(sender.state == UIGestureRecognizerStateEnded) bouton.frame = newFrame; } 

Boutonhome.h

 #import <Foundation/Foundation.h> @interface boutonHome : UIView { UIImage *boutonImage; } @property (nonatomic, retain) UIImage *boutonImage; // Initializer for this object @end 

boutonHome.m

 #import "bouton.h" @implementation boutonHome @synthesize boutonImage; @end 
+4
source share

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


All Articles