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).
我´相当新的编程和我的一半(简单的)应用程序但我想知道如何在屏幕上画一个图(用户绘制图),然后使用图像的游戏只是移动左右(和检查如果是碰撞与另一个图像)。
I've got this..
我有这个. .
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...
但当我在屏幕上按下一个蓝色方块时,它就会指向手指,但不会画出任何东西……
2 个解决方案
#1
2
Create a class which is a subclass of UIView... then add these lines of code in tat...
创建一个类,它是UIView的子类…然后在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(原子、保留)UIBezierPath * currentPath;
in the initWIthFrame method inside if block add these lines
在initWIthFrame方法中如果块添加这些行。
currentPath = [[UIBezierPath alloc]init];
currentPath.lineWidth=3;
Create a viewcontroler class then add these lines of code in loadVIew method..
创建一个viewcontroler类,然后在loadVIew方法中添加这些代码行。
mainView=[[sampleView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
mainView.backgroundColor=[UIColor whiteColor];
self.view=mainView;
where sampleView is the UIView subclass u created b4....
其中sampleView是UIView u b4 ....创建子类
Hope this helps...
希望这有助于……
#2
1
Drawing user generated pictures with Cocoa Touch is a 2 step process. A UIView will only draw the latest touch you hand it after clearing all the previously user drawn stuff.
用Cocoa Touch绘制用户生成的图片是一个两步的过程。UIView只会在清除之前用户绘制的东西后绘制最新的触摸。
One possible solution is to save all the user touches in a history array and (re)draw all of them into the view after any new touch is added. But this can be very slow, depending on the amount of drawing required.
一种可能的解决方案是将所有用户触摸保存到历史数组中,并(重新)在添加任何新触摸后将所有用户触摸绘制到视图中。但这可能非常缓慢,这取决于需要绘制的数量。
Another possible 2 step method is to create your own bitmap drawing context. First draw your new latest thing into this context, which will have kept the older portions of the drawing if configured correctly, then draw this context into the UIView (or convert the bitmap to an image displayed in a layer over the view).
另一个可能的两步方法是创建自己的位图绘制上下文。首先将最新的内容绘制到这个上下文中,如果配置正确,这个上下文中将保留绘图的旧部分,然后将这个上下文中绘制到UIView中(或者将位图转换为显示在视图层中的图像)。
#1
2
Create a class which is a subclass of UIView... then add these lines of code in tat...
创建一个类,它是UIView的子类…然后在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(原子、保留)UIBezierPath * currentPath;
in the initWIthFrame method inside if block add these lines
在initWIthFrame方法中如果块添加这些行。
currentPath = [[UIBezierPath alloc]init];
currentPath.lineWidth=3;
Create a viewcontroler class then add these lines of code in loadVIew method..
创建一个viewcontroler类,然后在loadVIew方法中添加这些代码行。
mainView=[[sampleView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
mainView.backgroundColor=[UIColor whiteColor];
self.view=mainView;
where sampleView is the UIView subclass u created b4....
其中sampleView是UIView u b4 ....创建子类
Hope this helps...
希望这有助于……
#2
1
Drawing user generated pictures with Cocoa Touch is a 2 step process. A UIView will only draw the latest touch you hand it after clearing all the previously user drawn stuff.
用Cocoa Touch绘制用户生成的图片是一个两步的过程。UIView只会在清除之前用户绘制的东西后绘制最新的触摸。
One possible solution is to save all the user touches in a history array and (re)draw all of them into the view after any new touch is added. But this can be very slow, depending on the amount of drawing required.
一种可能的解决方案是将所有用户触摸保存到历史数组中,并(重新)在添加任何新触摸后将所有用户触摸绘制到视图中。但这可能非常缓慢,这取决于需要绘制的数量。
Another possible 2 step method is to create your own bitmap drawing context. First draw your new latest thing into this context, which will have kept the older portions of the drawing if configured correctly, then draw this context into the UIView (or convert the bitmap to an image displayed in a layer over the view).
另一个可能的两步方法是创建自己的位图绘制上下文。首先将最新的内容绘制到这个上下文中,如果配置正确,这个上下文中将保留绘图的旧部分,然后将这个上下文中绘制到UIView中(或者将位图转换为显示在视图层中的图像)。