2011-08-01 7 views
2

サブクラスUIViewをどのように使用して、それをdrawRectと一緒に使用してグラフィックスを描くのですか? フレームワークを追加してから、ヘッダファイルQuartzCoreまたはその他のファイルにインクルードする必要がありますか?UIViewをサブクラス化し、drawRectでグラフィックを描画する方法をサブクラス化するにはどうすればいいですか?


合わせた溶液:


#import <UIKit/UIKit.h> 
@interface drawingViewController : UIViewController { 
} 
@end 

#import "drawingViewController.h" 
#import "myview.h" 
@implementation drawingViewController 
- (void)viewDidLoad { 
    [super viewDidLoad]; 

    CGRect rect = CGRectMake(0,0,340,480); 
    myview *ui = [[myview alloc] initWithFrame:rect]; 
    [self.view addSubview:ui]; 
} 

#import <UIKit/UIKit.h> 
@interface myview : UIView { 
} 
@end 

#import "myview.h" 
@implementation myview 
- (id)initWithFrame:(CGRect)frame { 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code. 
    } 
    return self; 
} 


// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
- (void)drawRect:(CGRect)rect { 
    // Drawing code. 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextAddArc(context, 50, 50, 50, 0, 30, 0); 
CGContextFillPath(context); 

    //set the fill or stroke color 
    CGContextSetRGBFillColor(context, 1, 0.5, 0.5, 1.0); 
    CGContextSetRGBStrokeColor(context, 0.5, 1, 0.5, 1.0); 

    //fill or draw the path 
    CGContextDrawPath(context, kCGPathStroke); 
    CGContextDrawPath(context, kCGPathFill); 

    ------------------------------ 
    CGContextAddArc(context, 100, 100, 20, 0, 30, 0); 

    //set the fill or stroke color 
    CGContextSetRGBFillColor(context, 1, 0.5, 0.5, 1.0); 
    CGContextSetRGBStrokeColor(context, 0.5, 1, 0.5, 1.0); 

    //fill or draw the path 
    CGContextDrawPath(context, kCGPathStroke); 
    CGContextDrawPath(context, kCGPathFill); 

} 

他の事に注意します

Drawing triangle/arrow on a line with CGContext

CGContext Optimization

iPhone clear CGContext

Saving and restoring CGContext

How do I draw a line on the iPhone?

.Mファイル内の.hファイルで

How to draw string in other device orientations using CGContext iOS

+0

を再描画するsetNeedsDisplayを呼び出しますUIVieをサブクラス化したいw? – lifemoveson

答えて

0

#import <QuartzCore/QuartzCore.h> 
@interface YourView : UIView { 

...

- (void)drawRect:(CGRect)rect{ 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    //your drawings 
} 

その後、あなたはなぜRECT

関連する問題