以编程方式设置自定义图像大小和位置

时间:2012-12-29 21:55:43

标签: ios objective-c cocoa-touch dimension activity-indicator

我尝试制作自定义活动指标并将大小和位置设置为: (x坐标280; y坐标5;高度34;宽度34) 我有代码:

UIImage *statusImage = [UIImage imageNamed:@"activity1.png"];
    UIImageView *activityImageView = [[UIImageView alloc]
                                      initWithImage:statusImage];

            activityImageView.animationImages = [NSArray arrayWithObjects:
                                         [UIImage imageNamed:@"activity1.png"],
                                         [UIImage imageNamed:@"activity2.png"],
                                         [UIImage imageNamed:@"activity3.png"],
                                         [UIImage imageNamed:@"activity4.png"],
                                         [UIImage imageNamed:@"activity5.png"],
                                         [UIImage imageNamed:@"activity6.png"],
                                         [UIImage imageNamed:@"activity7.png"],
                                         [UIImage imageNamed:@"activity8.png"],
                                         [UIImage imageNamed:@"activity9.png"],
                                         [UIImage imageNamed:@"activity10.png"],
                                         [UIImage imageNamed:@"activity11.png"],
                                         [UIImage imageNamed:@"activity12.png"],
                                         [UIImage imageNamed:@"activity13.png"],
                                         [UIImage imageNamed:@"activity14.png"],
                                         [UIImage imageNamed:@"activity15.png"],
                                         [UIImage imageNamed:@"activity16.png"],
                                         [UIImage imageNamed:@"activity17.png"],
                                         [UIImage imageNamed:@"activity18.png"],
                                         nil];

    activityImageView.animationDuration = 0.8;

    [activityImageView startAnimating];

    [self.view addSubview:activityImageView];

这使得圈子理解我需要代码:

activityImageView.frame = CGRectMake(
                                             self.view.frame.size.width/2
                                             -statusImage.size.width/2,
                                             self.view.frame.size.height/1.2
                                             -statusImage.size.width/1.2,
                                             statusImage.size.width,
                                             statusImage.size.height);

虽然我的问题是我不明白如何使用这段代码将坐标和大小设置为我想要的。非常感谢!

1 个答案:

答案 0 :(得分:8)

看起来你只需要这样做:

activityImageView.frame = CGRectMake(280, 5, 34, 34);

上面CGRectMake调用中的前2个参数是视图左上角相对于其超视图的X和Y坐标。其余参数是视图所需的宽度和高度。

另外,不要忘记将图像视图添加到超级视图中,例如

[self.view:addSubview:activityImageView];