ios创建画笔的样例(双笔画效果)

简介:

定义一个UIView:主要是在这个View里面加一个UIImageView,绘图都在这个UIImageView里面进行

@property(nonatomic) CGPoint prePoint;  //手指在进入move事件之前的那个点
@property(nonatomic) CGPoint oppsitePoint;  //手指在进入move事件之前的那个点
@property(nonatomic, retain) UIImageView* drawImage;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.drawImage = [[UIImageView alloc] initWithImage:nil];
        self.drawImage.frame = self.frame;
        [self addSubview:_drawImage];
        
    }
    return self;
}

然后处理手指的事件

#pragma mark - deal touch
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"touchesBegan");
    //以下两句知道手指在屏幕上的点的信息
    UITouch* touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    
    if (touch) {
        self.prePoint = point;
        _oppsitePoint = point;
        _oppsitePoint.x = 320.0f - point.x;
    }
}

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
     NSLog(@"touchesMoved");
    UITouch* touch = [touches anyObject];
    if (touch) {
        CGPoint point = [touch locationInView:self];
        UIGraphicsBeginImageContext(self.frame.size);
        [_drawImage.image drawInRect:CGRectMake(0, 0, _drawImage.frame.size.width, _drawImage.frame
                                                .size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2.0f);
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),0.314, 0.486, 0.859, 1.0);
        CGContextBeginPath(UIGraphicsGetCurrentContext());
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), _prePoint.x, _prePoint.y);
         CGContextAddQuadCurveToPoint(UIGraphicsGetCurrentContext(), _prePoint.x, _prePoint.y, point.x, point.y);
        
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), _oppsitePoint.x, _oppsitePoint.y);
        CGContextAddQuadCurveToPoint(UIGraphicsGetCurrentContext(), _oppsitePoint.x, _oppsitePoint.y, 320.0f - point.x, point.y);
        
       // CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), point.x, point.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        _drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        _prePoint = point;
        _oppsitePoint = point;
        _oppsitePoint.x = 320.0f - point.x;
        
        
    }
    
}


-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"touchesEnded");
    
    //这段的作用是假设在屏幕上点击能够画出点
    UIGraphicsBeginImageContext(self.frame.size);
    [_drawImage.image drawInRect:CGRectMake(0, 0, _drawImage.frame.size.width, _drawImage.frame
                                            .size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 3.0f);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),0.314, 0.486, 0.859, 1.0);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), _prePoint.x, _prePoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), _prePoint.x, _prePoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    _drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    self.isTouch = NO;
}


以下为效果图








本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5174654.html,如需转载请自行联系原作者

相关文章
|
存储 数据安全/隐私保护 开发者
iOS创建苹果证书、制作p12证书流程
iOS创建苹果证书、制作p12证书流程
iOS创建苹果证书、制作p12证书流程
|
安全 数据安全/隐私保护 iOS开发
uniapp开发,window下创建ios打包证书的详情流程
uniapp开发,window下创建ios打包证书的详情流程
|
前端开发 iOS开发
React Native | 创建IOS原生模块
React Native | 创建IOS原生模块
323 0
|
iOS开发
IOS创建二维码
IOS创建二维码
64 0
|
安全 数据安全/隐私保护 iOS开发
uniapp开发,window下创建ios打包证书的详情流程
uniapp开发,window下创建ios打包证书的详情流程
|
存储 数据安全/隐私保护 开发者
iOS创建苹果证书、p12证书申请流程
iOS创建苹果证书、p12证书申请流程
iOS创建苹果证书、p12证书申请流程
|
程序员 开发者 iOS开发
iOS开发:苹果开发者账号第一次新建APP ID以及创建App的步骤
在iOS开发过程中,关于苹果开发者账号相关操作的知识点也是不可忽视的,尤其是对于一些刚接触苹果开发的开发者来说,更应该多了解一些关于苹果开发者账号的知识点,这样有利于App的测试和上架。
949 0
iOS开发:苹果开发者账号第一次新建APP ID以及创建App的步骤
|
iOS开发 索引 Perl
iOS 开发创建CocoaPods公有库以及subspec分支功能(下)
iOS 开发创建CocoaPods公有库以及subspec分支功能
373 0
iOS 开发创建CocoaPods公有库以及subspec分支功能(下)
|
开发框架 开发工具 数据安全/隐私保护
iOS 开发创建CocoaPods公有库以及subspec分支功能(上)
iOS 开发创建CocoaPods公有库以及subspec分支功能
333 0
iOS 开发创建CocoaPods公有库以及subspec分支功能(上)
|
开发工具 开发者 iOS开发
使用香蕉云编创建ios打包证书的教程
使用mac创建ios证书,需要购买mac电脑,成本非常高,因此我这里教会大家使用香蕉云编来创建ios打包证书
2545 1
使用香蕉云编创建ios打包证书的教程