如何设置一个 UIImageView 指向另一个 UIImageView

发布于 2024-12-11 06:43:47 字数 2019 浏览 0 评论 0原文

我想要一个指向 UIImage 视图的指针,这样我就不必重复很多代码。我需要根据它所在的按钮来选择图像,但最后我将相同的代码应用于任何数字。 oneDigittwoDigitthirdDigit 都是头文件中的@property(nonatomic,retain)。我希望 imageView 能够指向这些 UIImageView 之一。现在,UIImageViews 没有被 removeFromSuperview 删除,因为 imageView 没有指向对象,而是创建自己的对象。

当前,当按下按钮时,会添加一个图像。我希望删除以前的图像并根据按下的按钮绘制新图像。如果我用 oneDigit 替换 imageView,代码就会按照我想要的方式工作,但这需要我为每个变量添加更多代码。

请参阅下面的简化示例:

- (void)viewDidLoad
{   ...
    UIImageView *oneDigit;
    UIImageView *twoDigit;
    UIImageView *threeDigit;
}

-(IBAction)pressButton:(id)sender {
    UIImage *image;
    UIImageView *imageView;
    UIButton *btn = (UIButton*)sender;
    int value = [btn.titleLabel.text intValue];
    switch (value) {
        case 10:
        case 15:
        case 20:
            [twoDigit removeFromSuperview]; //Remove old image from screen
            imageView = twoDigit; //Update imageView pointer for current case
            image = [UIImage imageNamed:@"twoDigit.png"];
            break;
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
            [oneDigit removeFromSuperview];
            imageView = oneDigit;
            image = [UIImage imageNamed:@"oneDigit.png"];
            break;
        case 100:
        case 200:
        case 300:
            [threeDigit removeFromSuperview];
            imageView = threeDigit;
            image = [UIImage imageNamed:@"threeDigit.png"];
            break;
        default:
            break;
    }
    CGPoint point = btn.frame.origin;
    CGFloat xposition = point.x - ((image.size.width - btn.frame.size.width) /2);
    CGFloat yposition = point.y - ((image.size.height - btn.frame.size.height) /2);
    imageView = [ [ UIImageView alloc ] initWithFrame:CGRectMake(xposition, yposition, image.size.width, image.size.height) ];
    imageView.image = image;
    imageView.contentMode = UIViewContentModeCenter;
    [self.view addSubview:imageView];
}

I want to have a pointer to an UIImage view so that I don't have to repeat a lot of my code. I need to choose the image based on which button it is but then at the end I apply the same code to whatever digit. oneDigit, twoDigit, and threeDigit are all @property(nonatomic, retain) in the header file. I want imageView to be able to point to one of these UIImageViews. Right now the UIImageViews are not being removed with removeFromSuperview because imageView is not pointing to the objects but creating its own object..

Currently when a button is pressed a Image is added. I want the previous image removed and the new image drawn according to what button was pressed. The code works how I want if I replace imageView with oneDigit, but that would require me to add more code for each variable.

See simplified example below:

- (void)viewDidLoad
{   ...
    UIImageView *oneDigit;
    UIImageView *twoDigit;
    UIImageView *threeDigit;
}

-(IBAction)pressButton:(id)sender {
    UIImage *image;
    UIImageView *imageView;
    UIButton *btn = (UIButton*)sender;
    int value = [btn.titleLabel.text intValue];
    switch (value) {
        case 10:
        case 15:
        case 20:
            [twoDigit removeFromSuperview]; //Remove old image from screen
            imageView = twoDigit; //Update imageView pointer for current case
            image = [UIImage imageNamed:@"twoDigit.png"];
            break;
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
            [oneDigit removeFromSuperview];
            imageView = oneDigit;
            image = [UIImage imageNamed:@"oneDigit.png"];
            break;
        case 100:
        case 200:
        case 300:
            [threeDigit removeFromSuperview];
            imageView = threeDigit;
            image = [UIImage imageNamed:@"threeDigit.png"];
            break;
        default:
            break;
    }
    CGPoint point = btn.frame.origin;
    CGFloat xposition = point.x - ((image.size.width - btn.frame.size.width) /2);
    CGFloat yposition = point.y - ((image.size.height - btn.frame.size.height) /2);
    imageView = [ [ UIImageView alloc ] initWithFrame:CGRectMake(xposition, yposition, image.size.width, image.size.height) ];
    imageView.image = image;
    imageView.contentMode = UIViewContentModeCenter;
    [self.view addSubview:imageView];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情话难免假 2024-12-18 06:43:47

假设 oneDigit、twoDigit 和 ThreeDigit 是 UIImageView,您可以执行类似的操作

imageView = oneDigit;

,因为两者都是指针,因此将 imageView 设置为 oneDigit 只会将 imageView 指向 oneDigit 的位置。当你将它设置为新图像时,不要先自动释放它,因为如果它设置为例如 ThreeDigit,它也会自动释放 ThreeDigit。

另外,在将 imageView 设置为新图像后,您不需要手动设置 imageView 的图像属性,因为它本质上是引用具有相同图像的完全相同的 UIImageView ;它只是用了不同的名字。

Assuming that oneDigit, twoDigit and threeDigit are UIImageViews, you can just do something similar to

imageView = oneDigit;

Since both are pointers, setting imageView to oneDigit will simply point imageView to the location of oneDigit. When you set it to the new image, don't autorelease it first, because if it's set to threeDigit for example, it will autorelease threeDigit too.

Also, you don't need to set the image property of imageView manually after setting imageView to the new image, because it's essentially referencing the exact same UIImageView with the same image; it's just under a different name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文