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

    Joined:
    Nov 19, 2009
    #1
    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
    Code:
    @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
    Code:
    @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];
    		imageView.center = location;
    	}
    }
    

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

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

    Joined:
    Jun 26, 2011
    Location:
    Florida
  3. jnoxx macrumors 65816

    jnoxx

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

    Joined:
    Jan 20, 2008
    #4
    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