ios 继承UIView实现自定义视图——实现画图

主要的原理包括:

继承UIView ,重载drawrect和重载触摸事件

待实现的功能还有,路径数组保存等。

用可变数据保存path路径

画曲线是通过二次贝塞尔曲线实现的

这里可以得到画图的UIImage对象

 UIGraphicsBeginImageContext(self.bounds.size);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *result=UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();
    return  result;
#import "testdrow.h"

@implementation testdrow
 UIColor *pick_color;
int choose;
UIBezierPath *mpath ;
UIBezierPath *eraser_path;
NSTimer *mytime;
UIImage *tempdraw;
CGContextRef CONTEXT;
-(void) settime:(NSTimer *)test_time{
   if(choose==1)
       choose=0;
   else if(choose==0)
        choose=1;
    [self setNeedsDisplay];
}
- (IBAction)test_draw:(id)sender {
    mytime=[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(settime:) userInfo:nil repeats:YES];
}

- (IBAction)eraser:(id)sender {
   //pick_color=[UIColor alloc];
   // [mpath closePath];
    pick_color=[UIColor blackColor];
    choose=-1;
}

- (id)initWithFrame:(CGRect)frame
{
    mpath=[[UIBezierPath bezierPath] init];
    eraser_path=[[UIBezierPath bezierPath]init];
   //pick_color=[[UIColor alloc]init];
  pick_color=[UIColor new];
    pick_color=[UIColor greenColor];
    choose=1;
    [self setBackgroundColor:[UIColor blackColor]];
    self.backgroundColor=[UIColor blackColor];
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        //self init

    }

    return self;

}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/
//int x,y;
//int firstx,firsty;

/*
- (void) drawRect:(CGRect)rect{
    CGContextRef CONTEXT=UIGraphicsGetCurrentContext();
    CGFloat temp=25.4;
    CGContextSetStrokeColorWithColor(CONTEXT,[UIColor redColor].CGColor);
    CGContextSetLineWidth(CONTEXT,5.8);
    CGContextMoveToPoint(CONTEXT,firstx,firsty);
    CGContextAddLineToPoint(CONTEXT, x, y);
    //CGContextAddLines(CONTEXT, //, <#size_t count#>)
       CGContextStrokePath(CONTEXT);
    printf("drawRect\n");
}
 */
-(void) drawRect1:(CGRect)rect{
    switch(choose){
        case 1:
        case -1:
            [tempdraw drawInRect:rect];
            break;
        case 0:
            //CONTEXT=UIGraphicsGetCurrentContext();
            //[self drawRect2:rect];

            break;
    }

}
- (void)drawRect:(CGRect)rect
{
   // pick_color=[UIColor alloc];
   // UIGraphicsBeginImageContext(self.frame.size);

    mpath.lineWidth = 15.0;

    mpath.lineCapStyle = kCGLineJoinRound; //线条拐角
    mpath.lineJoinStyle = kCGLineCapRound;
  CGContextRef CONTEXT=UIGraphicsGetCurrentContext();
  //  UIGraphicsBeginImageContext(SELF.CGSizeMake);
CGContextSetBlendMode(CONTEXT, kCGBlendModeNormal);
    CGContextSetShadowWithColor(CONTEXT,CGSizeMake(1, 1),18.f,  [pick_color CGColor]);

    UIColor *color =pick_color;
    //[UIColor greenColor];
    [color set]; //设置线条颜色
    UIColor *back_color=[UIColor blackColor];
    //UIBezierPath* apath =mpath;
    //[UIBezierPath bezierPath];
    //终点处理

    switch(choose){
        case 1:[mpath stroke];
            break;
        case -1:
            [eraser_path stroke];
            break;
        case 0:
            [back_color set];

            break;
    }
        //Draws line 根据坐标点连线
}

static CGPoint midpoint(CGPoint p0, CGPoint p1) {
    return (CGPoint) {
        (p0.x + p1.x) / 2.0,
        (p0.y + p1.y) / 2.0
    };
}

 -  (UIImage *) getimage{

    UIGraphicsBeginImageContext(self.bounds.size);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *result=UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();
    return  result;

}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
       UITouch *touch=[touches anyObject];
    CGPoint point=[touch locationInView:self];
    CGPoint mpoint=midpoint(lastpoint, point);
    switch(choose){
        case 1:[mpath addQuadCurveToPoint:mpoint controlPoint:lastpoint];
            break;
        case -1:
            [eraser_path addQuadCurveToPoint:mpoint controlPoint:lastpoint];
            break;
    }

    lastpoint=point;
    int tempx=point.x;
    int tempy=point.y;
    NSLog(@"touches MOVE%d %d \n",tempx,tempy);

    UIGraphicsBeginImageContext(self.frame.size);

    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    CONTEXT=UIGraphicsGetCurrentContext();
  [tempdraw drawInRect:self.frame];
    //[self drawRect2:self.frame];
    UIImage *result=UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();
    tempdraw=result;
    [self setNeedsDisplay];

}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    //[super touchesBegan:<#touches#> withEvent:<#event#>];
  //  touches uitouch
   // mpath=[[UIBezierPath bezierPath] init];
    if(choose==0){
        [mytime invalidate];
        choose=1;
    }
    CGPoint point=[[touches anyObject] locationInView:self];
    firstx=point.x;
    firsty=point.y;
    switch(choose){
        case 1:    [mpath moveToPoint:point];
            break;
        case -1:
            [eraser_path moveToPoint:point];
    }
    lastpoint=point;
    //NSLog(@"%d %d \n",x,y);

  [self setNeedsDisplay];

}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
   // [mpath closePath];
}

@end

ios 继承UIView实现自定义视图——实现画图,布布扣,bubuko.com

时间: 2024-08-05 11:17:02

ios 继承UIView实现自定义视图——实现画图的相关文章

Swift - 继承UIView实现自定义可视化组件(附记分牌样例)

在iOS开发中,如果创建一个自定义的组件通常可以通过继承UIView来实现.下面以一个记分牌组件为例,演示了组件的创建和使用,以及枚举.协议等相关知识的学习. 效果图如下: 组件代码:ScoreView.swift 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 5

继承UIView写自定义button

#import <UIKit/UIKit.h> @interface LPButton : UIView @property (nonatomic,strong) id target; @property (nonatomic,assign) SEL action; - (void)addTarget:(id)target action:(SEL)action; @end // // LPButton.m // loopdiner // // Created by yl on 15/12/8.

iOS基础-自定义视图、视图控制器

自定义视图 例如,登陆页面中,左边label,右边textField.自定义 @interface LTView : UIView @property (nonatomic,retain) UILabel *label; @property (nonatomic,retain) UITextField *textField; - (instancetype)initWithFrame:(CGRect)frame text:(NSString *)text placeHolder:(NSStrin

?Xamarin iOS教程之自定义视图

Xamarin iOS教程之自定义视图 Xamarin iOS自定义视图 工具栏中的视图在实际应用开发中用的很多,但是为了吸引用户的眼球,开发者可以做出一些自定义的视图. [示例2-33]以下将实现一个自定义的视图.当用户触摸屏幕时,就会出现一个显示手指当前位置的标签视图,以及改变主视图的背景颜色.代码如下: (1)创建一个Single View Application类型的工程,命名为2-13. (2)添加一个C#的类文件,并命名为MyView,具体步骤如下: 首先,选择菜单栏中的文件|New

工程日记之HelloSlide(1):Swift自定义可视化组件的方法(继承UIView和在StoryBoard中设置)

需求描述 HelloSlide是把文本自动转化成幻灯片的软件,在幻灯片中我们有SmartArt:各种各样的几何形状,并且可以自定义大小和颜色,放在幻灯片不同的位置. 为了在我们的软件中实现类似的效果,我封装了一些自定义的组件,因为暂时只需要几何形状,我通过直接继承UIView来实现 代码 class ArcView:UIView{ var mystrokecolor:UIColor //设置笔触颜色 var color : UIColor //设置填充颜色 init(frame:CGRect,c

Custom UIView 通过xib实现一个简单地自定义视图

1 /** 2 * 使用Custom UIView的这条路,我走的很不顺利,前期犯2,中期有事,浪费了很多时间! 3 * 现在空闲的时间不多了,只能抓紧时间写两个小demo,一来做练习,二来整理下也许能给要学习的人一些帮助! 4 */ 1 /** 2 * Custom UIView常用的三种实现方法: 3 * Method_1:通过初始化的方式(eg:UITableViewCell的重写) 4 Method_2:通过drawRect的方法,使用代码自己画出想要的视图 5 Method_3:通过x

自定义视图(继承View)

前言 Android提供了丰富的控件,但是有时候还是不能满足自己的需求,这时候就需要自定义视图了,自定义视图分为几种,一种为继承为View的,一种为继承于ViewGroup的.继承于View的需要我们自己去绘制控件,继承于ViewGroup的可以组织已有的控件,下面就先介绍下继承于View的情况. 效果图 下面就是自定义了一个简单的圆形图来介绍整个的绘制过程,如下所示 概述 绘制一个控件需要绘制两部分内容,一是尺寸,二是内容,这通过两个方法来进行绘制,一个是onMeasure.一个是onDraw

UI第三讲.自定义视图 视图控制器指定自定义view 检测屏幕旋转 处理内存警告 容器视图控制器

一.自定义视图 (自定义label-textField视图) 目的:为了进一步优化登录界面,提高代码的精简程度和复用性,可移植性,从而需要在原有视图控件的基础之上自由组合成自定义视图. 一般自定义的视图会继承于UIView.以下是自定义视图的要点和步骤: 1.创建一个UIView子类 2.在类的初始化方法中添加子视图 3.类的.h文件提供一些接口(方法),便于外界操作子视图. 例子及相应代码: 例题:假设我们使用LTView类代表label-textfield视图.创建一个LTView类继承于U

UI开发----自定义视图和视图控制器(Controller)

//  Created By 郭仔  2015年04月14日21:34:01 一.自定义视图: 根据需求的不同,?自定义视图继承的类也有所不同.?一般?自定义的 视图会继承于UIView.以下是?自定义视图的要点: 1.创建?一个UIView?子类 2.在类的初始化?方法中添加?子视图 3.类的.h?文件提供?一些接?口(?方法),便于外界操作?子视图 ================== 这里以label-textfield自定义视图为例: 把Label和Textfield封装到LTView中