iOS平台如何画线

Posted

技术标签:

【中文标题】iOS平台如何画线【英文标题】:How to draw lines in iOS platform 【发布时间】:2013-02-04 10:26:33 【问题描述】:

我用的是竹书和纸之类的应用,在iPad上,体验真的很好,效果如何?在屏幕上绘制直线或曲线时使用了哪种技术?

【问题讨论】:

这不完全是一个编程问题,但在 ios 中很可能是使用 Quartz 框架来绘制线条和形状。 可能重复***.com/questions/3128752/draw-line-in-uiview 【参考方案1】:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 

    mouseSwiped = NO;
    UITouch *touch = [touches anyObject];

    if ([touch tapCount] == 2) 
        //self.imageView.image = nil;
        return;
    

    lastPoint = [touch locationInView:self.view];
    lastPoint.y -= 20;



- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
    mouseSwiped = YES;

    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:self.view];
    currentPoint.y -= 20;

    CGFloat components[3];
    [self getRGBComponents:components forColor:self.colorView.backgroundColor];
    NSLog(@"%f %f %f", components[0], components[1], components[2]);

    UIGraphicsBeginImageContext(self.view.frame.size);
    [self.imageView.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), components[0], components[1], components[2],5.0);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);


        CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeNormal);


    CGContextStrokePath(UIGraphicsGetCurrentContext());
    self.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    lastPoint = currentPoint;

    mouseMoved++;

    if (mouseMoved == 10) 
        mouseMoved = 0;
    



- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 

    UITouch *touch = [touches anyObject];

    if ([touch tapCount] == 2) 
        //self.imageView.image = nil;
        return;
    


    if(!mouseSwiped) 

        CGFloat components[3];
        [self getRGBComponents:components forColor:self.colorView.backgroundColor];
        NSLog(@"%f %f %f", components[0], components[1], components[2]);

        UIGraphicsBeginImageContext(self.view.frame.size);
        [self.imageView.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), components[0], components[1], components[2], 5.0);
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        CGContextFlush(UIGraphicsGetCurrentContext());
        self.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    

同时查看以下链接

http://www.techotopia.com/index.php/An_iOS_4_iPhone_Graphics_Drawing_Tutorial_using_Quartz_2D

https://***.com/questions/6449661/drawing-2d-images-on-iphone-tutorials

http://mobile.tutsplus.com/tutorials/iphone/ios-sdk_freehand-drawing/

http://www.raywenderlich.com/18840/how-to-make-a-simple-drawing-app-with-uikit

【讨论】:

以上是关于iOS平台如何画线的主要内容,如果未能解决你的问题,请参考以下文章

iOS Swift - 在 UIImage 上画线以保存到文件

iOS:在两个动画视图之间画线

如何仅使用 iOS 源代码更新 Cordova iOS 平台项目

如何跨平台与Android和IOS

如何在 Codemagic 中指定目标平台 ios 版本号

Quickblox - 如何将通知推送到 Android 和 iOS 平台?