Iphone 在uiview上绘制随机设计
我想在uiview上绘制一个随机设计,就像我们在画笔上绘制一样,我想让用户在屏幕上触摸开始绘制。比如,如果他想在上面写ok,那么他就可以画出来。如果他想做鸭子或球,那么他就可以做到。请帮帮我。试试这个。 在这种情况下,无论我在哪里Iphone 在uiview上绘制随机设计,iphone,facebook,Iphone,Facebook,我想在uiview上绘制一个随机设计,就像我们在画笔上绘制一样,我想让用户在屏幕上触摸开始绘制。比如,如果他想在上面写ok,那么他就可以画出来。如果他想做鸭子或球,那么他就可以做到。请帮帮我。试试这个。 在这种情况下,无论我在哪里myPic.frame,都必须使用self.view.frame 在头文件中: #import <Foundation/Foundation.h> @interface DrawView : UIView { UIImage *myPic;
myPic.frame
,都必须使用self.view.frame
在头文件中:
#import <Foundation/Foundation.h>
@interface DrawView : UIView {
UIImage *myPic;
NSMutableArray *myDrawing;
}
-(void)drawPic:(UIImage *)thisPic;
-(void)cancelDrawing;
@end
#导入
@接口DrawView:UIView{
UIImage*myPic;
NSMutableArray*myDrawing;
}
-(无效)drawPic:(UIImage*)此Pic;
-(作废)取消提款;
@结束
在实施文件中:
#import "DrawView.h"
@implementation DrawView
-(void)drawPic:(UIImage *)thisPic {
myPic = thisPic;
[myPic retain];
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
float newHeight;
float newWidth;
if (!myDrawing) {
myDrawing = [[NSMutableArray alloc] initWithCapacity:0];
}
CGContextRef ctx = UIGraphicsGetCurrentContext();
if (myPic != NULL) {
float ratio = myPic.size.height/460;
if (myPic.size.width/320 > ratio) {
ratio = myPic.size.width/320;
}
newHeight = myPic.size.height/ratio;
newWidth = myPic.size.width/ratio;
[myPic drawInRect:CGRectMake(0,0,newWidth,newHeight)];
}
if ([myDrawing count] > 0) {
CGContextSetLineWidth(ctx, 5);
for (int i = 0 ; i < [myDrawing count] ; i++) {
NSArray *thisArray = [myDrawing objectAtIndex:i];
if ([thisArray count] > 2) {
float thisX = [[thisArray objectAtIndex:0] floatValue];
float thisY = [[thisArray objectAtIndex:1] floatValue];
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, thisX, thisY);
for (int j = 2; j < [thisArray count] ; j+=2) {
thisX = [[thisArray objectAtIndex:j] floatValue];
thisY = [[thisArray objectAtIndex:j+1] floatValue];
CGContextAddLineToPoint(ctx, thisX,thisY);
}
CGContextStrokePath(ctx);
}
}
}
}
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[myDrawing addObject:[[NSMutableArray alloc] initWithCapacity:4]];
CGPoint curPoint = [[touches anyObject] locationInView:self];
[[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.x]];
[[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.y]];
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint curPoint = [[touches anyObject] locationInView:self];
[[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.x]];
[[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.y]];
[self setNeedsDisplay];
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint curPoint = [[touches anyObject] locationInView:self];
[[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.x]];
[[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.y]];
[self setNeedsDisplay];
}
-(void)cancelDrawing {
[myDrawing removeAllObjects];
[self setNeedsDisplay];
}
- (void)dealloc {
[super dealloc];
[myPic release];
[myDrawing release];
}
@end
#导入“DrawView.h”
@实现DrawView
-(无效)drawPic:(UIImage*)此Pic{
myPic=此PIC;
[myPic保留];
[自我设置需要显示];
}
-(void)drawRect:(CGRect)rect{
浮动新高度;
浮动宽度;
如果(!myDrawing){
myDrawing=[[NSMutableArray alloc]initWithCapacity:0];
}
CGContextRef ctx=UIGraphicsGetCurrentContext();
如果(myPic!=NULL){
浮动比率=myPic.size.height/460;
如果(myPic.size.width/320>比率){
比率=myPic.size.width/320;
}
newHeight=myPic.size.height/ratio;
newWidth=myPic.size.width/比值;
[myPic drawInRect:CGRectMake(0,0,新宽度,新高度)];
}
如果([myDrawing count]>0){
CGContextSetLineWidth(ctx,5);
对于(int i=0;i<[myDrawing count];i++){
NSArray*thisArray=[myDrawing objectAtIndex:i];
如果([thisArray count]>2){
float thisX=[[thisArray对象索引:0]floatValue];
float thisY=[[thisArray对象索引:1]floatValue];
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx、thisX、thisY);
对于(int j=2;j<[此数组计数];j+=2){
thisX=[[thisArray objectAtIndex:j]floatValue];
thisY=[[thisArray对象索引:j+1]floatValue];
CGContextAddLineToPoint(ctx、thisX、thisY);
}
CGContextStrokePath(ctx);
}
}
}
}
-(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件{
[myDrawing addObject:[[NSMutableArray alloc]initWithCapacity:4]];
CGPoint curPoint=[[触摸任何对象]位置查看:self];
[[myDrawing lastObject]addObject:[NSNumber numberWithFloat:curPoint.x]];
[[myDrawing lastObject]addObject:[NSNumber numberWithFloat:curPoint.y]];
}
-(无效)触摸移动:(NSSet*)触摸事件:(UIEvent*)事件{
CGPoint curPoint=[[触摸任何对象]位置查看:self];
[[myDrawing lastObject]addObject:[NSNumber numberWithFloat:curPoint.x]];
[[myDrawing lastObject]addObject:[NSNumber numberWithFloat:curPoint.y]];
[自我设置需要显示];
}
-(void)touchesend:(NSSet*)toucheevent:(UIEvent*)event{
CGPoint curPoint=[[触摸任何对象]位置查看:self];
[[myDrawing lastObject]addObject:[NSNumber numberWithFloat:curPoint.x]];
[[myDrawing lastObject]addObject:[NSNumber numberWithFloat:curPoint.y]];
[自我设置需要显示];
}
-(作废)取消提款{
[myDrawing removeAllObjects];
[自我设置需要显示];
}
-(无效)解除锁定{
[super dealoc];
[myPic发布];
[我的图纸发布];
}
@结束
您必须使UIView成为上述内容的子类。在controller class.m文件中,必须添加并调用这两个方法。
我希望这能奏效
-(i)清除{
}
-(iAction)保存图形
{
}上述问题可能重复。请参阅链接。[1] :不,我的问题是不同的,在视图上绘制任何设计这可能是开始时的最佳设计。没有在这有固定的设计,但我想设计像蝙蝠或任何其他设计我想像一个油漆刷作为用户触摸屏上开始绘制感谢你给了我时间,但没有显示在模拟器上
[self.view cancelDrawing];
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *finishedPic = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(finishedPic, self, @selector(exitProg:didFinishSavingWithError:contextInfo:), nil);