iphone - 将 UIImageView 添加到 UIView
<p><p>我已经坚持了一段时间,所以任何帮助将不胜感激。</p>
<p>我基本上已经创建了一个名为 shape 的类,它是一个 <code>UIView</code>,我想向该 <code>UIView</code> 添加一个 <code>UIImageView</code>。</p >
<p>这是我的 .h 文件:</p>
<pre><code>@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
</code></pre>
<p>这是我的 .m 文件:</p>
<pre><code>@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 = ;
blockView.backgroundColor = ;
;
}
- (void)dealloc {
;
;
;
}
@end
</code></pre>
<p>非常感谢!</p>
<p>嘿,再次抱歉,如果这令人困惑,但我试图将此 c 尖锐代码移植到目标中,上面的代码是我的第一次尝试,显然不需要完成,但你可以希望看到我想要实现的目标,抱歉但我对 Objective C 完全陌生,它与我习惯使用的其他语言完全不同:S</p>
<pre><code>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);
}
}
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>(删除旧文本)</p>
<p>实际上,您在 C# 代码中所做的在 Objective-C 中并没有什么不同(此代码未经测试!):</p>
<p>在头文件中:</p>
<pre><code>#import <UIKit/UIKit.h>
@interface ShapeView : UIView {
}
@end
</code></pre>
<p>实现文件:</p>
<pre><code>#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 = ) != nil)
{
for (int i = 0; i < blocksX; i++)
{
for (int j = 0; j < blocksY; j++)
{
UIImage* image = ];
UIImageView* blockView = [ initWithImage:image];
blockView.frame = CGRectMake(kBlockWidth * i,
kBlockHeight * j,
kBlockWidth,
kBlockHeight);
;
;
}
}
}
return self;
}
@end
</code></pre>
<p>此实现只是将 UIImageViews 添加为 subview 。注意 <code>blockView</code> 被分配和配置,然后添加为 <code>self</code> 的 subview 。调用 <code>-addSubview:</code> 会保留新的 subview 并实际将其添加到 View 层次结构中。因此,<code>blockView</code> 立即发布。当整个 <code>ShapeView</code> 对象被释放时,UIView 的 <code>-dealloc</code> 实现负责删除和释放 subview 。</p>
<p>希望有帮助!</p></p>
<p style="font-size: 20px;">关于iphone - 将 UIImageView 添加到 UIView,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/4346876/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/4346876/
</a>
</p>
页:
[1]