我是靠谱客的博主 隐形期待,最近开发中收集的这篇文章主要介绍iOS OC 涂鸦、画板,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述


1.属性

#import "drawView.h"
#define SCREEN_SIZE self.frame.size
@interface drawView()<UIGestureRecognizerDelegate>
//每次触摸结束前经过的点用来连成线
@property (nonatomic,strong) NSMutableArray *pointArray;
//保存线条的数组
@property (nonatomic,strong) NSMutableArray *arrayLine;
@property (nonatomic,strong) UISlider *sliderWidth;
@property (nonatomic,strong) UIImageView *testImage;
@property (nonatomic,strong) UIColor *blackColor;//画笔默认颜色
@property (nonatomic,strong) UIButton *btnCancelDraw;//撤销画笔
@property (nonatomic, strong) UIButton *btnChangeColor;//改变颜色
@end

2.初始化

- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self addSubview:self.sliderWidth];
_blackColor = [UIColor blackColor];
self.backgroundColor = [UIColor clearColor];
[self addSubview:self.btnChangeColor];
//
[self addSubview:self.testImage];
[self addSubview:self.btnCancelDraw];
}
return self;
}

3.手势

#pragma mark --添加图片,设置手势
- (UIImageView *)testImage
{
if(!_testImage)
{
_testImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"avatar01"]];
_testImage.frame = CGRectMake(80, 250, 200, 300);
_testImage.userInteractionEnabled = YES;
//添加拖动shous
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(gestureHandler:)];
//旋转手势
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGestureHanlder:)];
//缩放手势
UIPinchGestureRecognizer *pingGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchGestureHanlder:)];
//设置手势代理
[panGesture setDelegate:self];
[rotationGesture setDelegate:self];
[pingGesture setDelegate:self];
//添加手势
[_testImage addGestureRecognizer:pingGesture];
[_testImage addGestureRecognizer:rotationGesture];
[_testImage addGestureRecognizer:panGesture];
}
return _testImage;
}
#pragma mark -- 设置多个手势并存
- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
#pragma mark -- 手势事件
- (void) gestureHandler:(UIPanGestureRecognizer *)sender
{
CGPoint translation = [sender translationInView:self];
sender.view.center = CGPointMake(sender.view.center.x+translation.x, sender.view.center.y+translation.y);
[sender setTranslation:CGPointZero inView:self];
}
- (void) rotationGestureHanlder:(UIRotationGestureRecognizer *) sender
{
sender.view.transform = CGAffineTransformRotate(sender.view.transform, sender.rotation);
sender.rotation = 0;
}
- (void) pinchGestureHanlder:(UIPinchGestureRecognizer *) sender
{
sender.view.transform = CGAffineTransformScale(sender.view.transform, sender.scale, sender.scale);
sender.scale = 1;
}

4.按钮点击事件

#pragma mark --点击事件----随机改变颜色
- (void) btnClicked
{
_blackColor = [UIColor colorWithRed:(float)rand()/RAND_MAX green:(float)rand()/RAND_MAX blue:(float)rand()/RAND_MAX alpha:1.0];
[self setNeedsDisplay];
}
- (void) btnCanCelDrawClicked
{
[self.arrayLine removeObjectAtIndex:self.arrayLine.count-1];//注意清除画笔这里数组超出范围会导致崩溃
[self setNeedsDisplay];
}

5.图形绘制

#pragma mark --重新绘制
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, self.sliderWidth.value);
CGContextSetLineJoin(context, kCGLineJoinRound);//设置拐角样式
CGContextSetLineCap(context, kCGLineCapRound);//设置线头样式
if(self.arrayLine.count>0)
{
//将里面的线条画出来
for(int i = 0;i<self.arrayLine.count;i++)
{
NSArray *array = [NSArray arrayWithArray:self.arrayLine[i]];
if(array.count>0)
{
CGPoint myStartPoint = CGPointFromString(array[0]);
//将画笔移动到指定的点
CGContextMoveToPoint(context, myStartPoint.x, myStartPoint.y);
for(int j = 1;j<array.count;j++)
{
CGPoint myEndPoint = CGPointFromString(array[j]);
CGContextAddLineToPoint(context, myEndPoint.x, myEndPoint.y);
}
[_blackColor setStroke];
CGContextStrokePath(context);
}
}
}
if(self.pointArray.count>0)
{
//划线
CGPoint startPoint = CGPointFromString(self.pointArray[0]);
CGContextMoveToPoint(context, startPoint.x, startPoint.y);
for(int i = 1;i<self.pointArray.count;i++)
{
CGPoint tempPoint = CGPointFromString(self.pointArray[i]);
CGContextAddLineToPoint(context, tempPoint.x, tempPoint.y);
}
[_blackColor setStroke];
CGContextStrokePath(context);
}
}
//画笔触摸的所有点
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
//去除每一个点
CGPoint myBeginPoint = [touch locationInView:self];
NSString *strPoint = NSStringFromCGPoint(myBeginPoint);
[self.pointArray addObject:strPoint];
[self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self addArray];
}

6.Getter

#pragma mark --Getter
- (UIButton *)btnCancelDraw
{
if(!_btnCancelDraw)
{
_btnCancelDraw = [UIButton buttonWithType:UIButtonTypeCustom];
_btnCancelDraw.frame = CGRectMake(80,120, 50, 50);
_btnCancelDraw.layer.cornerRadius = 25;
_btnCancelDraw.backgroundColor = [UIColor blueColor];
[_btnCancelDraw setTitle:@"撤销" forState:UIControlStateNormal];
[_btnCancelDraw addTarget:self action:@selector(btnCanCelDrawClicked) forControlEvents:UIControlEventTouchUpInside];
}
return _btnCancelDraw;
}
- (UIButton *)btnChangeColor
{
if(!_btnChangeColor)
{
_btnChangeColor = [UIButton buttonWithType:UIButtonTypeCustom];
_btnChangeColor.frame = CGRectMake(20, 120, 50, 50);
_btnChangeColor.layer.cornerRadius = 25;
_btnChangeColor.alpha = 0.5;
_btnChangeColor.backgroundColor = [UIColor redColor];
[_btnChangeColor setTitle:@"改变" forState:UIControlStateNormal];
[_btnChangeColor addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
}
return _btnChangeColor;
}
- (UISlider *)sliderWidth
{
if(!_sliderWidth)
{
_sliderWidth = [[UISlider alloc]init];
_sliderWidth.maximumValue = 50;
_sliderWidth.minimumValue = 1;
_sliderWidth.frame = CGRectMake(10, 80, [UIScreen mainScreen].bounds.size.width-20, 30);
[_sliderWidth addTarget:self action:@selector(sliderValueChange:) forControlEvents:UIControlEventValueChanged];
_sliderWidth.tag = 1000;
}
return _sliderWidth;
}
- (void) sliderValueChange:(UISlider *)sender
{
if(sender.tag == 1000)
{
[self setNeedsDisplay];
}
}
- (NSMutableArray *)arrayLine
{
if(!_arrayLine)
{
_arrayLine = [NSMutableArray array];
}
return _arrayLine;
}
- (NSMutableArray *)pointArray
{
if(!_pointArray)
{
_pointArray = [NSMutableArray array];
}
return _pointArray;
}
- (void) addArray
{
if(self.pointArray!=nil)
{
[self.arrayLine addObject:self.pointArray];//将点得数组放入存到线的数组
}
self.pointArray = [NSMutableArray array];//将点数组清空
}


最后

以上就是隐形期待为你收集整理的iOS OC 涂鸦、画板的全部内容,希望文章能够帮你解决iOS OC 涂鸦、画板所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部