开发者

iPhone custom ui components

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开发者_Python百科 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?


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. (with Quartz 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:

  • http://developer.apple.com/library/ios/#documentation/uikit/reference/UIView_Class/UIView/UIView.html%23//apple_ref/doc/uid/TP40006816-CH3-BBCDGJHF
  • http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/CreatingViews/CreatingViews.html%23//apple_ref/doc/uid/TP40009503-CH5-SW23

And take care about you have to draw with CGContextRef retrieved by calling UIGraphicsGetCurrentContext ( void ); 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.

As an example:

@interface MyView : UIView
@end

@implementation MyView
- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    // Do your drawing here with the `context`.
    // And don't release it because the it is not yours, just borrowed from `UIKit`.
}
@end

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 from CALayer and shouldRasterize. However if you need more, you have to start studying about real-time graphics.


It's certainly possible to draw a custom view as Eonil describes. However, have you considered simply using a UIImageView?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜