自定义 UIView 类
我有一个自定义 UIView 类,它创建一个可以在主视图控制器中使用的视图,以实现更好的拖动。问题是,现在我拖动的对象只是一个黑盒子。我希望能够将我自己的自定义图像放入该框中。我该怎么做?自定义 UIView 只有一个 .h 和一个 .m,我不知道该将代码放入哪个方法中。
这是我现在使用的代码
UIImageView *player = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 37, 37)];
player.image = [UIImage imageNamed:@"ghost.png"];
[self addSubview: player];
I have a custom UIView class that creates a view I can use in my main view controller that allows for better dragging. The problem, is that right now, the object I'm dragging is just a black box. I would like to be able to put my own custom image into that box. How would I do this? The custom UIView only has a .h and a .m and I don't know which method to put the code in.
Here's the code Im using right now
UIImageView *player = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 37, 37)];
player.image = [UIImage imageNamed:@"ghost.png"];
[self addSubview: player];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以
在视图控制器主 xib 文件中添加自定义视图,并在界面构建器视图中添加图像视图
在视图控制器
viewDidLoad
方法中,您可以使用当前代码将UIImageView
添加到自定义视图重写自定义视图的
drawRect
方法并绘制那里的图像请参见此处(但请注意答案说你不应该加载在drawRect
方法中加载图像,在initWithFrame
或awakeFromNib
方法中构建视图时加载图像,具体取决于加载方式)< /p>You could
Add your custom view in your view controllers main xib file and add the image view to it in the interface builder view
In your view controllers
viewDidLoad
method you could add theUIImageView
to the custom view using your current codeOverride the
drawRect
method of your custom view and draw the image there see here (however note the answer which says you should not load the image in thedrawRect
method, load the image on construction of your view in theinitWithFrame
or theawakeFromNib
method depending on how you load it)