ios建立畫筆的例子(雙筆畫效果)

來源:互聯網
上載者:User

定義一個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;}


下面為

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.