<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">iphone 中的滑动条解锁一直是一个经典 而自己也在学CALayer 所以,写了这篇文章,虽然很多都是模仿着别人来写的,但还是自己有所收获的</span>
首先是在main,storyboard中拉进一个view和label 并和viewcontroller进行关联,
随后创建CAGrandtlayer
let grandientLayer = CAGrandientLayer()
随后设置grandient的属性
gradientLayer.bounds = CGRectMake(0, 0, backGroundView.frame.size.width, backGroundView.frame.size.height)
gradientLayer.position = CGPoint(x: backGroundView.frame.size.width/2, y: backGroundView.frame.size.height/2)
//绘制颜色渐变的起始点
gradientLayer.startPoint = CGPoint(x: 0, y: 0.5)
gradientLayer.endPoint = CGPoint(x: 1, y: 0.5)
//设置渐变颜色
gradientLayer.colors = [
UIColor.blackColor().CGColor,
UIColor.whiteColor().CGColor,
UIColor.blackColor().CGColor
]
//对颜色出现的位置做出
gradientLayer.locations = [0.2,0.5,0.8]
随后再加载到backgroundView中
<pre name="code" class="plain"> backGroundView.layer.addSublayer(gradientLayer)
让颜色也要渐变起来
let gradientLayer = CAGradientLayer()
<pre name="code" class="plain"> gradient.fromValue = [0,0,0.25]
gradient.toValue = [0.75,1,1]
gradient.duration = 2.5
gradient.repeatCount = HUGE
gradientLayer.addAnimation(gradient, forKey: nil)
整体代码如下
<pre name="code" class="plain">//
// ViewController.swift
// shiyan31
//
// Created by HISE_CS on 15/10/12.
// Copyright 2015年 HISE_CS. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
var text = "nihao"
@IBOutlet var textLabel: UILabel!
@IBOutlet var backGroundView: UIView!
//在layer上绘制出渐变颜色的效果
let gradientLayer = CAGradientLayer()
override func viewDidLoad() {
textLabel.text = text
gradientLayer.mask = textLabel.layer
gradientLayer.bounds = CGRectMake(0, 0, backGroundView.frame.size.width, backGroundView.frame.size.height)
gradientLayer.position = CGPoint(x: backGroundView.frame.size.width/2, y: backGroundView.frame.size.height/2)
//绘制颜色渐变的起始点
gradientLayer.startPoint = CGPoint(x: 0, y: 0.5)
gradientLayer.endPoint = CGPoint(x: 1, y: 0.5)
//设置渐变颜色
gradientLayer.colors = [
UIColor.blackColor().CGColor,
UIColor.whiteColor().CGColor,
UIColor.blackColor().CGColor
]
//对颜色出现的位置做出
gradientLayer.locations = [0.2,0.5,0.8]
//在层中添加
backGroundView.layer.addSublayer(gradientLayer)
gradient.fromValue = [0,0,0.25]
gradient.toValue = [0.75,1,1]
gradient.duration = 2.5
gradient.repeatCount = HUGE
gradientLayer.addAnimation(gradient, forKey: nil)
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//让颜色渐变动起来(location类型的动画)
let gradient = CABasicAnimation(keyPath: "location")
}
具体看这个链接
http://www.csdn.net/article/2015-09-22/2825765