I have a UIImageView connected to a game I coded. However I would like to know how I could move this UIImageView via a Joystick/D-Pad. I have not coded a JoyStick or a D-Pad as I don't know how to. I would really appreciate it if you could tell me how I could go abouts doing this.
Edit:
A virtual joystick (in the app).
If your looking to create a virtual dpad try the following:
Create 4 UIButton objects, and set them up with direction arrow graphics (I would recommend doing this part in IB).
Then Create 4 seperate IBAction methods and link them to your buttons through IB or programmatically in viewDidLoad
.
Each IBAction method should look something like this:
-(IBAction)moveLeft:(id)sender{
myImageView.center = CGPointMake(myImageView.center.x-10, myImageView.center.y);
}
-(IBAction)moveRight:(id)sender{
myImageView.center = CGPointMake(myImageView.center.x+10, myImageView.center.y);
}
-(IBAction)moveUp:(id)sender{
myImageView.center = CGPointMake(myImageView.center.x, myImageView.center.y-10);
}
-(IBAction)moveDown:(id)sender{
myImageView.center = CGPointMake(myImageView.center.x, myImageView.center.y+10);
}
You can change 10 to any number of pixels you want your image to move.
If you want the view to be animated when you move it you can wrap your movement code insdide an animation block:
[UIView animateWithDuration:1.0 animations:^{
myImageView.center = CGPointMake(myImageView.center.x, myImageView.center.y+10);
}];
Let me know if you need any further clarification.