OGeek|极客世界-中国程序员成长平台

标题: iphone - 将 UIImageView 添加到 UIView [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 15:38
标题: iphone - 将 UIImageView 添加到 UIView

我已经坚持了一段时间,所以任何帮助将不胜感激。

我基本上已经创建了一个名为 shape 的类,它是一个 UIView,我想向该 UIView 添加一个 UIImageView

这是我的 .h 文件:

@interface Shape : UIView {

 int blockWidth;
 int blockHeight;
 NSString *colour;

 IBOutlet UIImageView *blockView;

}

@property (nonatomic, retain) IBOutlet UIImageView *blockView;
@property (nonatomic, retain) NSString *colour;

-(void)setColourNSString *)colour;
-(void)createShapeint)blocksX int)blocksY;


@end

这是我的 .m 文件:

@implementation Shape

@synthesize blockView;
@synthesize colour;

- (void)setColourNSString *)colour{
 NSLog(@"Colour: %@", colour);
}

-(void)createShapeint)blocksX int)blocksY{
 self.frame = CGRectMake(0, 0, 200, 200);
 blockView.frame = CGRectMake(0, 0, 100, 100);

 self.backgroundColor = [UIColor greenColor];
 blockView.backgroundColor = [UIColor redColor];
 [self addSubview:blockView];

}

- (void)dealloc {

 [blockView release];
 [colour release];
 [super dealloc];
}


@end

非常感谢!

嘿,再次抱歉,如果这令人困惑,但我试图将此 c 尖锐代码移植到目标中,上面的代码是我的第一次尝试,显然不需要完成,但你可以希望看到我想要实现的目标,抱歉但我对 Objective C 完全陌生,它与我习惯使用的其他语言完全不同:S

public class Shape : UIView
    {
        public UIImageView blockView;

        private int blockWidth = 40;
        private int blockHeight = 40;


        public Shape (int startX, int startY ,string colour, int blocksX, int blocksY)
        {
            Console.WriteLine("Colour: "+colour+" Blocks: "+blocksX+" "+blocksY);

            this.Frame = new RectangleF(startX,startY,blockWidth*blocksX,blockHeight*blocksY);
            this.UserInteractionEnabled = true;


            for(int i = 0; i<blocksX; i++)
            {
                for(int j = 0; j<blocksY; j++)
                {
                    blockView = new UIImageView(UIImage.FromFile("Images/Blocks/"+colour+"block.jpg"));
                    blockView.Frame = new RectangleF(blockWidth*i,blockHeight*j,blockWidth,blockHeight);
                    Console.WriteLine("I: "+i+" J: "+j);
                    this.AddSubview(blockView);
                }
            }
        }   



Best Answer-推荐答案


(删除旧文本)

实际上,您在 C# 代码中所做的在 Objective-C 中并没有什么不同(此代码未经测试!):

在头文件中:

#import <UIKit/UIKit.h>

@interface ShapeView : UIView {

}

@end

实现文件:

#import "ShapeView.h"

#define kBlockWidth 40
#define kBlockHeight 40

@implementation ShapeView

- (id)initWithStartXint)startX startYint)startY colourNSString*)colour blocksXint)blocksX blocksY:(int)blocksY
{
    CGRect frame = CGRectMake(startX, startY, kBlockWidth * blocksX, kBlockHeight * blocksY);

    if ((self = [super initWithFrame:frame]) != nil)
    {
        for (int i = 0; i < blocksX; i++)
        {
            for (int j = 0; j < blocksY; j++)
            {
                UIImage* image = [UIImage imageNamed:[NSString stringWithFormat"Images/Blocks/%@block.jpg", colour]];
                UIImageView* blockView = [[UIImageView alloc] initWithImage:image];

                blockView.frame = CGRectMake(kBlockWidth * i,
                                             kBlockHeight * j,
                                             kBlockWidth,
                                             kBlockHeight);

                [self addSubview:blockView];
                [blockView release];
            }
        }
    }

    return self;
}

@end

此实现只是将 UIImageViews 添加为 subview 。注意 blockView 被分配和配置,然后添加为 self 的 subview 。调用 -addSubview: 会保留新的 subview 并实际将其添加到 View 层次结构中。因此,blockView 立即发布。当整个 ShapeView 对象被释放时,UIView 的 -dealloc 实现负责删除和释放 subview 。

希望有帮助!

关于iphone - 将 UIImageView 添加到 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4346876/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4