车小弟模仿

需要注意的是几个动画对象的同时使用
//
//  ViewController.swift
//  车小弟
//
//  Created by admin on 16/1/28.
//  Copyright © 2016年 jin. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var image: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
        for var i = 1;i < 4;i++
        {
            let button = UIButton(type: UIButtonType.Custom)
//            button.frame = CGRectMake(-153, -153, 612, 612)
            // 勾选了use autolayout这里就显示不正常,就需要用上面的CGRect,但是取消之后再勾选就又正常了。。。。
            button.frame = self.image.bounds
            button.setImage(UIImage(imageLiteral: "circle\(i)"), forState: UIControlState.Normal)
            self.image.addSubview(button)
        }
        let image = UIImage(imageLiteral: "home_btn_dealer_had_bind")
        let button = UIButton(type: UIButtonType.Custom)
        button.bounds = CGRectMake(0, 0, image.size.width, image.size.height)
        button.center = self.image.center
        button.setImage(image, forState: UIControlState.Normal)
        button.addTarget(self, action: "changeImage", forControlEvents: UIControlEvents.TouchDown)
        self.view.addSubview(button)
    }
    func changeImage()
    {
        if self.image.layer.animationForKey("changeImage") != nil
        {
            return
        }
        let animationGroup = CAAnimationGroup()
        // 透明度
        let fade = CABasicAnimation(keyPath: "opacity")
        // 缩放
        let scale = CAKeyframeAnimation(keyPath: "transform.scale")
        // 旋转
        let rotation = CABasicAnimation(keyPath: "transform.rotation")
        // 做不同的操作
        if self.image.layer.opacity == 0
        {
            self.image.layer.opacity = 1
            // 设置路径
            scale.values = [0,1.2,1.0]
            rotation.fromValue = -CGFloat(M_PI * 0.25)
            rotation.toValue = 0
        }
        else
        {
            self.image.layer.opacity = 0
            scale.values = [1.0,1.2,0]
            rotation.fromValue = 0
            rotation.toValue = -CGFloat(M_PI * 0.25)
        }
        // 添加进入分组
        animationGroup.animations = [fade,scale,rotation]
        animationGroup.duration = 1
        self.image.layer.addAnimation(animationGroup, forKey: "changeImage")
    }
}

标签: swift, 核心动画

添加新评论