7 绘图和手势: Core Graphics(核心绘图)/UIGestureRecognizer(手势) - 斯坦福 iOS

一些结构体

 //CGFloat:
 //CGPoint: 有 CGFloat x 和 CGFloat y 两个元素
 //CGSize: 有 CGFloat width 和 CGFloat height 两个属性
 //CGRect: 有 CGPoint origin 和 CGSize size 两个属性

View 的创建方法

 //view 的创建方法
 - (void)awakeFromNib;
 - (void)init;
 - (void)initWithFrame:(CGRect)aRect;

 //view 的添加和移除方法
 - (void)addSubview:(UIView *)aView;
 - (void)removeFromSuperView;

 //@property(nonatomic) CGRect bounds;  //它描述了在它自己的坐标系中的位置和大小
 //@property(nonatomic) CGPoint center; //描述了父视图的中心位置
 //@property(nonatomic) CGRect frame;   //它描述了在它在父视图坐标系统视图的位置和大小

Custom Views

 - (void)drawRect:(CGRect)aRect;    //实现这个方法就可以绘制内容, 这个方法是系统自动调用的, 不用手动调用(每次调用这个方法会刷新上下文)
 - (void)setNeedsDisplay;   //重新绘制视图(它会调用 drawRect 方法)
 - (void)setNeedsDisplayInRect:(CGRect)aRect;  //传进来的矩形会被重绘 

 //如何实现 drawRect:? 
    方法是使用 Quartz 库,叫做 Core Graphics(核心绘图) \
    Core Graphics 有很多 C 函数,函数名以 CG 开头, 几乎都以 context上下文 作为第一个参数 \
    还可以使用 UIBezierPath 类, UIBezierPath类可以把各种复杂的形状, 组成一个大的路径 \
    然后你可以在屏幕止对其进行描边或填充 \

 //Core Graphics(核心绘图) 的基本流程 \
    1.你需要有一个绘制的Context(上下文) \
    2.你需要创建路径 \
    3.然后设置颜色,线宽,描边,填充等属性 \
    4.对路径进行描边和填充 \

 //UIBezierPath 封装了上面4步的全部操作(其实也是操作上下文)
 //1.如何获得上下文? \
    一般 UIKit 会在调用 drawRect: 前设置好上下文, 所以当执行到 drawRect: 时上下文就可以使用了 \
    //CGContextRef context = UIGraphicsGetCurrentContext(); //如果在 drawRect: 中调用这个方法就可以获取上下文 \

 //2.如何定义路径? \
    UIBezierPath *path = [[UIBezierPath alloc] init]; //创建一个路径(画三角形) \

    [path moveToPoint:CGPointMake(75,10)];      //移动到点 \
    [path addLineToPoint:CGPointMake(160,150)]; //连线到点 \
    [path addLineToPoint:CGPointMake(10.150)];  //连线到点 \

    [path closePath];   //封闭路径 \

 //3.如何设设置颜色,线宽,描边,填充等属性?(除了描边和填充其它的都用 UIBezierPath类 进行设置) \

    //[[UIColor greenColor] set];   //调用 set 方法会把描边和填充设置为一个颜色
    [[UIColor greenColor] setFill]; //设置填充的颜色
    [[UIColor redColor] setStroke]; //设置描边的颜色
    path.lineWidth = 2.0;   //设置线宽为2

 //4.如何进行描边和填充? \

    [path fill];
    [path stroke];

UIBezierPath 的一些其它方法

    //1.bezierPathWithRoundedRect: cornerRadius: 方法返回一个圆角矩形 \
    UIBezierPath *roundedRect = [UIBezierPath bezierPathWithRoundedRect:(CGRect)bounds cornerRadius:(CGFloat)radius]; \

    //2.使用 UIBezierPath 来裁剪绘图 \
    如果你想绘制某种图案, 但是想让它出现在一个圆角矩形内 \
    你只要生成一个圆角矩形, 然后调用 addClip 方法 \
    从这之后,你的所有绘图都会被裁剪, 出现在那个圆角矩形路径内, 并且还可以添加更多的裁剪区域 \
    [roundedRect addClip]; \

 //3.上下文的存档与恢复 \
    CGContextSaveGState ( CGContextRef c );    //上下文的存档 \
    CGContextRestoreGState ( CGContextRef c ); //恢复到最近的存档 \


 //4.在上下文中绘制文本(属性字符串), 调用 NSAttributedString 的 drawAtPoint方法 \

     //创建属性化字符串, 添加所需要的属性
    NSAttributedString *text = [[NSAttributedString alloc] initWithString:@"colinwang" attributes:@{}];

    //调用 drawAtPoint: 方法,确定属性化字符串的位置
    [text drawAtPoint:(CGPoint)point];

    //获取文本所占空间的大小
    CGSize textSize = [text size];

 //5.绘制图片是用到 UIImage 中的绘制方法

UIViewContentMode(view的一个属性,它代表 bounds 变化时会发生什么)

文档说明:当 view 大小改变时, 通过选项来校准它的内容

typedef enum {
    UIViewContentModeScaleToFill,(默认,当 view.bounds 时通过拉伸像素来改变内容)
    UIViewContentModeScaleAspectFit,
    UIViewContentModeScaleAspectFill,
    UIViewContentModeRedraw,
    UIViewContentModeCenter,
    UIViewContentModeTop,
    UIViewContentModeBottom,
    UIViewContentModeLeft,
    UIViewContentModeRight,
    UIViewContentModeTopLeft,
    UIViewContentModeTopRight,
    UIViewContentModeBottomLeft,
    UIViewContentModeBottomRight,
} UIViewContentMode;

UIGestureRecognizer(手势)

 //UIGestureRecognizer 是一个抽象类, 不能对它进行实例化
 //手势识别器的使用步骤 \
    1.创建手势识别器,并添加到视图中 \
    2.提供一个处理器,当手势发生时要调用的方法

 //在输出口添加手势识别
 - (void)setPannableView:(UIView *)pannableView{

    _pannableView = pannableView;

    //1创建拖动手势
    UIPanGestureRecognizer *pangr = 
        [[UIPanGestureRecognizer alloc] initWithTarget:pannableView action:@selector(pan:)];

    //把手势加入到目标视图中
    [pannableView addGestureRecognizer:pangr];

 }

 - (void)pan:(UIPanGestureRecognizer *)recognizer{

    if((recognizer.state == UIPanGestureRecognizerStateChanged) || (recognizer.state == UIPanGestureRecognizerStateEnded)){

        //获得拖动后的位置
        CGPoint translation = [recognizer translationInView:self];
        self.origin = CGPointMake(self.origin.x + translation.x, self.origin.y + translation.y);
    
        //重置
        [recognizer setTranslation:CGPointZero inView:self];
    }
 }

手势动作

 UIPanGestureRecognizer      拖动手势
 UIPinchGestureRecognizer    捏合手势
 UIRotationGestureRecognizer 旋转手势
 UILongPressGestureRecognizer  长按手势
 UISwipeGestureRecognizer    滑动手势(不连续手势)
 UITapGestureRecognizer    点击手势(不连续手势)

你可能感兴趣的:(7 绘图和手势: Core Graphics(核心绘图)/UIGestureRecognizer(手势) - 斯坦福 iOS)