iPhone - 设置UIImage位置 [英] iPhone - Set UIImage position

查看:286
本文介绍了iPhone - 设置UIImage位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望每次按下按钮时都会移动一个图像。它应该每次移动一个设定的像素值。



我希望image1现在有一个位置属性,但据我所知,它并没有。

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

谢谢



编辑: UIImageView * image1 = [[UIImageView alloc] ...

解决方案

当其他两个回答状态时,您可以使用框架(或CGRect)设置UIImageView的初始位置。请注意,CGRectMake的参数是; x位置,y位置,x维度,y维度。

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

要在每次按下按钮时移动图像,每次都需要更新帧按下按钮。但是,框架的x和y坐标是只读的,因此您需要创建一个新框架,如下所示:

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

请注意,此代码将图像向右移动10个点(沿x轴)。 / p>

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]...

解决方案

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).

这篇关于iPhone - 设置UIImage位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆