2019-04-29 16:39:51 heyuzu1 阅读数 185

利用stroke和Path动画制作下拉刷新动画。
首先创建一个虚线圆。
image_1

	let ovalShapeLayer = CAShapeLayer()
    ovalShapeLayer.strokeColor = UIColor.white.cgColor//指定path的渲染颜色
    ovalShapeLayer.fillColor = UIColor.clear.cgColor//指定圆填充色
    ovalShapeLayer.lineWidth = 4.0//线宽
    ovalShapeLayer.lineDashPattern = [2, 3]//实线长度2,间隙长度3

    let refreshRadius = frame.size.height/2 * 0.8

    ovalShapeLayer.path = UIBezierPath(ovalIn: CGRect(
      x: frame.size.width/2 - refreshRadius,
      y: frame.size.height/2 - refreshRadius,
      width: 2 * refreshRadius,
      height: 2 * refreshRadius)
      ).cgPath//传递path
    layer.addSublayer(ovalShapeLayer)

lineDashPattern:设置边线的样式,默认为实线,传递一个NSNumber数组,数组中的数值依次表示虚线中,单个线的长度和空白的长度。
在scrollView滚动时,更改路径终点的位置,progress表示下拉百分比值:

	ovalShapeLayer.strokeEnd = progress

strokeEnd默认值为1,strokeStart默认值为0,起始值和终点值取值范围为0-1。
progress为0.5时,效果如图:
image_2
现在我们来初始化一架飞机,设置opacity初始值为0.0。

    let airplaneImage = UIImage(named: "airplane.png")!
    airplaneLayer.contents = airplaneImage.cgImage
    airplaneLayer.bounds = CGRect(x: 0.0, y: 0.0,
                                  width: airplaneImage.size.width,
                                  height: airplaneImage.size.height)
    
    airplaneLayer.position = CGPoint(
        x: frame.size.width/2 + frame.size.height/2 * 0.8,
        y: frame.size.height/2)
    airplaneLayer.opacity = 0.0
    layer.addSublayer(airplaneLayer)

当progress改变时,修改其透明度opacity

	airplaneLayer.opacity = Float(progress)

在开始刷新时,为strokeStart和strokeEnd添加动画,让虚线看起来在转圈。

    let strokeStartAnimation = CABasicAnimation(
        keyPath: "strokeStart")
    strokeStartAnimation.fromValue = -0.5
    strokeStartAnimation.toValue = 1.0
    
    let strokeEndAnimation = CABasicAnimation(
        keyPath: "strokeEnd")
    strokeEndAnimation.fromValue = 0.0
    strokeEndAnimation.toValue = 1.0
    
    let strokeAnimationGroup = CAAnimationGroup()
    strokeAnimationGroup.duration = 1.5
    strokeAnimationGroup.repeatDuration = 5.0
    strokeAnimationGroup.animations =
        [strokeStartAnimation, strokeEndAnimation]
    ovalShapeLayer.add(strokeAnimationGroup, forKey: nil)

image_3
同时为飞机添加运动轨迹

    let flightAnimation = CAKeyframeAnimation(keyPath: "position")
    flightAnimation.path = ovalShapeLayer.path
    flightAnimation.calculationMode = kCAAnimationPaced
    
    let airplaneOrientationAnimation = CABasicAnimation(keyPath:
        "transform.rotation")
    airplaneOrientationAnimation.fromValue = 0
    airplaneOrientationAnimation.toValue = 2.0 * .pi
    
    let flightAnimationGroup = CAAnimationGroup()
    flightAnimationGroup.duration = 1.5
    flightAnimationGroup.repeatDuration = 5.0
    flightAnimationGroup.animations = [flightAnimation,
                                       airplaneOrientationAnimation]
    airplaneLayer.add(flightAnimationGroup, forKey: nil)

这里有一个新的属性calculationMode计算模式,它有以下取值
kCAAnimationLinear:calculationMode的默认值,表示当关键帧为座标点的时候,关键帧之间直接直线相连进行插值计算;
kCAAnimationDiscrete:离散的,就是不进行插值计算,所有关键帧直接逐个进行显示;
kCAAnimationPaced:使得动画均匀进行,而不是按keyTimes设置的或者按关键帧平分时间,此时keyTimes和timingFunctions无效;
kCAAnimationCubic:对关键帧为座标点的关键帧进行圆滑曲线相连后插值计算,对于曲线的形状还可以通过tensionValues,continuityValues,biasValues来进行调整自定义,这里的数学原理是Kochanek–Bartels spline,这里的主要目的是使得运行的轨迹变得圆滑;
kCAAnimationCubicPaced:看这个名字就知道和kCAAnimationCubic有一定联系,其实就是在kCAAnimationCubic的基础上使得动画运行变得均匀,就是系统时间内运动的距离相同,此时keyTimes以及timingFunctions也是无效的。
最后效果如图:
在这里插入图片描述

2015-01-05 03:02:50 gnsydss 阅读数 246

CAAnimationGroup 同时进行多个动画

_animateLayer.backgroundColor = [UIColor colorWithWhite:1.000 alpha:0.250].CGColor;
_animateLayer.borderWidth = 2;
_animateLayer.borderColor = [UIColor  grayColor].CGColor;
CAAnimationGroup *group = [CAAnimationGroup animation];
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"bounds"];
animation.fromValue = [NSValue valueWithCGRect:_animateLayer.bounds];
animation.toValue   = [NSValue valueWithCGRect: CGRectMake(0, 0, _animateLayer.bounds.size.width + 130,_animateLayer.bounds.size.height + 130)];

CABasicAnimation *bgAnimation = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
bgAnimation.fromValue = (__bridge id)([UIColor colorWithWhite:1.000 alpha:0.250].CGColor);
bgAnimation.toValue   = (__bridge id)([UIColor colorWithWhite:1.000 alpha:0].CGColor);

CABasicAnimation *borderAnimation = [CABasicAnimation animationWithKeyPath:@"borderColor"];
borderAnimation.fromValue = (__bridge  id)([UIColor grayColor].CGColor);
borderAnimation.toValue = (__bridge  id)([UIColor colorWithWhite:0.500 alpha:0.000].CGColor);

CABasicAnimation *cornerAnimation = [CABasicAnimation animationWithKeyPath:@"cornerRadius"];
cornerAnimation.fromValue = @(50);
cornerAnimation.toValue   = @(115);

group.animations = @[animation,bgAnimation,borderAnimation,cornerAnimation];
group.repeatCount = MAXFLOAT;
group.duration = 1;
[_animateLayer addAnimation:group forKey:nil];

水!!
我厂广招各路大神加入:job.koudaitong.com
可以发简历到 tianchi@qima-inc.com O(∩_∩)O~

2018-04-26 14:31:40 u011315300 阅读数 2297

let layer = CAGradientLayer()


layer.frame = view.bounds

///设置颜色

layer.colors = [color("61ccc8").cgColor,color("61ccc8").cgColor,kRGBColorAlpha(r: 97, g: 204, b: 200, alpha: 0.1).cgColor]

///设置颜色渐变的位置 (我这里是横向 中间点开始变化)

layer.locations = [0,0.5,1]

///开始的坐标点

layer.startPoint = CGPoint(x: 0, y: 0)

///结束的坐标点

layer.endPoint = CGPoint(x: 1, y: 0)
view.layer.addSublayer(layer)

2016-03-07 10:12:56 shadowluoluo 阅读数 85
利用系统方法  bool CGColorEqualToColor (CGColorRef color1, CGColorRef color2);
有按钮button1和button2分别取CGColor

   if (CGColorEqualToColor(btn1.titleLabel.textColor.CGColor, btn2.titleLabel.textColor.CGColor)) {

        NSLog(@"两个颜色相等");

    }else{

    NSLog(@"两个颜色不相等");

    }

2011-10-09 22:31:32 zani515 阅读数 23

CGColorRef darkColor = [[UIColor blackColor] colorWithAlphaComponent:.5f].CGColor;
	CGColorRef lightColor = [UIColor clearColor].CGColor;
	
	//Footer shadow
	UIView *footerShadow = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 10)];
	
	CAGradientLayer *bottomShadow = [[[CAGradientLayer alloc] init] autorelease];
	bottomShadow.frame = CGRectMake(0,0, self.view.frame.size.width, 10);
	bottomShadow.colors = [NSArray arrayWithObjects:(id)darkColor, (id)lightColor, nil];
	footerShadow.alpha = 0.6;
	
	[footerShadow.layer addSublayer:bottomShadow];
	tb.tableFooterView = footerShadow;
	
	//Header shadow
	UIView *headerShadow = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 10)];
	
	CAGradientLayer *topShadow = [[[CAGradientLayer alloc] init] autorelease];
	topShadow.frame = CGRectMake(0, 0, self.view.frame.size.width, 10);
	topShadow.colors = [NSArray arrayWithObjects:(id)lightColor, (id)darkColor, nil];
	headerShadow.alpha = 0.3;
	
	[headerShadow.layer addSublayer:topShadow];
	tb.tableHeaderView = headerShadow;
	
	tb.contentInset = UIEdgeInsetsMake(-10, 0, 0, 0);

 

The problem is that this work only for plain style UITableViews that do not use thetableHeaderViews and tableFooterViews for anything else. If the view is using those (which it definitely is in case it is a grouped UITableView) do the following:

//replace
self.tableView.tableHeaderView = headerShadow;
//with
[self.tableView insertSubview:headerShadow aboveSubview:self.tableView.tableHeaderView];

//and
self.tableView.tableFooterView = footerShadow;
//with
[self.tableView insertSubview:footerShadow belowSubview:self.tableView.tableFooterView];
 

OS X中的动画

阅读数 286

给btn设置边框

阅读数 262

iOS 核心动画2

阅读数 167

没有更多推荐了,返回首页