iOS swift OC 水纹效果

郑斌

郑斌

发表于 2016-07-23 20:30:17
内容来源: 网络
iOS swift  OC 水纹效果
swift
______________________________________________________________________________________
//  Created by 王木木 on 16/7/7.
//  Copyright © 2016年 王木木. All rights reserved.
//

import UIKit

class BoWenView: UIView {

    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        
        super.drawRect(rect)
        
        let color = UIColor.whiteColor()

        color.setFill()
        
        UIRectFill(rect)
        
        let pulsingCount = 8
        
        let animationDuration = 4
        
        let animationLayer = CALayer()
        
        for i in 0...(pulsingCount - 1) {
            
            let pulsingLayer = CALayer()
            
            pulsingLayer.frame = CGRectMake(rect.size.width/4, rect.size.width/4, rect.size.width/2, rect.size.height/2)
            
            pulsingLayer.borderColor = UIColor.init(red: 0.17, green: 0.8, blue: 0.39, alpha: 1).CGColor;
            
            pulsingLayer.borderWidth = 6;
            
            pulsingLayer.cornerRadius = rect.size.height / 2 / 2
        
        let defaultCurve = CAMediaTimingFunction.init(name: kCAMediaTimingFunctionDefault)
        
            
            let animationGroup = CAAnimationGroup()
            
            
            animationGroup.fillMode = kCAFillModeBackwards
            
            animationGroup.beginTime = CACurrentMediaTime() + Double(i) * Double(animationDuration) / Double(pulsingCount)
            
            animationGroup.duration = Double(animationDuration)
            
            animationGroup.repeatCount = HUGE;
            
            animationGroup.timingFunction = defaultCurve;
            
           let scaleAnimation = CABasicAnimation.init(keyPath: "transform.scale")
            
            scaleAnimation.fromValue = 1.2;
            
            scaleAnimation.toValue = 2.2;
            
            let opacityAnimation = CAKeyframeAnimation.init(keyPath: "opacity")
            
            opacityAnimation.values = [1, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0];
          
            opacityAnimation.keyTimes = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1];
            
            animationGroup.animations = [scaleAnimation, opacityAnimation]
            
            pulsingLayer.addAnimation(animationGroup, forKey: "plulsing")
            
            animationLayer.addSublayer(pulsingLayer)
            
        }
        
        self.layer.addSublayer(animationLayer)
        
    }
 

}
______________________________________________________________________________________
oc
______________________________________________________________________________________

//  Created by 王木木 on 16/7/7.
//  Copyright © 2016年 王木木. All rights reserved.
//

#import "BoWenView.h"

@implementation BoWenView


- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        UILabel *laebl = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 50, 50)];
        laebl.backgroundColor =  [UIColor colorWithRed:0.12f green:0.72f blue:0.33f alpha:1.00f];
        [self addSubview:laebl];
    }
    
    return self;
    
}

-(void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    UIColor *color = [UIColor whiteColor];
    
    [color setFill];
    UIRectFill(rect);
    NSInteger pulsingCount = 8;
    double animationDuration = 4;
    CALayer * animationLayer = [CALayer layer];
    for (int i = 0; i < pulsingCount; i++) {
        CALayer * pulsingLayer = [CALayer layer];
        pulsingLayer.frame = CGRectMake(rect.size.width/4, rect.size.width/4, rect.size.width/2, rect.size.height/2);
        pulsingLayer.borderColor = [UIColor colorWithRed:0.17f green:0.80f blue:0.39f alpha:1.00f].CGColor;
        pulsingLayer.borderWidth = 6;
        pulsingLayer.cornerRadius = rect.size.height / 2 /2;
        
        CAMediaTimingFunction * defaultCurve = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
        
        CAAnimationGroup * animationGroup = [CAAnimationGroup animation];
        animationGroup.fillMode = kCAFillModeBackwards;
        animationGroup.beginTime = CACurrentMediaTime() + (double)i * animationDuration / (double)pulsingCount;
        animationGroup.duration = animationDuration;
        animationGroup.repeatCount = HUGE;
        animationGroup.timingFunction = defaultCurve;
        
        CABasicAnimation * scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
        scaleAnimation.fromValue = @1.2;
        scaleAnimation.toValue = @2.2;
        
        CAKeyframeAnimation * opacityAnimation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
        opacityAnimation.values = @[@1, @0.9, @0.8, @0.7, @0.6, @0.5, @0.4, @0.3, @0.2, @0.1, @0];
        opacityAnimation.keyTimes = @[@0, @0.1, @0.2, @0.3, @0.4, @0.5, @0.6, @0.7, @0.8, @0.9, @1];
        
        animationGroup.animations = @[scaleAnimation, opacityAnimation];
        [pulsingLayer addAnimation:animationGroup forKey:@"plulsing"];
        [animationLayer addSublayer:pulsingLayer];
    }
    
    [self.layer addSublayer:animationLayer];
}

@end





                            
用户评论
开源开发学习小组列表