xcode iPhone - 设置 UIImage 位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11611115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iPhone - Set UIImage position
提问by chwi
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.
我希望 image1 现在有一个 position 属性,但据我所知它没有。
UIImageView *image1=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"indicator"]];
[self.view addSubview:image1];
...
Thank you
谢谢
EDIT: UIImageView *image1=[[UIImageView alloc]...
编辑: UIImageView *image1=[[UIImageView alloc]...
回答by TeaPow
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.
正如其他两个答案所述,您可以使用框架(或 CGRect)设置 UIImageView 的初始位置。请注意,CGRectMake 的参数是;x 位置,y 位置,x 维度,y 维度。
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:
要在每次按下按钮时移动图像,每次按下按钮时都需要更新框架。但是,框架的 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;
Note that this code moves the image 10 points to the right, (along the x axis).
请注意,此代码将图像向右移动 10 个点(沿 x 轴)。
回答by Saurabh Passolia
you need an imageview to display an UIImage.
你需要一个图像视图来显示一个 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];
回答by Sehrish Khan
You can set the frame of the UIImageView:
您可以设置 UIImageView 的框架:
UIImageView* imgv= [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 120, 120)];
imgv.image = [UIImage imageNamed:@"image.png"];
Here you will have to set the position of frame by trial and error.
在这里,您必须通过反复试验来设置框架的位置。