iOS應用開發:小畫板Demo

來源:互聯網
上載者:User

還有很多功能可以加,記在這裡留著慢慢加吧。

代碼部分

TouchView.h

#import <UIKit/UIKit.h>            @interface TouchView : UIView    {        NSMutableArray *points;        NSArray *points_all;        CGContextRef context;        UIColor *paint_clr;    }    @property (strong,nonatomic) NSMutableArray *points;    @property (strong,nonatomic) NSArray *points_all;    @property (strong,nonatomic) UIColor *paint_clr;            @end

TouchView.m

#import "TouchView.h"            @implementation TouchView    @synthesize points, points_all, paint_clr;            - (id)initWithFrame:(CGRect)frame    {        self = [super initWithFrame:frame];        if (self) {            // Initialization code            paint_clr = [UIColor greenColor];        }        return self;    }            // Only override drawRect: if you perform custom drawing.    // An empty implementation adversely affects performance during animation.    - (void)drawRect:(CGRect)rect    {        // Drawing code        if ((!self.points) || (self.points.count < 2)) {            return;        }                      context = UIGraphicsGetCurrentContext();        //設定畫筆粗細         CGContextSetLineWidth(context, 5.0f);        //設定畫筆顏色        //[[UIColor blueColor]set ];        // [paint_clr set];        //CGContextSetStrokeColorWithColor(context, [[UIColor blueColor]CGColor]);        CGContextSetStrokeColorWithColor(context, [paint_clr CGColor]);                    //畫以前的軌跡        for (int j = 0 ; j < [self.points_all count]; j++) {            NSMutableArray *points_tmp = [points_all objectAtIndex:j];                                    for (int i = 0;i < [points_tmp count]-1;i++)                {                    CGPoint point1 = [[points_tmp objectAtIndex:i] CGPointValue];                    CGPoint point2 = [[points_tmp objectAtIndex:(i+1)] CGPointValue];                    CGContextMoveToPoint(context, point1.x, point1.y);                    CGContextAddLineToPoint(context, point2.x, point2.y);                    CGContextStrokePath(context);                }            }                    //畫這次        for (int i=0; i < [self.points count]-1; i++) {            CGPoint point1 = [[self.points objectAtIndex:i] CGPointValue];            CGPoint point2 = [[self.points objectAtIndex:(i+1)] CGPointValue];            CGContextMoveToPoint(context, point1.x, point1.y);            CGContextAddLineToPoint(context, point2.x, point2.y);            CGContextStrokePath(context);        }        }            //不支援多點觸摸    - (BOOL) isMultipleTouchEnabled    {        return NO;    }            //建立一個array,並且記錄初始ponit    - (void) touchesBegan:(NSSet *) touches withEvent:(UIEvent *) event{        self.points = [NSMutableArray array];        CGPoint pt = [[touches anyObject] locationInView:self];        [self.points addObject:[NSValue valueWithCGPoint:pt]];    }            //移動過程中記錄這些points    //調用setNeedsDisplay,會觸發drawRect方法的調用    - (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{        CGPoint pt = [[touches anyObject] locationInView:self];        [self.points addObject:[NSValue valueWithCGPoint:pt]];        [self setNeedsDisplay];    }            - (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{        NSMutableArray *points_tmp = [[NSMutableArray alloc] initWithArray:self.points];        if (self.points_all == nil) {            self.points_all = [[NSArray alloc] initWithObjects:points_tmp, nil];        }else {            self.points_all = [self.points_all arrayByAddingObject:points_tmp];        }    }    @end

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.