最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

IOS實(shí)現(xiàn)的簡(jiǎn)單畫板功能

 更新時(shí)間:2017年04月01日 09:46:43   作者:這酸爽!  
本文主要介紹了IOS實(shí)現(xiàn)簡(jiǎn)單畫板的示例代碼。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧

效果圖

設(shè)計(jì)要求

 1、畫筆能設(shè)置大小、顏色

 2、有清屏、撤銷、橡皮擦、導(dǎo)入照片功能

 3、能將繪好的畫面保存到相冊(cè)

實(shí)現(xiàn)思路

1、畫筆的實(shí)現(xiàn),我們可以通過監(jiān)聽用戶的 平移手勢(shì) 中創(chuàng)建 UIBezierPath 來(lái)實(shí)現(xiàn)線條的繪制

2、撤銷功能,我們先來(lái)看下撤銷功能,我們會(huì)想到用一個(gè)數(shù)組隊(duì)列將用戶的每一次的筆畫都加入到數(shù)組中,然后撤銷的時(shí)候只需要將最后添加進(jìn)去的筆畫pop掉,重新繪制就可以了

3、清屏功能就簡(jiǎn)單了,只需要將上面說(shuō)到的那個(gè)數(shù)組清空重新繪制下就可以了

4、導(dǎo)入照片功能,可以用系統(tǒng)的 UIImagePickerController 選取照片得到UIImage,然后再將 UIImage 繪制到屏幕中就可以了

5、保存到相冊(cè)功能,可以使用 UIGraphicsGetImageFromCurrentImageContext 獲取當(dāng)前的圖片上下文,得到屏幕畫面的 UIImage ,然后通過 UIImageWriteToSavedPhotosAlbum 寫入到相冊(cè)

具體代碼實(shí)現(xiàn)

1、先畫個(gè)界面

2、因?yàn)槲覀兝L制線條會(huì)用到 UIBezierPath ,并且要能可設(shè)置顏色,但是UIBezierPath是沒有設(shè)置顏色的屬性,所以我們這里需要簡(jiǎn)單擴(kuò)展一下,創(chuàng)建一個(gè)繼承于 UIBezierPath 的子類 DrawPath

//
// DrawPath.h
// 畫板
//
// Created by xgao on 16/4/13.
// Copyright © 2016年 xgao. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface DrawPath : UIBezierPath
// 畫筆顏色
@property(nonatomic,retain)UIColor* pathColor;
@end

這個(gè)子類只需要擴(kuò)展一個(gè)屬性,就是 pathColor 用來(lái)保存畫筆的顏色

//
// DrawPath.m
// 畫板
//
// Created by xgao on 16/4/13.
// Copyright © 2016年 xgao. All rights reserved.
//
#import "DrawPath.h"
@implementation DrawPath
@end

DrawPath.m 里面不需要做其它功能

3、接到來(lái)我們對(duì)畫板功能的實(shí)現(xiàn)封裝一下,創(chuàng)建一個(gè)繼承于UIView的 DrawView子類

//
// DrawView.h
// 畫板
//
// Created by xgao on 16/4/13.
// Copyright © 2016年 xgao. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface DrawView : UIView

// 畫線的寬度
@property(nonatomic,assign)CGFloat lineWidth;

// 線條顏色
@property(nonatomic,retain)UIColor* pathColor;

// 加載背景圖片
@property(nonatomic,strong)UIImage* image;

// 清屏
- (void)clear;

// 撤銷
- (void)undo;

// 橡皮擦
- (void)eraser;

// 保存
- (void)save;
@end
//
// DrawView.m
// 畫板
//
// Created by xgao on 16/4/13.
// Copyright © 2016年 xgao. All rights reserved.
//

#import "DrawView.h"
#import "DrawPath.h"

@interface DrawView()

@property(nonatomic,strong) DrawPath* path;

// 線的數(shù)組
@property(nonatomic,strong) NSMutableArray* paths;

@end

@implementation DrawView

- (void)awakeFromNib{

 [self setUp];
}

- (instancetype)initWithFrame:(CGRect)frame
{
 self = [super initWithFrame:frame];
 if (self) {
 [self setUp];
 }
 return self;
}

// 重繪UI
- (void)drawRect:(CGRect)rect {

 for (DrawPath* path in self.paths) {

 if ([path isKindOfClass:[UIImage class]]) {
  // 畫圖片
  UIImage* image = (UIImage*)path;
  [image drawInRect:rect];
 }else{
  // 畫線

  // 設(shè)置畫筆顏色
  [path.pathColor set];

  // 繪制
  [path stroke];
 }
 }
}

// 懶加載屬性
- (NSMutableArray*)paths{

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

// 重寫image屬性
- (void)setImage:(UIImage *)image{

 _image = image;

 // 將圖片加入到線條數(shù)組中
 [self.paths addObject:image];

 [self setNeedsDisplay];
}

#pragma mark - Init

// 初始化
- (void)setUp{

 // 添加平移手勢(shì)
 UIPanGestureRecognizer* panGes = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGes:)];
 [self addGestureRecognizer:panGes];

 // 默認(rèn)值
 self.lineWidth = 1;
 self.pathColor = [UIColor blackColor];
}

#pragma mark - Event

// 平移事件
- (void)panGes:(UIPanGestureRecognizer*)ges{

 // 獲取當(dāng)前點(diǎn)
 CGPoint curPoint = [ges locationInView:self];

 if (ges.state == UIGestureRecognizerStateBegan) { // 開始移動(dòng)

 // 創(chuàng)建貝塞爾曲線
 _path = [[DrawPath alloc]init];

 // 設(shè)置線條寬度
 _path.lineWidth = _lineWidth;

 // 線條默認(rèn)顏色
 _path.pathColor = _pathColor;

 // 設(shè)置起始點(diǎn)
 [_path moveToPoint:curPoint];

 [self.paths addObject:_path];
 }

 // 連線
 [_path addLineToPoint:curPoint];

 // 重繪
 [self setNeedsDisplay];
}

#pragma mark - Method

// 清屏
- (void)clear{

 [self.paths removeAllObjects];

 [self setNeedsDisplay];
}

// 撤銷
- (void)undo{

 [self.paths removeLastObject];

 [self setNeedsDisplay];
}

// 橡皮擦
- (void)eraser{

 self.pathColor = [UIColor whiteColor];

 [self setNeedsDisplay];
}

// 保存
- (void)save{

 // ---- 截圖操作
 // 開啟上下文
 UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, 0);

 // 獲取當(dāng)前上下文
 CGContextRef context = UIGraphicsGetCurrentContext();

 // 渲染圖層到上下文
 [self.layer renderInContext:context];

 // 從上下文中獲取圖片
 UIImage* image = UIGraphicsGetImageFromCurrentImageContext();

 // 關(guān)閉上下文
 UIGraphicsEndImageContext();

 // ---- 保存圖片
 UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

}

// 圖片保存方法,必需寫這個(gè)方法體,不能會(huì)保存不了圖片
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{

 // 提示
 UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"保存成功" message:nil delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
 [alert show];
}
@end

4、接下來(lái)就是如果使用這個(gè)畫板類了,直接上代碼吧

//
// ViewController.m
// 畫板
//
// Created by xgao on 16/4/13.
// Copyright © 2016年 xgao. All rights reserved.
//

#import "ViewController.h"
#import "DrawView.h"

@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

// 畫板
@property (weak, nonatomic) IBOutlet DrawView *drawView;

@end

@implementation ViewController

- (void)viewDidLoad {
 [super viewDidLoad];

}

#pragma mark - Event

// 線條寬度變化
- (IBAction)lineWidthChange:(UISlider*)sender {

 _drawView.lineWidth = sender.value;
}

// 線條顏色變化
- (IBAction)pathColorChange:(UIButton*)sender {

 _drawView.pathColor = sender.backgroundColor;
}

// 清屏
- (IBAction)clearAction:(id)sender {

 [_drawView clear];
}

// 撤銷
- (IBAction)undoAction:(id)sender {

 [_drawView undo];
}

// 橡皮擦
- (IBAction)eraserAction:(id)sender {

 [_drawView eraser];
}

// 照片
- (IBAction)pickerPhotoAction:(id)sender {

 // 照片選擇控制器
 UIImagePickerController* picVC = [[UIImagePickerController alloc]init];
 // 照片源
 picVC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
 // 委托
 picVC.delegate = self;

 [self presentViewController:picVC animated:YES completion:nil];
}

// 保存
- (IBAction)saveAction:(id)sender {

 [_drawView save];
}

#pragma mark - UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo{

 // 設(shè)置圖片
 _drawView.image = image;

 // 關(guān)閉窗口
 [self dismissViewControllerAnimated:YES completion:nil];
}
@end

到這里就差不多了,這個(gè)小功能實(shí)現(xiàn)的基本思路與具體代碼我都已經(jīng)放上來(lái)了,大家如果還有哪里不清楚的可以留言喔~~

以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!

相關(guān)文章

  • iOS 沙盒圖片保存讀取實(shí)例

    iOS 沙盒圖片保存讀取實(shí)例

    下面小編就為大家分享一篇iOS 沙盒圖片保存讀取實(shí)例,具有很的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧
    2017-12-12
  • iOS實(shí)現(xiàn)簡(jiǎn)易的導(dǎo)航欄顏色漸變實(shí)例代碼

    iOS實(shí)現(xiàn)簡(jiǎn)易的導(dǎo)航欄顏色漸變實(shí)例代碼

    很多APP 都有導(dǎo)航欄顏色漸變的效果,下面這篇文章主要給大家介紹了關(guān)于iOS如何實(shí)現(xiàn)簡(jiǎn)易的導(dǎo)航欄顏色漸變效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧
    2018-10-10
  • iOS模擬中獎(jiǎng)名單循環(huán)滾動(dòng)效果

    iOS模擬中獎(jiǎng)名單循環(huán)滾動(dòng)效果

    這篇文章主要為大家詳細(xì)介紹了iOS模擬中獎(jiǎng)名單循環(huán)滾動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • IOS 開發(fā)之自定義按鈕實(shí)現(xiàn)文字圖片位置隨意定制

    IOS 開發(fā)之自定義按鈕實(shí)現(xiàn)文字圖片位置隨意定制

    這篇文章主要介紹了IOS 開發(fā)之自定義按鈕實(shí)現(xiàn)文字圖片位置隨意定制的相關(guān)資料,這里附有實(shí)例代碼及實(shí)現(xiàn)效果圖,需要的朋友可以參考下
    2016-12-12
  • iOS如何自定義控制器轉(zhuǎn)場(chǎng)動(dòng)畫push詳解

    iOS如何自定義控制器轉(zhuǎn)場(chǎng)動(dòng)畫push詳解

    在平時(shí)開發(fā)中,有時(shí)候需要一些轉(zhuǎn)場(chǎng)動(dòng)畫給界面調(diào)整增添一些活力,而實(shí)現(xiàn)這些動(dòng)畫相對(duì)比較繁瑣。下面這篇文章主要給大家介紹了關(guān)于iOS如何自定義控制器轉(zhuǎn)場(chǎng)動(dòng)畫push的相關(guān)資料,需要的朋友可以參考下。
    2017-12-12
  • IOS代碼筆記UIView的placeholder的效果

    IOS代碼筆記UIView的placeholder的效果

    這篇文章主要為大家詳細(xì)介紹了IOS實(shí)現(xiàn)placeholder效果的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-07-07
  • iOS開發(fā)仿電商類APP首頁(yè)實(shí)例

    iOS開發(fā)仿電商類APP首頁(yè)實(shí)例

    本篇文章主要介紹了iOS開發(fā)仿電商類APP首頁(yè)實(shí)例,主要是利用ui布局,具有一定的參考價(jià)值,有需要的可以了解一下。
    2016-11-11
  • iOS 11 safeArea詳解及iphoneX 適配

    iOS 11 safeArea詳解及iphoneX 適配

    本篇文章主要介紹了iOS 11 safeArea詳解及iphoneX 適配,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧
    2018-02-02
  • iOS中使用JSPatch框架使Objective-C與JavaScript代碼交互

    iOS中使用JSPatch框架使Objective-C與JavaScript代碼交互

    有了JSPatch,我們便可以在iOS App開發(fā)中令JavaScript代碼調(diào)用原生的Objective-C屬性和方法等,下面就來(lái)詳細(xì)看一下如何在iOS中使用JSPatch框架使Objective-C與JavaScript代碼交互
    2016-06-06
  • Objective-C中block循環(huán)引用問題詳解

    Objective-C中block循環(huán)引用問題詳解

    這篇文章主要給大家介紹了關(guān)于Objective-C中block循環(huán)引用問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Objective-C具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06

最新評(píng)論

大同市| 毕节市| 阿勒泰市| 简阳市| 威信县| 会东县| 赤峰市| 金平| 漳州市| 宜良县| 大厂| 望都县| 图片| 曲阜市| 湖南省| 河源市| 蒙自县| 富顺县| 云阳县| 建湖县| 萨迦县| 多伦县| 聂拉木县| 沂水县| 永仁县| 汤原县| 得荣县| 栾川县| 资阳市| 开江县| 陇西县| 阿荣旗| 甘泉县| 华阴市| 新巴尔虎右旗| 郧西县| 县级市| 思南县| 华容县| 福清市| 山西省|