美文网首页
自定义按钮动画

自定义按钮动画

作者: William_ | 来源:发表于2018-06-07 18:37 被阅读2次

    自定义一个类,继承与UIButton。

    -(void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event{
    
        [super sendAction:action to:target forEvent:event];
        
        //按钮点击时的效果
        CAShapeLayer * layer = [[CAShapeLayer alloc]init];
        layer.frame = self.bounds;
        layer.strokeColor = [UIColor clearColor].CGColor;
        layer.fillColor = [[UIColor blackColor] colorWithAlphaComponent:0.1].CGColor;
        [self.layer addSublayer:layer];
        
    
        UIBezierPath * pathStart = [UIBezierPath bezierPath];
        pathStart = [UIBezierPath bezierPathWithOvalInRect:self.bounds]; //椭圆形,如果是rect是正方形,则是圆形。
        
        UIBezierPath * pathEnd = [UIBezierPath bezierPathWithOvalInRect:CGRectInset(self.bounds, -5, -5)];
        
    
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];
        animation.delegate = self;
        animation.fromValue = (__bridge id)pathStart.CGPath;
        animation.toValue =  (__bridge id)pathEnd.CGPath;
        animation.duration = 0.25f;
        animation.timingFunction = [CAMediaTimingFunction  functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        [layer addAnimation:animation forKey:@"path"];
        
      
        
    }
    
    

    相关文章

      网友评论

          本文标题:自定义按钮动画

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