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
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
boutonHome.m
#import "bouton.h" @implementation boutonHome @synthesize boutonImage; @end
source share