zoukankan      html  css  js  c++  java
  • ios中自定义图层的2种方法

    1:自定义图层,在图层中画图

    #import <QuartzCore/QuartzCore.h>
    
    @interface MJLayer : CALayer
    
    @end
    
    #import "MJLayer.h"
    
    @implementation MJLayer
    
    #pragma mark 在这个方法中所画的动画都会显示到MJLayer上面
    - (void)drawInContext:(CGContextRef)ctx {
        // 在这里不能使用跟UIKit相关的东西
        // [[UIColor redColor] set];
        
        CGContextSetRGBFillColor(ctx, 1, 0, 0, 1);
        
        CGContextAddEllipseInRect(ctx, CGRectMake(0, 0, 50, 50));
        CGContextFillPath(ctx);
    }
    
    @end

    ==================================================================

    - (void)diylayer {
    MJLayer *layer = [MJLayer layer];
    layer.frame = CGRectMake(50, 50, 100, 100);
    layer.backgroundColor = [UIColor blueColor].CGColor;
    // 只有调用这个方法才会进行第一次的绘制
    [layer setNeedsDisplay];
    [self.view.layer addSublayer:layer];
    }



    画图使用代理方式

    - (void)diylayer2 {
        CALayer *layer = [CALayer layer];
        layer.frame = CGRectMake(50, 50, 100, 100);
        layer.backgroundColor = [UIColor blueColor].CGColor;
        // 设置代理,让代理帮图层画东西
        layer.delegate = self;
        // 只有调用这个方法才会进行第一次的绘制
        [layer setNeedsDisplay];
        [self.view.layer addSublayer:layer];
    }
    
    #pragma mark 图层的代码方法,在这里帮图层画东西
    // 方法定义在QuartzCore框架中CALayer.h的@interface NSObject (CALayerDelegate)分类
    - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
        CGContextSetRGBFillColor(ctx, 1, 0, 0, 1);
        
        CGContextAddEllipseInRect(ctx, CGRectMake(50, 50, 50, 50));
        CGContextFillPath(ctx);
    }
  • 相关阅读:
    用c#小程序理解线程
    我对线程入门理解
    网站发布后IIS故障解决办法
    ASP .NET XML 文件
    SortedList 对象兼有 ArrayList 和 Hashtable 对象的特性。
    (笔记)索引器
    HOW TO:使用 Visual C# .NET 在 ASP.NET 中创建自定义错误报告
    读取EXCEL的数据到datagridview
    一个超级简单的文件流操作WINDOW应用程序
    Gridview事件
  • 原文地址:https://www.cnblogs.com/gcb999/p/3189277.html
Copyright © 2011-2022 走看看