Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mandude

macrumors member
Original poster
Nov 19, 2009
64
0
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 : /
 
Last edited:
Try setting your UIImageView's userInteractionEnabled to YES. UIImageView's have this property default to NO, which is different than the UIView default.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.