iOS应用开发——小画板Demo

    还有很多功能可以加,记在这里留着慢慢加吧。


代码部分

TouchView.h

  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface TouchView : UIView  
  4. {  
  5.     NSMutableArray *points;  
  6.     NSArray *points_all;  
  7.     CGContextRef context;  
  8.     UIColor *paint_clr;  
  9. }  
  10. @property (strong,nonatomic) NSMutableArray *points;  
  11. @property (strong,nonatomic) NSArray *points_all;  
  12. @property (strong,nonatomic) UIColor *paint_clr;  
  13.   
  14. @end  


TouchView.m

  1. #import "TouchView.h"  
  2.   
  3. @implementation TouchView  
  4. @synthesize points, points_all, paint_clr;  
  5.   
  6. - (id)initWithFrame:(CGRect)frame  
  7. {  
  8.     self = [super initWithFrame:frame];  
  9.     if (self) {  
  10.         // Initialization code  
  11.         paint_clr = [UIColor greenColor];  
  12.     }  
  13.     return self;  
  14. }  
  15.   
  16. // Only override drawRect: if you perform custom drawing.  
  17. // An empty implementation adversely affects performance during animation.  
  18. - (void)drawRect:(CGRect)rect  
  19. {  
  20.     // Drawing code  
  21.     if ((!self.points) || (self.points.count < 2)) {  
  22.         return;  
  23.     }  
  24.         
  25.     context = UIGraphicsGetCurrentContext();  
  26.     //设置画笔粗细   
  27.     CGContextSetLineWidth(context, 5.0f);  
  28.     //设置画笔颜色  
  29.     //[[UIColor blueColor]set ];  
  30.     // [paint_clr set];  
  31.     //CGContextSetStrokeColorWithColor(context, [[UIColor blueColor]CGColor]);  
  32.     CGContextSetStrokeColorWithColor(context, [paint_clr CGColor]);  
  33.       
  34.     //画以前的轨迹  
  35.     for (int j = 0 ; j < [self.points_all count]; j++) {  
  36.         NSMutableArray *points_tmp = [points_all objectAtIndex:j];  
  37.               
  38.             for (int i = 0;i < [points_tmp count]-1;i++)  
  39.             {  
  40.                 CGPoint point1 = [[points_tmp objectAtIndex:i] CGPointValue];  
  41.                 CGPoint point2 = [[points_tmp objectAtIndex:(i+1)] CGPointValue];  
  42.                 CGContextMoveToPoint(context, point1.x, point1.y);  
  43.                 CGContextAddLineToPoint(context, point2.x, point2.y);  
  44.                 CGContextStrokePath(context);  
  45.             }  
  46.         }  
  47.       
  48.     //画这次  
  49.     for (int i=0; i < [self.points count]-1; i++) {  
  50.         CGPoint point1 = [[self.points objectAtIndex:i] CGPointValue];  
  51.         CGPoint point2 = [[self.points objectAtIndex:(i+1)] CGPointValue];  
  52.         CGContextMoveToPoint(context, point1.x, point1.y);  
  53.         CGContextAddLineToPoint(context, point2.x, point2.y);  
  54.         CGContextStrokePath(context);  
  55.     }      
  56. }  
  57.   
  58. //不支持多点触摸  
  59. - (BOOL) isMultipleTouchEnabled  
  60. {  
  61.     return NO;  
  62. }  
  63.   
  64. //创建一个array,并且记录初始ponit  
  65. - (void) touchesBegan:(NSSet *) touches withEvent:(UIEvent *) event  
  66. {  
  67.     self.points = [NSMutableArray array];  
  68.     CGPoint pt = [[touches anyObject] locationInView:self];  
  69.     [self.points addObject:[NSValue valueWithCGPoint:pt]];  
  70. }  
  71.   
  72. //移动过程中记录这些points  
  73. //调用setNeedsDisplay,会触发drawRect方法的调用  
  74. - (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event  
  75. {  
  76.     CGPoint pt = [[touches anyObject] locationInView:self];  
  77.     [self.points addObject:[NSValue valueWithCGPoint:pt]];  
  78.     [self setNeedsDisplay];  
  79. }  
  80.   
  81. - (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event  
  82. {  
  83.     NSMutableArray *points_tmp = [[NSMutableArray alloc] initWithArray:self.points];  
  84.     if (self.points_all == nil) {  
  85.         self.points_all = [[NSArray alloc] initWithObjects:points_tmp, nil];  
  86.     }else {  
  87.         self.points_all = [self.points_all arrayByAddingObject:points_tmp];  
  88.     }  
  89. }  
  90. @end  


ViewController.h

  1. #import <UIKit/UIKit.h>  
  2.   
  3. @class TouchView;  
  4. @interface ViewController : UIViewController  
  5. {  
  6.     TouchView *tv;  
  7. }  
  8. @end  


ViewController.m

  1. #import "ViewController.h"  
  2. #import "TouchView.h"  
  3.   
  4. @interface ViewController ()  
  5.   
  6. @end  
  7.   
  8. @implementation ViewController  
  9.   
  10. - (void)viewDidLoad  
  11. {  
  12.     [super viewDidLoad];  
  13.     // Do any additional setup after loading the view, typically from a nib.  
  14.     self.view.userInteractionEnabled = YES;  
  15.       
  16.   // TouchView *tv = [[TouchView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 260.0f, 260.0f)];  
  17.     tv = [[TouchView alloc]initWithFrame:self.view.frame];  
  18.     tv.backgroundColor = [UIColor blackColor];  
  19.       
  20.     [self.view addSubview:tv];  
  21.       
  22.     UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:[@"White Red Blue Green Yellow" componentsSeparatedByString:@" "]];  
  23.     seg.segmentedControlStyle = UISegmentedControlSegmentCenter;  
  24.     seg.tintColor = [UIColor blackColor];   
  25.     seg.center = CGPointMake(self.view.center.x, (self.view.bounds.size.height - seg.bounds.size.height));   
  26.     [self.view addSubview:seg];  
  27.       
  28.     [seg addTarget:self action:@selector(colorChange:) forControlEvents:UIControlEventValueChanged];  
  29. }  
  30.   
  31. - (void)viewDidUnload  
  32. {  
  33.     [super viewDidUnload];  
  34.     // Release any retained subviews of the main view.  
  35. }  
  36.   
  37. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  38. {  
  39.     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);  
  40. }  
  41.   
  42. - (void) colorChange: (UISegmentedControl *) seg  
  43. {  
  44.     switch ([seg selectedSegmentIndex])  
  45.     {  
  46.         case 0:   
  47.             tv.paint_clr = [UIColor whiteColor];  
  48.             break;  
  49.         case 1:  
  50.             tv.paint_clr = [UIColor redColor];  
  51.             break;  
  52.         case 2:  
  53.             tv.paint_clr = [UIColor blueColor];  
  54.             break;  
  55.         case 3:  
  56.             tv.paint_clr = [UIColor greenColor];  
  57.             break;  
  58.         case 4:  
  59.             tv.paint_clr = [UIColor yellowColor];  
  60.             break;  
  61.         default:  
  62.               
  63.             break;  
  64.     }  
  65. }  
  66.   
  67. @end  


效果图






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值