码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2014-07-01 09:04:36      阅读:359      评论:0      收藏:0      [点我收藏+]

标签: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;
}


下面为效果图
bubuko.com,布布扣

ios创建画笔的例子(双笔画效果),布布扣,bubuko.com

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

标签:ios创建画笔的程序

原文地址:http://blog.csdn.net/baidu_nod/article/details/36036747

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!