国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

繪畫板

2019-11-14 18:20:53
字體:
來源:轉載
供稿:網友

知識點:將View上的信息保存到相冊

關于繪制圖形、路線

******************************************

#import <UIKit/UIKit.h>

@interface FFFPaintView : UIView

@PRoperty (nonatomic,strong) UIColor *lineColor;

@property(nonatomic,assign) CGFloat lineWithed;

@property (nonatomic,copy) CGFloat (^lineWithBlock)();

//清屏

-(void)clearScreen;

//回退

-(void)recede;

//橡皮擦

-(void)eraser;

@end

******************************************

#import "FFFPaintView.h"

@interface FFFBezierPath : UIBezierPath

@property (nonatomic,strong) UIColor *lineColor;

@end

@implementation FFFBezierPath

@end

 

@interface FFFPaintView ()

@property (nonatomic,strong) NSMutableArray *paths;

@end

@implementation FFFPaintView

 

-(NSMutableArray *)paths{

 

    if(_paths==nil){

    

        _paths = [NSMutableArray array];

    }

    return _paths;

}

//清屏

-(void)clearScreen{

    

//    注意這里有時候移除全部的時候會出錯,所以為了防止,建議 == nil,即可,然后在重繪

    [self.paths removeAllObjects];

    self.paths = nil;

    //    重繪

    [self setNeedsDisplay];

}

//回退

-(void)recede{

    [self.paths removeLastObject];

    //    重繪

    [self setNeedsDisplay];

}

//橡皮擦

-(void)eraser{

    self.lineColor = self.backgroundColor;

    //    重繪

    [self setNeedsDisplay];

}

 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

//   獲取觸摸的對象

    UITouch *touch = [touches anyObject];

//    通過觸摸對象獲取手指的位置

    CGPoint point = [touch locationInView:touch.view];  

//    創建路徑對象

    FFFBezierPath *path = [[FFFBezierPath alloc] init];

    [path moveToPoint:point];   

//    設置線的顏色

    path.lineColor = self.lineColor;

 

    if(self.lineWithBlock){

        path.lineWidth = self.lineWithBlock();

    }

//    下面的方法是無法獲取到值的

//    path.lineWidth = self.lineWithed;

//    NSLog(@"%f",self.lineWithed);

    

//    把path添加到數組中

    [self.paths addObject:path];

}

 

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

 

//    獲取觸摸的對象

    UITouch *touch = [touches anyObject];

    CGPoint point = [touch locationInView:touch.view];

    

//    使用數組當中最后一個路徑 來連線

    [[self.paths lastObject] addLineToPoint:point];

    

//    重繪

    [self setNeedsDisplay];

}

 

-(void)drawRect:(CGRect)rect{

//    遍歷所有的路徑渲染

    for (FFFBezierPath *path in self.paths) {

        [path.lineColor set];

        

//        設置樣式

        [path setLineCapStyle:kCGLineCapRound];

        [path setLineJoinStyle:kCGLineJoinRound];

    //    渲染

        [path stroke];

//        NSLog(@"%@",path);

    }

}

 

@end

 

************************************************************************************

 

#import "ViewController.h"

#import "FFFPaintView.h"

@interface ViewController ()

 

@property (weak, nonatomic) IBOutlet FFFPaintView *paintView;

 

@property (weak, nonatomic) IBOutlet UISlider *slider;

 

@property (weak, nonatomic) IBOutlet UIButton *firstButton;

 

@end

 

@implementation ViewController

//- (IBAction)slider:(UISlider *)sender {

//    

//    self.paintView.lineWithed = sender.value;

//    

//}

 

- (IBAction)savePaint {

    

//    開啟圖片類型的上下文

    UIGraphicsBeginImageContextWithOptions(self.paintView.bounds.size, NO, 0);

    

//    獲取當前上下文

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    

//    把當前view的樣式,繪制到上下文當中

    [self.paintView.layer renderInContext:ctx];

    

//    保存到相冊,同時從上下文中獲取圖片

    UIImageWriteToSavedPhotosAlbum(UIGraphicsGetImageFromCurrentImageContext(), nil, nil, nil);

    

//    關閉上下文

    UIGraphicsEndImageContext();

}

 

//清屏

-(IBAction)clearScreen{

    [self.paintView clearScreen];

}

//回退

-(IBAction)recede{

    [self.paintView recede];

}

//橡皮擦

-(IBAction)eraser{

    [self.paintView eraser];

}

 

- (void)viewDidLoad {

    [super viewDidLoad];

    [self.paintView setLineWithBlock:^CGFloat{

        return self.slider.value;

    }];

    self.paintView.lineColor = self.firstButton.backgroundColor;

}

 

- (IBAction)setColorSelected:(UIButton *)sender {

    self.paintView.lineColor = sender.backgroundColor;

}

@end

 

 

************************************************************************************

 

 

 

************************************************************************************

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 沙湾县| 永和县| 青浦区| 林西县| 阿城市| 荣成市| 大港区| 丁青县| 岑溪市| 天镇县| 上栗县| 石城县| 乐平市| 礼泉县| 茶陵县| 宁都县| 正安县| 虹口区| 高尔夫| 潍坊市| 石屏县| 长汀县| 古浪县| 萍乡市| 民权县| 乌鲁木齐市| 盐亭县| 石林| 临江市| 武汉市| 富顺县| 巧家县| 弥勒县| 海宁市| 德清县| 冕宁县| 苏尼特右旗| 隆化县| 云阳县| 馆陶县| 德化县|