在uiview上绘制随机设计

Posted

技术标签:

【中文标题】在uiview上绘制随机设计【英文标题】:Draw random design on uiview 【发布时间】:2012-05-21 12:37:25 【问题描述】:

我想在 uiview 上绘制一个随机设计,就像我们在画笔上绘制一样我想要用户在屏幕上触摸开始绘制的位置。就像他想在里面写好而不是画出来。如果他想做鸭子或球,那么他就可以做到。请帮帮我。

【问题讨论】:

可能重复上述问题。参考链接。 [1]:***.com/questions/3128752/draw-line-in-uiview 不,我的问题是在视图上绘制任何设计 这可能是最好的启动目的。 edumobile.org/iphone/ipad-development/… 这里没有修复设计,但我想像蝙蝠或任何其他设计一样设计 我想像画笔一样作为用户触摸屏开始在视图上绘制 【参考方案1】:

试试这个。 在此,无论我有myPic.Frame,您都必须使用self.view.frame

在头文件中:

#import <Foundation/Foundation.h>

@interface DrawView : UIView 

    UIImage *myPic;

    NSMutableArray *myDrawing;



-(void)drawPic:(UIImage *)thisPic;

-(void)cancelDrawing;

@end

在实施文件中:

#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

【讨论】:

感谢您的给予时间,但模拟器上没有显示任何内容【参考方案2】:

您必须使您的 UIView 成为上述的子类。在您的控制器 class.m 文件中,您必须添加并调用这两个方法。 我希望这行得通。

-(IBAction)清除

[self.view cancelDrawing];

-(IBAction)saveDrawing

UIGraphicsBeginImageContext(self.view.bounds.size);

[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *finishedPic = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

UIImageWriteToSavedPhotosAlbum(finishedPic, self, @selector(exitProg:didFinishSavingWithError:contextInfo:), nil);

【讨论】:

以上是关于在uiview上绘制随机设计的主要内容,如果未能解决你的问题,请参考以下文章

带有 TableView 和 CollectionView 的 UiView 不会随机刷新

每次视图出现时,UIView 层的子层显示不同/随机

通过按JButton在随机JPanel上绘制圆圈

重点突破——SVG技术动态随机绘制圆形

统计从多个 UIView 中随机抽出的 UIView 数量,即:从 9 个 UIView 中随机抽出 5 个 UIView,然后执行操作

重点突破——Canvas技术绘制随机改变的验证码