IOS 画图板-使用UIBezierPath

本文介绍了一个简单的iOS绘图应用实现,使用Swift语言和UIKit框架。文章详细展示了如何通过触摸事件来绘制路径,并提供了清除、撤销及保存画作的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



uiviewcontroller.m

//
//  ViewController.m
//  06-画图
//
//  Created by panba on 16-5-23.
//  Copyright (c) 2016年 panba. All rights reserved.
//

#import "ViewController.h"
#import "JCView.h"
@interface ViewController ()
@property(strong,nonatomic) JCView *myview;

@end

@implementation ViewController
            
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //1-添加view
    JCView *myview = [[JCView alloc]initWithFrame:CGRectMake(0, 100, 320, 320)];
    myview.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:myview];
    self.view.backgroundColor = [UIColor grayColor];
    self.myview = myview;
    //2-添加清屏,回退,保存按钮
    UIButton *clearBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    clearBtn.frame = CGRectMake(20, 60, 50, 44);
    [clearBtn setTitle:@"清屏" forState:UIControlStateNormal];
    [clearBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.view addSubview:clearBtn];
    [clearBtn addTarget:self action:@selector(clearClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    backBtn.frame = CGRectMake(100, 60, 50, 44);
    [backBtn setTitle:@"回退" forState:UIControlStateNormal];
    [backBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.view addSubview:backBtn];
    [backBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    saveBtn.frame = CGRectMake(180, 60, 50, 44);
    [saveBtn setTitle:@"保存" forState:UIControlStateNormal];
    [saveBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.view addSubview:saveBtn];
    [saveBtn addTarget:self action:@selector(saveClick) forControlEvents:UIControlEventTouchUpInside];
}
-(void)clearClick
{
    [self.myview clearClick];
}
-(void)backClick
{
    [self.myview backClick];
}
-(void)saveClick
{
    //1-得到bit上下文
     UIGraphicsBeginImageContext(self.myview.frame.size) ;
    //2-绘图到上下文上
    [self.myview.layer renderInContext:UIGraphicsGetCurrentContext()];
    //3-得到新图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    //4-保存
    UIImageWriteToSavedPhotosAlbum(newImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if (error) {
        NSLog(@"保存失败");
    }
    else
    {
        NSLog(@"保存成功");
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

JCView.h

//
//  JCView.h
//  06-画图
//
//  Created by panba on 16-5-23.
//  Copyright (c) 2016年 panba. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface JCView : UIView
-(void)clearClick;
-(void)backClick;
-(void)saveClick;

@end

JCView.m

//
//  JCView.m
//  06-画图
//
//  Created by panba on 16-5-23.
//  Copyright (c) 2016年 panba. All rights reserved.
//

#import "JCView.h"

@interface JCView ()
@property (nonatomic,strong) NSMutableArray *totalPoint;

@end


@implementation JCView

//懒加载
-(NSMutableArray *)totalPoint
{
    if (_totalPoint == nil) {
        _totalPoint = [NSMutableArray array];
    }
    return _totalPoint;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    //1-得到开始点
    CGPoint startPoint = [touch locationInView:touch.view];
//    //2-创建小数组
//    NSMutableArray *subtarr = [NSMutableArray array];
//    [subtarr addObject:[NSValue valueWithCGPoint:startPoint]];
//    //3-将小数组添加到大数组
//    [self.totalPoint addObject:subtarr];
    //2-创建路径
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:startPoint];
    //3-添加到数组
    [self.totalPoint addObject:path];

}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    //1-得到停止点
    CGPoint movePoint = [touch locationInView:touch.view];
//    //2-得到小数组
//    NSMutableArray *subtarr = [self.totalPoint lastObject];
//    [subtarr addObject:[NSValue valueWithCGPoint:movePoint]];
//    
//    //4-渲染
//    [self setNeedsDisplay];
    //2-得到路径
    UIBezierPath *currpath = [self.totalPoint lastObject];
    [currpath addLineToPoint:movePoint];
    //4-渲染
    [self setNeedsDisplay];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    //1-得到当前点
    CGPoint endPoint = [touch locationInView:touch.view];
//    //2-得到小数组
//    NSMutableArray *subarr = [self.totalPoint lastObject];
//    [subarr addObject:[NSValue valueWithCGPoint:endPoint]];
    //2-得到路径
    UIBezierPath *currpath = [self.totalPoint lastObject];
    [currpath addLineToPoint:endPoint];
    
    //4-渲染
    [self setNeedsDisplay];
    
}


- (void)drawRect:(CGRect)rect
{
    for (UIBezierPath *path in self.totalPoint) {
        [path stroke];
    }
}
-(void)test
{
    // Drawing code
    //1-获取上下文
    CGContextRef ctx =  UIGraphicsGetCurrentContext();
    
    //2-划线
    for (NSMutableArray *subarr in self.totalPoint) {
        for (int index = 0; index <subarr.count; index++) {
            CGPoint point = [subarr[index] CGPointValue];
            if (0 == index) {
                CGContextMoveToPoint(ctx, point.x, point.y);
            }
            else
            {
                CGContextAddLineToPoint(ctx, point.x, point.y);
            }
        }
    }
    //    CGContextMoveToPoint(ctx, <#CGFloat x#>, <#CGFloat y#>);
    //    CGContextAddLineToPoint(ctx, <#CGFloat x#>, <#CGFloat y#>);
    
    //3-渲染
    CGContextStrokePath(ctx);
}
 
-(void)clearClick
{
    [self.totalPoint removeAllObjects];
    [self setNeedsDisplay];
}
-(void)backClick
{
    [self.totalPoint removeLastObject];
    [self setNeedsDisplay];
}
-(void)saveClick
{
   
}

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Y型树杈子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值