美文网首页
Quartz2D_Day4 基本应用

Quartz2D_Day4 基本应用

作者: MR_詹 | 来源:发表于2016-12-01 16:00 被阅读4次

    雪花划落

    two.gif
    #import "SnowView.h"
    
    @interface SnowView()
    @property (nonatomic,assign) float imagaeY;
    
    @end
    
    @implementation SnowView
    
    - (instancetype)initWithCoder:(NSCoder *)aDecoder{
        self = [super initWithCoder:aDecoder];
        if (self) {
            //CADisplayLink刷帧定时器,默认是每秒刷新60次
            //该定时器创建后,默认是不会执行的,需要把它加载到消息循环中
            CADisplayLink *display = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateImage)];
            [display addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
        }
        return self;
    }
    
    - (void)updateImage{
        //调用此方法重绘画面
        [self setNeedsDisplay];
    }
    
    - (void)awakeFromNib{
        [super awakeFromNib];
        NSLog(@"awakeFromNib");
    }
    
    - (void)drawRect:(CGRect)rect{
        self.imagaeY += 5;
        if (self.imagaeY > rect.size.height) {
            self.imagaeY = 0;
        }
        UIImage *image = [UIImage imageNamed:@"雪花.png"];
        [image drawAtPoint:CGPointMake(0, self.imagaeY)];
        
    }
    

    第一个:

    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateImage) userInfo:nil repeats:YES];
    
    说明: NSTimer一般用于定时的更新一些非界面上的数据,告诉多久调用一次
    

    第二个:

     CADisplayLink *display= [CADisplayLink displayLinkWithTarget:self selector:@selector(updateImage)];
     [display addToRunLoop:[NSRunLoopmainRunLoop] forMode:NSDefaultRunLoopMode];
     说明: CADisplayLink刷帧,默认每秒刷新60次。该定时器创建之后,默认是不会执行的,需要把它加载到消息循环中
    

    动态改变圆大小

    two.gif
    #import "CircleView.h"
    
    @implementation CircleView
    
    - (void)setRadius:(CGFloat)radius{
        _radius = radius;
        [self setNeedsDisplay];
    }
    
    - (void)drawRect:(CGRect)rect{
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        //将当前View相对于父视图的中心坐标转换为相对于自己坐标系的中心点
        CGPoint center = self.center;
        CGPoint newPoint = [self convertPoint:center fromView:self.superview];
        CGContextAddArc(ctx, newPoint.x, newPoint.y, self.radius, 0, 2*M_PI, 0);
        [[UIColor redColor]setFill];
        CGContextFillPath(ctx);
    }
    
    - (void)awakeFromNib{
        [super awakeFromNib];
        self.radius = 20;
    }
    

    相关文章

      网友评论

          本文标题:Quartz2D_Day4 基本应用

          本文链接:https://www.haomeiwen.com/subject/mhgxmttx.html