help with dragging a UIImageView on screen

Discussion in 'iOS Programming' started by mandude, Dec 17, 2010.

  1. mandude, Dec 17, 2010
    Last edited: Dec 17, 2010

    mandude macrumors member

    Nov 19, 2009
    I've been having problems trying to get an imageview to move when being dragged in my view, *gameView. I got it to work in my view only when i declared my imageview as an IBOutlet and connecting it in Interface Buider, but that's not the case in my situation. here's my code, if anyone can help it's very much appreciated.

    .h file
    @interface ViewController : UIViewController <UIActionSheetDelegate> {
    UIImageView *imageView;
    UIView *gameView
    UIImage *randomImage;
    NSMutableArray *array;
    @property (nonatomic, retain) UIImageView *imageView;
    @property (nonatomic, retain) UIImage *randomImage;
    @property (nonatomic, retain) IBOutlet UIView *gameView;
    @property (nonatomic, retain) NSMutableArray *array;
    m. file
    @implementation ViewController
    @synthesize imageView;
    @synthesize gameView;
    @synthesize randomImage;
    @synthesize array;
    - (void)viewDidLoad {
    randomImage = [array objectAtIndex: arc4random() % [array count]];
    imageView = [[UIImageView alloc] initWithFrame:CGRectMake(164, 162, 46, 72)];
    imageView.image = randomImage;
    [self.view addSubview:imageView];
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [touches anyObject];
    	if ([touch view] == imageView) {
    		CGPoint location = [touch locationInView:gameView]; = location;

    So for this code nothing happens when I drag the image. i also tryed using

        UITouch *touch = [[event allTouches] anyObject];
    nadda : /
  2. macrumors regular

    Jun 26, 2011
  3. jnoxx macrumors 65816


    Dec 29, 2010
    Aartselaar // Antwerp // Belgium
    TouchesBegin/TouchesEnded is the pre-iOS4, the there are PanRecognizers for that now.
    Which is quite easy.
  4. ppilone macrumors 6502

    Jan 20, 2008
    Try setting your UIImageView's userInteractionEnabled to YES. UIImageView's have this property default to NO, which is different than the UIView default.

Share This Page