2011-09-13 1 views
3

私はプログラミングにはあまり新しく、私は(単純な)アプリを作りましたが、画面上に画像を描画する方法(ユーザーが画像を描画する方法)を知りたいその画像を使用してゲームを左右に動かすだけで(そして別の画像と衝突しているかどうかを確認する)目的のCでiPhoneに描画する

...私は

float pointx; 
float pointy; 


- (void)drawRect:(CGRect)rect { 
CGColorRef blue = [[UIColor blueColor] CGColor]; 

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextClearRect(context, self.bounds); 

CGContextSetFillColorWithColor(context, blue); 
CGContextFillRect(context, CGRectMake(pointx, pointy, 10, 10)); 

} 

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
UITouch *touch=[[event allTouches]anyObject]; 
CGPoint point = [touch locationInView:touch.view]; 
pointx = point.x; 
pointy = point.y; 
[self setNeedsDisplay]; 
} 

。これを持っているが、私は青い四角が指に入る、画面上を押したときに、しかし、会費は何も描画しませ

+0

あなたはユーザーが画面上で指を動かすと画像が見えますか? – booleanBoy

+0

yeah ........... – miniman

答えて

1

描画ユーザーが画像を生成しましたココアタッチの2ステップのプロセスです。 UIViewは、以前にユーザーが描画したものをすべてクリアした後、手作業で最新のタッチを描画します。

考えられる解決策の1つは、すべてのユーザーのタッチをヒストリ配列に保存し、新しいタッチが追加された後にそれらをすべてビューに描画することです。しかし、これは必要な描画の量に応じて非常に遅くなる可能性があります。

もう1つの可能な方法は、独自のビットマップ描画コンテキストを作成することです。最初に新しいコンテキストを描画してください。これは、正しく設定されていれば、図面の古い部分を保持しておき、このコンテキストをUIViewに描画します(または、ビットマップをビュー上のレイヤーに表示される画像に変換します)。

2

次いでTATのコード行を追加...のUIViewのサブクラスであるクラスを作成...

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
UITouch *touch = [touches anyObject]; 
gestureStartPoint = [touch locationInView:self]; 
[currentPath moveToPoint:(gestureStartPoint)]; 

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
UITouch *touch = [touches anyObject]; 
currentPosition = [touch locationInView:self]; 
[currentPath addLineToPoint:(currentPosition)]; 
[self setNeedsDisplay]; 

}

- (void)drawRect:(CGRect)rect { 
[[UIColor redColor] set]; 
[currentPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0]; 
} 

ヘッダファイルに次のように宣言します.....

CGPoint gestureStartPoint,currentPosition; 
UIBezierPath *currentPath; 

とプロパティを宣言...

@property(アトミック、保持)UIBezierPath *電流経路。

内部initWIthFrame方法において

ブロックは、これらの行を追加する場合

currentPath = [[UIBezierPath alloc]init]; 
currentPath.lineWidth=3; 

viewcontrolerクラスを作成し、その後loadViewメソッド内のコードの次の行を追加..

mainView=[[sampleView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)]; 
mainView.backgroundColor=[UIColor whiteColor]; 
self.view=mainView; 
sampleViewはUIViewのサブクラスUある

b4 ....

希望します。

+0

私の書式設定の方法は申し訳ありません... – booleanBoy

+0

booleanBoy、メインビューを宣言していません... – miniman

+0

こんにちは......... – miniman

関連する問題