iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了iOS核心图形如何擦除绘图大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_874_0@
@H_874_0@
我正在关注我的涂鸦应用程序的本教程.

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

他的擦除功能是使用白色绘图.但我正在使用图像背景,当我擦除时,我真的需要擦除.

我试过了

CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeClear);

但它不起作用.这是我的代码

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    _mouseSwiped = NO;
    UITouch *touch = [touches anyObject];
    _lastPoint = [touch LOCATIOnInView:self.tempImageView];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    _mouseSwiped = YES;
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch LOCATIOnInView:self.tempImageView];

    UIGraphicsBeginImageContext(self.tempImageView.frame.sizE);
    [self.tempImageView.image drawInRect:CGRectMake(0,self.tempImageView.frame.size.width,self.tempImageView.frame.size.height)];
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(),_lastPoint.x,_lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(),currentPoint.x,currentPoint.y);
    CGContextSetLineCap(UIGraphicsGetCurrentContext(),kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(),_width );


    CGContextSetRGBstrokeColor(UIGraphicsGetCurrentContext(),_red,_green,_blue,1.0);

    if (_isErasing) {
        CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeClear);
    }
    else {
        CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
    }

    CGContextstrokePath(UIGraphicsGetCurrentContext());
    self.tempImageView.image = UIGraphicsGetImageFromCurrentImageContext();
    [self.tempImageView setAlpha:_alpha];
    UIGraphicsEndImageContext();

    _lastPoint = currentPoint;
}

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

    CGSize size = self.tempImageView.frame.size;

    if(!_mouseSwiped) {
        UIGraphicsBeginImageContext(self.tempImageView.frame.sizE);
        [self.tempImageView.image drawInRect:CGRectMake(0,size.width,size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(),kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(),_width);


        CGContextSetRGBstrokeColor(UIGraphicsGetCurrentContext(),_alpha);
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(),_lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(),_lastPoint.y);
        CGContextstrokePath(UIGraphicsGetCurrentContext());
        CGContextFlush(UIGraphicsGetCurrentContext());
        self.tempImageView.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }




    UIGraphicsBeginImageContext(self.drawImageView.frame.sizE);
    [self.drawImageView.image drawInRect:CGRectMake(0,size.height) blendMode:kCGBlendModeNormal alpha:1.0];

    if (_isErasing) {
        CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeClear);
    }

    [self.tempImageView.image drawInRect:CGRectMake(0,self.tempImageView.frame.size.height) blendMode:kCGBlendModeNormal alpha:_alpha];


    self.drawImageView.image = UIGraphicsGetImageFromCurrentImageContext();
    self.tempImageView.image = nil;
    UIGraphicsEndImageContext();
}

解决方法

通过在触摸开始时交换tempImageView和drawImageView来解决这个问题,如果它正在擦除

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    _mouseSwiped = NO;
    UITouch *touch = [touches anyObject];
    _lastPoint = [touch LOCATIOnInView:self.tempImageView];

    if (_isErasing) {
        self.tempImageView.image = self.drawImageView.image;
        self.drawImageView.image = nil;
    }

}



- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    _mouseSwiped = YES;
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch LOCATIOnInView:self.tempImageView];

    UIGraphicsBeginImageContext(self.tempImageView.frame.sizE);
    [self.tempImageView.image drawInRect:CGRectMake(0,_width );


    if (_isErasing) {
        CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeClear);
    }
    else {
        CGContextSetRGBstrokeColor(UIGraphicsGetCurrentContext(),1.0);
        CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
    }

    CGContextstrokePath(UIGraphicsGetCurrentContext());
    self.tempImageView.image = UIGraphicsGetImageFromCurrentImageContext();
    [self.tempImageView setAlpha:_alpha];
    UIGraphicsEndImageContext();

    _lastPoint = currentPoint;
}





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

    CGSize size = self.tempImageView.frame.size;

    if(!_mouseSwiped) {
        UIGraphicsBeginImageContext(self.tempImageView.frame.sizE);
        [self.tempImageView.image drawInRect:CGRectMake(0,_width);


        if (_isErasing) {
            CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeClear);
        }
        else {
            CGContextSetRGBstrokeColor(UIGraphicsGetCurrentContext(),_alpha);
            CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
        }

        CGContextMoveToPoint(UIGraphicsGetCurrentContext(),size.height) blendMode:kCGBlendModeNormal alpha:1.0];
    [self.tempImageView.image drawInRect:CGRectMake(0,size.height) blendMode:kCGBlendModeNormal alpha:_alpha];
    self.drawImageView.image = UIGraphicsGetImageFromCurrentImageContext();
    self.tempImageView.image = nil;
    UIGraphicsEndImageContext();
}
@H_874_0@

大佬总结

以上是大佬教程为你收集整理的iOS核心图形如何擦除绘图全部内容,希望文章能够帮你解决iOS核心图形如何擦除绘图所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:ios图形如何擦除核心绘图
猜你在找的iOS相关文章
其他相关热搜词更多
phpJavaPython程序员load如何string使用参数jquery开发安装listlinuxiosandroid工具javascriptcap