我想要一个每次按下按钮都会移动的图像。它应该每次移动一组像素值。
我希望image1现在有一个position属性,但就我所见,它没有。
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轴)。
您需要一个imageview来显示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];
您可以设置UIImageView:的框架
UIImageView* imgv= [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 120, 120)];
imgv.image = [UIImage imageNamed:@"image.png"];
在这里,你必须通过反复试验来设置框架的位置。