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


    猫猫分享,必须精品

    原创文章,欢迎转载。转载请注明:翟乃玉的博客
    地址: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
    

    其他的可以在源码中自己看,这两个是最重要的。

  • 相关阅读:
    在autolayout中加入每个view的weight
    iOS 拨打电话
    20141211笔记(UIImageView 设置内容的Mode的方法UICollectionViewCell Custom的方法ios modal segue code)
    UILabel总结(转载)
    Error:duplicate files during packaging of APK app/build/output/apk
    《UNIX-Shell编程24学时教程》读书笔记Chap3,4 文件,目录操作
    《UNIX-Shell编程24学时教程》读书笔记Chap1,2 Shell基础,脚本基础
    《UNIX-Shell编程24学时教程》读书笔记chap7 变量
    《软件调试的艺术》读书笔记
    ubuntu环境准备
  • 原文地址:https://www.cnblogs.com/znycat/p/4521016.html
Copyright © 2020-2023  润新知