美文网首页
iOS 网格扫描线

iOS 网格扫描线

作者: 移动的键盘 | 来源:发表于2023-06-05 17:06 被阅读0次

    效果


    IMG_CFC06192E39E-2.jpeg
    #import <QuartzCore/QuartzCore.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface ScanLayer : CAShapeLayer
    
    + (instancetype)layerWithFrame:(CGRect)frame;
    
    - (void)stopRun;
    
    @end
    
    NS_ASSUME_NONNULL_END
    
    #import "ScanLayer.h"
    #import <UIKit/UIKit.h>
    
    @interface ScanLayer ()
    
    @property (nonatomic, strong) CAShapeLayer *layer;
    
    @end
    
    @implementation ScanLayer
    
    + (instancetype)layerWithFrame:(CGRect)frame
    {
        ScanLayer *l = [super layer];
        if (l) {
            l.frame = frame;
            l.masksToBounds = YES;
            l.backgroundColor = [UIColor clearColor].CGColor;
            [l draw];
        }
        return l;
    }
    
    - (void)draw
    {
        CGFloat w = 8;
        CGFloat h = 8;
        NSInteger indexW = (NSInteger)self.frame.size.width/w + 1;
        NSInteger indexH = (NSInteger)self.frame.size.height/h + 1;
        
        UIBezierPath *path = [UIBezierPath bezierPath];
        for (int i = 0; i < indexW; i++) {
            UIBezierPath *path1 = [UIBezierPath bezierPath];
            [path1 moveToPoint:CGPointMake(h * i, 0)];
            [path1 addLineToPoint:CGPointMake(h * i, self.frame.size.height)];
            [path appendPath:path1];
        }
        for (int j = 0; j < indexH; j++) {
            UIBezierPath *path1 = [UIBezierPath bezierPath];
            [path1 moveToPoint:CGPointMake(0, w * j)];
            [path1 addLineToPoint:CGPointMake(self.frame.size.width, w * j)];
            [path appendPath:path1];
        }
        self.layer = [CAShapeLayer layer];
        self.layer.frame = self.bounds;
        self.layer.path = path.CGPath;
        self.layer.lineWidth = 1;
        self.layer.strokeColor = [UIColor greenColor].CGColor;
        self.layer.fillColor = [UIColor clearColor].CGColor;
        [self addSublayer:self.layer];
    
        
        CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
        anim.fromValue = @(-self.frame.size.height);
        anim.toValue = @(0);
        anim.repeatCount = MAXFLOAT;
        anim.duration = 1.0;
        anim.fillMode = kCAFillModeForwards;
        anim.removedOnCompletion = NO;
        anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
        
        CABasicAnimation *anim1 = [CABasicAnimation animationWithKeyPath:@"opacity"];
        anim1.fromValue = @1;
        anim1.toValue = @(0);
        anim1.repeatCount = MAXFLOAT;
        anim1.duration = 1.0;
        anim1.fillMode = kCAFillModeForwards;
        anim1.removedOnCompletion = NO;
        anim1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
        
        CAAnimationGroup *group = [CAAnimationGroup animation];
        group.animations = @[anim,anim1];
        group.repeatCount = MAXFLOAT;
        group.duration = 1.0;
        group.fillMode = kCAFillModeForwards;
        group.removedOnCompletion = NO;
        [self.layer addAnimation:group forKey:@"anim"];
        
    }
    
    - (void)stopRun
    {
        [self.layer removeAllAnimations];
    }
    
    @end
    

    相关文章

      网友评论

          本文标题:iOS 网格扫描线

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