Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone - Set UIImage position

I want to have an image that moves each time I hit a button. It should move a set value of pixels each time.

I was hoping that image1 now would have a position property, but as far as I can see it doesnt't.

  UIImageView *image1=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"indicator"]];
  [self.view addSubview:image1];
...

Thank you

EDIT: UIImageView *image1=[[UIImageView alloc]...

like image 362
chwi Avatar asked Sep 23 '26 02:09

chwi


2 Answers

As the other two answers state, you can set the initial position of a UIImageView using a frame (or CGRect). Note that the parameters for the CGRectMake are; x position, y position, x dimension, y dimension.

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
[imgView setImage:[UIImage imageNamed:"image.png"]];

To move the image each time you press a button, you'll need to update the frame each time the button is pressed. However, the x and y coordinates of a frame are read only, so you'll need to create a new frame like so:

CGRect oldFrame = imgView.frame;
CGRect newFrame = CGRectMake(oldFrame.origin.x + 10, oldFrame.origin.y, oldFrame.size.width, oldFrame.size.height);
imgView.frame = newFrame;

Note that this code moves the image 10 points to the right, (along the x axis).

like image 194
TeaPow Avatar answered Sep 27 '26 13:09

TeaPow


you need an imageview to display an UIImage.

UIImage *image1=[UIImage imageNamed:@"indicator.png"];

UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(10,10,100,100)];
iv.image = image1;
[self.view addSubView:iv];
[image1 release];
[iv release];
like image 34
Saurabh Passolia Avatar answered Sep 27 '26 13:09

Saurabh Passolia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!