• iOS上手指点击波纹效果的实现


    https://www.jianshu.com/p/35e6f53ca0fe

    2016.10.19 22:00* 字数 135 阅读 2468评论 2

    闲暇时间做了一个反馈手指点击屏幕的效果,用到了CAShapeLayer和基本的动画知识,模拟器上效果如下:


     
    fingerWave.gif
    • 这种效果使用在某些页面上肯定会给用户更有趣的体验,特别是面向儿童的app中

    具体的实现代码如下

    • 首先监听控制器view的Tap事件
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap:)];
        [self.view addGestureRecognizer:tap];
    
     - (void)onTap:(UITapGestureRecognizer*)sender {
        CGPoint center = [sender locationInView:sender.view];
        [FingerWaveView  showInView:self.view center:center];
    }
    
    • FingerWaveView.h
    #import <UIKit/UIKit.h>
    @interface FingerWaveView : UIView
     + (instancetype)showInView:(UIView *)view center:(CGPoint)center;
    @end
    
    • FingerWaveView.m
    #import "FingerWaveView.h"
    @interface FingerWaveView () <CAAnimationDelegate>
    {
        CGSize waveSize;
        NSTimeInterval duration;
    }
    @end
    @implementation FingerWaveView
     - (instancetype)initWithFrame:(CGRect)frame{
        self=[super initWithFrame:frame];
        if (self) {
            waveSize = CGSizeMake(150, 150);
            duration = 1.0;
        }
        return self;
    }
     + (instancetype)showInView:(UIView *)view center:(CGPoint)center {
        FingerWaveView *waveView = [FingerWaveView new];
        [waveView setframeWithCenter:center];
        [view addSubview:waveView];
        return waveView;
    }
     - (void)didMoveToSuperview{
        CAShapeLayer *waveLayer = [CAShapeLayer new];
        waveLayer.backgroundColor  = [UIColor clearColor].CGColor;
        waveLayer.opacity = 0.6;
        waveLayer.fillColor = [UIColor whiteColor].CGColor;
        [self.layer addSublayer:waveLayer];
        
        [self startAnimationInLayer:waveLayer];
    }
     - (void)startAnimationInLayer:(CALayer *)layer{
        UIBezierPath *beginPath = [UIBezierPath bezierPathWithArcCenter:[self pathCenter] radius:[self animationBeginRadius] startAngle:0 endAngle:M_PI*2 clockwise:YES];
        UIBezierPath *endPath = [UIBezierPath bezierPathWithArcCenter:[self pathCenter] radius:[self animationEndRadius] startAngle:0 endAngle:M_PI*2 clockwise:YES];
        
        CABasicAnimation *rippleAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
        rippleAnimation.delegate = self;
        rippleAnimation.fromValue = (__bridge id _Nullable)(beginPath.CGPath);
        rippleAnimation.toValue = (__bridge id _Nullable)(endPath.CGPath);
        rippleAnimation.duration = duration;
        
        CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
        opacityAnimation.delegate = self;
        opacityAnimation.fromValue = [NSNumber numberWithFloat:0.6];
        opacityAnimation.toValue = [NSNumber numberWithFloat:0.0];
        opacityAnimation.duration = duration;
        
        [layer addAnimation:rippleAnimation forKey:@"rippleAnimation"];
        [layer addAnimation:opacityAnimation forKey:@"opacityAnimation"];
    }
     - (void)setframeWithCenter:(CGPoint)center{
        CGRect frame = CGRectMake(center.x-waveSize.width*0.5, center.y-waveSize.height*0.5, waveSize.width, waveSize.height);
        self.frame = frame;;
    }
     - (CGFloat)animationBeginRadius{
        return waveSize.width*0.5*0.2;
    }
     - (CGFloat)animationEndRadius{
        return waveSize.width*0.5;
    }
     - (CGPoint)pathCenter{
        return CGPointMake(waveSize.width*0.5, waveSize.height*0.5);
    }
    #pragma mark - CAAnimationDelegate
     - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
        if (flag) {
            [self removeFromSuperview];
        }
    }
    @end
    
    • 大家也可以DIY我的代码,做出很多其他的效果,比如改成其他的波纹颜色。


    作者:星星Y灬
    链接:https://www.jianshu.com/p/35e6f53ca0fe
    來源:简书
    简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
  • 相关阅读:
    Redis批量删除key的小技巧,你知道吗?
    Spring条件注解@Conditional
    Spring Boot 2.X 如何快速集成单元测试?
    idea git提交时候提示 --author 'java_suisui' is not 'Name ' and matches no existing author
    Spring Boot 2.X 如何添加拦截器?
    SpringMVC+Mybatis 如何配置多个数据源并切换?
    Spring Boot 2.X 如何优雅的解决跨域问题?
    基于SSL实现MySQL的加密主从复制
    mysql -- mysql基于ssl的主从复制
    MySQL DB 主从复制之SSL
  • 原文地址:https://www.cnblogs.com/sundaysgarden/p/10305324.html
Copyright © 2020-2023  润新知