您的位置:首页 > 产品设计 > UI/UE

(素材源码)猫猫学IOS(三十四)UI之Quartz2D画画板的实现

2015-05-06 11:07 681 查看
猫猫分享,必须精品

原创文章,欢迎转载。转载请注明:翟乃玉的博客

地址:http://blog.csdn.net/u013357243?viewmode=contents

源码:http://download.csdn.net/detail/u013357243/8666923

效果:



代码:

NYView

NYView.h

//
//  NYView.h
//  画画板
//
//  Created by apple on 15-5-6.
//  Copyright (c) 2015年 znycat. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface NYView : UIView

- (void)clearView;
- (void)backView;
@end


NYView.m

//
//  NYView.m
//  画画板
//
//  Created by apple on 15-5-6.
//  Copyright (c) 2015年 znycat. All rights reserved.
//

#import "NYView.h"

@interface NYView ()

@property (nonatomic, strong) NSMutableArray *paths;

@end

@implementation NYView

- (NSMutableArray *)paths
{
if (_paths == nil) {
_paths = [NSMutableArray array];
}
return _paths;
}

// 开始触摸
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

// 1.获取手指对应UITouch对象
UITouch *touch = [touches anyObject];
// 2.通过UITouch对象获取手指触摸的位置
CGPoint startPoint = [touch locationInView:touch.view];

// 3.当用户手指按下的时候创建一条路径
UIBezierPath *path = [UIBezierPath bezierPath];
// 3.1设置路径的相关属性
[path setLineJoinStyle:kCGLineJoinRound];
[path setLineCapStyle:kCGLineCapRound];
[path setLineWidth:10];

// 4.设置当前路径的起点
[path moveToPoint:startPoint];
// 5.将路径添加到数组中
[self.paths addObject:path];

}
// 移动
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// 1.获取手指对应UITouch对象
UITouch *touch = [touches anyObject];
// 2.通过UITouch对象获取手指触摸的位置
CGPoint movePoint = [touch locationInView:touch.view];

// 3.取出当前的path
UIBezierPath *currentPaht = [self.paths lastObject];
// 4.设置当前路径的终点
[currentPaht addLineToPoint:movePoint];

// 6.调用drawRect方法重回视图
[self setNeedsDisplay];

}

// 离开view(停止触摸)
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

[self touchesMoved:touches withEvent:event];
/*
// 1.获取手指对应UITouch对象
UITouch *touch = [touches anyObject];
// 2.通过UITouch对象获取手指触摸的位置
CGPoint endPoint = [touch locationInView:touch.view];

// 3.取出当前的path
UIBezierPath *currentPaht = [self.paths lastObject];
// 4.设置当前路径的终点
[currentPaht addLineToPoint:endPoint];

// 6.调用drawRect方法重回视图
[self setNeedsDisplay];
*/

}

// 画线
- (void)drawRect:(CGRect)rect
{

[[UIColor redColor] set];
// 边路数组绘制所有的线段
for (UIBezierPath *path in self.paths) {
[path stroke];
}

}

- (void)clearView
{
[self.paths removeAllObjects];
[self setNeedsDisplay];
}
- (void)backView
{
[self.paths removeLastObject];
[self setNeedsDisplay];
}

@end


NYViewController

//
//  NYViewController.m
//  画画板
//
//  Created by apple on 15-5-6.
//  Copyright (c) 2015年 znycat. All rights reserved.
//

#import "NYViewController.h"
#import "NYView.h"
#import "MBProgressHUD+NJ.h"
#import "UIImage+captureView.h"

@interface NYViewController ()
/**
*  清屏
*/
- (IBAction)clearBtnClick;
/**
*  回退
*/
- (IBAction)backBtnClick;
/**
*  保存
*/
- (IBAction)saveBtnClick;

@property (weak, nonatomic) IBOutlet NYView *customView;
@end

@implementation NYViewController

- (IBAction)clearBtnClick {
[self.customView clearView];
}

- (IBAction)backBtnClick {

[self.customView backView];
}

- (IBAction)saveBtnClick {

UIImage *newImage = [UIImage captureImageWithView:self.customView];
// 4.保存到相册
UIImageWriteToSavedPhotosAlbum(newImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
if (error) {
[MBProgressHUD showError:@"保存失败"];
}else
{
[MBProgressHUD showSuccess:@"保存成功"];
}
}

@end


其他的可以在源码中自己看,这两个是最重要的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐