在目标C中在iPhone上绘图 [英] Drawing on the iPhone in objective c

查看:68
本文介绍了在目标C中在iPhone上绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编程还很陌生,我有一半都做了我的(简单的)应用程序,但我想知道如何在屏幕上绘制图片(用户绘制图片),然后将该图像用于游戏只需左右移动(并检查它是否与其他图像碰撞).

I´m quite new to programming and i have sort of half made my (simple) app but i want to know how to draw an picture on the screen (the user draws the picture) and then use that image for the game to just move left and right (and to check if it is colliding with another image).

我已经知道了.

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];
}

但是然后当我在屏幕上按下时,手指上会出现一个蓝色方块,但是应收帐款却什么也没画……

but then when i press on the screen a blue square goes to the finger, but dues not draw anything...

推荐答案

创建一个类,它是UIView的子类...然后在tat中添加这些代码行...

Create a class which is a subclass of UIView... then add these lines of code in tat...

- (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];
}

在头文件中声明以下内容.....

in the header file declare the following.....

CGPoint gestureStartPoint,currentPosition;
UIBezierPath *currentPath;

并声明一个属性...

and declare a property...

@property(nonatomic,retain)UIBezierPath * currentPath;

@property(nonatomic,retain)UIBezierPath *currentPath;

在ifblock的initWIthFrame方法中,添加以下行

in the initWIthFrame method inside if block add these lines

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

创建一个viewcontroler类,然后将这些代码行添加到loadVIew方法中.

Create a viewcontroler class then add these lines of code in loadVIew method..

mainView=[[sampleView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
mainView.backgroundColor=[UIColor whiteColor];
self.view=mainView;

其中sampleView是您创建的UIView子类b4....

where sampleView is the UIView subclass u created b4....

希望这对您有帮助...

Hope this helps...

这篇关于在目标C中在iPhone上绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆