I am trying to create a program where a user can drag a UIImageView around the screen by using UIPanGestureRecognizer. I have tried a few different ways but cannot figure it out. I am not sure if I need to create a sender/requestor. My understanding is that the UIImageView needs to be placed in a UIView that can handle gestures. In my program I have a view controller - AdvancedViewController1. I have created a UIView named AdvancedView1. In interface Builder I have inserted AdvancedView1 into the AdvancedViewController 1. The following are my .h
and .m
files for the controller and the view. I have only included the relevant code. Please let me know if I am close, 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 have linked an IBOutlet to AdvancedView1 to the UIView and option1 to the UIImageView that 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];
}
You have to actually set the position of UIView in your pan: selector. And since you're doing it within your AdvancedView class rather than outside of it, you have to get the view's parent view ([self superview]) since updating the position is relative to the parent (containing) view. Try this:
- (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 your subclassed view enables user interaction or gesture recognizers won't work. Just turn it on with [self setUserInteractionEnabled:YES]
when you init the view.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With