gpt4 book ai didi

iphone - 将 UIImageView 添加到 UIView

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:52:27 25 4
gpt4 key购买 nike

我已经坚持了一段时间,所以非常感谢任何帮助。

我基本上创建了一个名为 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)setColour:(NSString *)colour;
-(void)createShape:(int)blocksX :(int)blocksY;


@end

这是我的 .m 文件:

@implementation Shape

@synthesize blockView;
@synthesize colour;

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

-(void)createShape:(int)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 sharp 代码移植到目标中,上面的代码是我的第一次尝试,显然没有完成的地方,但你可以希望看到我正在努力实现的目标,抱歉但我对 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);
}
}
}

最佳答案

(删除旧文本)

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

在头文件中:

#import <UIKit/UIKit.h>

@interface ShapeView : UIView {

}

@end

执行文件:

#import "ShapeView.h"

#define kBlockWidth 40
#define kBlockHeight 40

@implementation ShapeView

- (id)initWithStartX:(int)startX startY:(int)startY colour:(NSString*)colour blocksX:(int)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

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

希望对您有所帮助!

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com