ios学习之CALAyerAnimation 的iphone经典滑动解锁动画

<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



 
 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值