//
// ViewController.swift
// ios核心动画编程
//
// Created by Macx on 18/9/16.
// Copyright (c) 2018年 learn. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
var mylayer = CALayer()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.testLayerProperty()
}
@IBAction func start(sender: AnyObject) {
self.caBasicAnimate()
}
func testLayerProperty(){
mylayer = CALayer()
mylayer.backgroundColor = UIColor.magentaColor().CGColor
mylayer.borderColor = UIColor.yellowColor().CGColor
mylayer.borderWidth = 3.0
mylayer.cornerRadius = 10.0
mylayer.shadowColor = UIColor.whiteColor().CGColor
mylayer.shadowOffset = CGSizeMake(20.0, 20.0)
mylayer.shadowOpacity = 0.2
mylayer.frame = CGRectMake(100, 100, 100, 100)
self.view.layer.addSublayer(mylayer)
}
//隐式动画
func implicitAnimation(){
mylayer.position.y += 100
}
//显示动画
func caBasicAnimate(){
var imgview = UIImageView(image: UIImage(named: "1.jpeg"))
imgview.frame = CGRectMake(150, 200, 100, 100)
self.view.addSubview(imgview)
//选定角色
var la = imgview.layer
//写剧本1
var scaleAni = CABasicAnimation(keyPath: "transform.scale")
scaleAni.fromValue = 1.0
scaleAni.toValue = 2.0
scaleAni.fillMode = kCAFillModeForwards
scaleAni.repeatCount = MAXFLOAT
scaleAni.autoreverses = true
scaleAni.duration = 2.0
//写剧本2
var opacityAni = CABasicAnimation(keyPath: "opacitty")
opacityAni.fromValue = 0.3
opacityAni.toValue = 1.0
opacityAni.fillMode = kCAFillModeBackwards
opacityAni.duration = 1.0
opacityAni.autoreverses = true
opacityAni.repeatCount = MAXFLOAT
//写剧本3
var keyAnimate = CAKeyframeAnimation(keyPath: "position")
var value0 = NSValue(CGPoint: la.position)
var value1 = NSValue(CGPoint: CGPointMake(la.position.x, la.position.y + 200))
var value2 = NSValue(CGPoint: CGPointMake(la.position.x - 150, la.position.y + 200))
var value3 = NSValue(CGPoint: CGPointMake(la.position.x - 150, la.position.y + 200))
var value4 = NSValue(CGPoint: la.position)
keyAnimate.values = [value0,value1,value2,value3,value4]
//每一段执行时间
keyAnimate.keyTimes = [0,0.4,0.5,0.6,1]
keyAnimate.autoreverses = false
keyAnimate.repeatCount = MAXFLOAT
keyAnimate.duration = 7
la.addAnimation(scaleAni, forKey: "group")
la.addAnimation(keyAnimate, forKey: "keyAnimate")
//la.addAnimation(opacityAni, forKey: "opa")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}