iPhone 自定义 ui 组件
我对 iPhone 开发非常陌生。
我想创建一个可以包含正方形、圆形或三角形的 ui 组件。我将向其构造函数传递一个参数来说明我想要什么样的形状。
我希望这个物体可以通过加速度计或用户的手指移动。我也想把它变大或变小。
我想过创建一个继承 UIView 的自定义 UI 组件。
我不知道如果我用 Quartz 2D 绘制一个形状,它的行为会像 UI 组件一样。
我可以这样做吗?您有更好的解决方案吗?
I'm very very new on iPhone development.
I want to create an ui component that can contains a square, a circle or a triangle. I will pass a parameter to its constructor to say it what kind of shape I want.
I want that this object can be move by acelerometer or by user's finger. I also want to make it bigger or smaller.
I've thought to create a custom UI component that inherits of UIView.
I don't know if I draw a shape with Quartz 2D it will be behave like an UI Component.
Can I do that? Do you have a better solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为简单而常规的解决方案是子类化 UIView。这就是你要做的事。
从概念上讲,
UIView
通过绘制它们来保留其视觉内容。 (大多数情况下使用Quartz
!)视觉内容是否发生变化?UIView
只是重新绘制它们。当您更改任何内容时,UIView
会重新绘制所有内容。 (当然,这是概念性描述,实际情况有很大不同,因为有大量优化)但是重要的一点是您要做的只是覆盖一个方法。因为
UIKit
代替您完成了大部分重要的优化。UIView
使用- (void)drawRect:(CGRect)rect
方法绘制其视觉内容。您可以重写此方法来绘制形状。如果您在此方法中绘制某些内容,该形状将绘制在透明视图上。如果您操作视图(缩放、移动、旋转...)
UIKit
可能会请求视图重绘其图形。并且,视图将调用绘制形状的方法。这是非常常规的技术。大多数自定义图形和动画都是用这种方法构建的。您所要做的只是在方法- (void)drawRect:(CGRect)rect
中编写绘图代码。参考文档中有关于这个概念的很好的指南:
并注意您必须使用通过调用
UIGraphicsGetCurrentContext 检索的
方法而不是创建您自己的方法。上下文表示实际设备屏幕。如果您创建自己的上下文,则它们都无法提供实际的设备屏幕。CGContextRef
进行绘制(空白
);
举个例子:
也许您需要某种形式的优化。
UIKit
进行基本的优化,但不是全部。因为图形优化在内存和处理之间有很多权衡。您可以从CALayer
和shouldRasterize
获得一些优化。但是,如果您需要更多,则必须开始研究实时图形。I think simple and regular solution is subclassing
UIView
. Just what you're going to do.Conceptually,
UIView
is keep its visual content by drawing them. (withQuartz
in most cases!) If the visual content changed?UIView
just re-draws them. When you change anything,UIView
re-draws everything. (Of course, this is conceptual description, real is a lot differ because there are heavy optimizations)However the important point is what you have to do for this is just overriding a method. Because
UIKit
does most of important optimizations instead of you.UIView
draws its visual content with- (void)drawRect:(CGRect)rect
method. And you can override this method to draw your shapes. If you draw something in this method, the shape will be drawn on the transparent view.If you manipulate the view (scaling, move, rotate...)
UIKit
may request the view to redraw its graphics. And, the view will recall the method to draw your shape. This is very regular technique. Most custom graphics and animation built with this method. All what you have to do is just writing drawing code in the method- (void)drawRect:(CGRect)rect
.There're good guides about this concept on reference documentation:
And take care about you have to draw with
CGContextRef
retrieved by callingUIGraphicsGetCurrentContext (
method instead of creating your own. The context means actual device screen. If you make your own context, none of them can offer actual device screen.void
);
As an example:
Maybe you'll need a sort of optimization in any form.
UIKit
does basic optimizations but not everything. Because graphic optimization has many trade-offs between memory and processing. You can gain a few optimization fromCALayer
andshouldRasterize
. However if you need more, you have to start studying about real-time graphics.正如 Eonil 所描述的那样,当然可以绘制自定义视图。但是,您是否考虑过简单地使用
UIImageView
?It's certainly possible to draw a custom view as Eonil describes. However, have you considered simply using a
UIImageView
?