//
// ViewController.swift
// MapAnimation
//
// Created by su on 15/12/10.
// Copyright © 2015年 tian. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
var appBackGround:UIImageView!
var mapView: UIImageView!
var mapShowing:Bool = false
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.blackColor()
//构建第一视图
appBackGround = UIImageView(frame: CGRectMake(0, 64, self.view.frame.size.width,458))
appBackGround.image = UIImage(named: "app-bg")
self.view.addSubview(appBackGround)
//构建地图视图
mapView = UIImageView(frame: CGRectMake(0, 132, self.view.frame.size.width,458))
mapView.image = UIImage(named: "map-arrow")
mapView.alpha = 0
CGAffineTransformScale(mapView.transform, 1.1, 1.1)
self.view.addSubview(mapView)
//构建执行动画的button
let right = UIBarButtonItem(image: UIImage(named: "map-icon"), style: UIBarButtonItemStyle.Plain, target: self, action: "right")
self.navigationItem.rightBarButtonItem = right
}
func right() {
//显示地图的阻尼值是16 隐藏地图的阻尼值24
let damplingStiffness:CGFloat = 8.0
let daplingStiffnessOut:CGFloat = 12
if mapShowing {
mapShowing = false
//隐藏地图
UIView.animateWithDuration(0.1, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
self.appBackGround.alpha = 1
}, completion: nil)
let jnw缩放 = JNWSpringAnimation(keyPath: "transform.scale")
jnw缩放.damping = damplingStiffness
jnw缩放.stiffness = damplingStiffness
jnw缩放.mass = 1
//将尺寸从0.9 还原成1
jnw缩放.fromValue = 0.9
jnw缩放.toValue = 1
appBackGround.layer.addAnimation(jnw缩放, forKey: jnw缩放.keyPath)
appBackGround.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0)
//地图视图的动画代码
UIView.animateWithDuration(0.1, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
self.mapView.alpha = 0
}, completion: nil)
let jnw地图缩放 = JNWSpringAnimation(keyPath: "transform.scale")
jnw地图缩放.damping = daplingStiffnessOut
jnw地图缩放.stiffness = daplingStiffnessOut
jnw地图缩放.mass = 1
jnw地图缩放.fromValue = 1.1
jnw地图缩放.toValue = 1
self.mapView.layer.addAnimation(jnw地图缩放, forKey: jnw地图缩放.keyPath)
self.mapView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1, 1)
//位移jnw对象
let jnw地图下移 = JNWSpringAnimation(keyPath: "transform.translation.y")
jnw地图下移.damping = daplingStiffnessOut
jnw地图下移.stiffness = daplingStiffnessOut
jnw地图下移.mass = 1
jnw地图下移.fromValue = 0
jnw地图下移.toValue = 30
self.mapView.layer.addAnimation(jnw地图下移, forKey: jnw地图下移.keyPath)
self.mapView.transform = CGAffineTransformTranslate(self.mapView.transform, 0, 0)
} else {
//显示地图
mapShowing = true
//第一视图的动画代码
UIView.animateWithDuration(0.1, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
self.appBackGround.alpha = 0.3
}, completion: nil)
let scale = JNWSpringAnimation(keyPath: "transform.scale")
scale.damping = damplingStiffness
scale.stiffness = damplingStiffness
scale.mass = 1
//初始值取得当前背景的缩放值
scale.fromValue = 1
scale.toValue = 0.9
self.appBackGround.layer.addAnimation(scale, forKey: scale.keyPath)
self.appBackGround.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9)
//地图动画代码
UIView.animateWithDuration(0.1, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
self.mapView.alpha = 1
}, completion: nil)
//利用jnw框架组合两个动画
let mapScale = JNWSpringAnimation(keyPath: "transform.scale")
mapScale.damping = daplingStiffnessOut
mapScale.stiffness = daplingStiffnessOut
mapScale.mass = 1
mapScale.fromValue = 1
mapScale.toValue = 1.1
self.mapView.layer.addAnimation(mapScale, forKey: mapScale.keyPath)
self.mapView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1)
//位移动画
let mapTranslate = JNWSpringAnimation(keyPath: "transform.translation.y")
mapTranslate.damping = daplingStiffnessOut
mapTranslate.stiffness = daplingStiffnessOut
mapTranslate.mass = 1
mapTranslate.fromValue = 30
mapTranslate.toValue = 0
self.mapView.layer.addAnimation(mapTranslate, forKey: mapTranslate.keyPath)
self.mapView.transform = CGAffineTransformTranslate(self.mapView.transform, 0, 0)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
// ViewController.swift
// MapAnimation
//
// Created by su on 15/12/10.
// Copyright © 2015年 tian. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
var appBackGround:UIImageView!
var mapView: UIImageView!
var mapShowing:Bool = false
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.blackColor()
//构建第一视图
appBackGround = UIImageView(frame: CGRectMake(0, 64, self.view.frame.size.width,458))
appBackGround.image = UIImage(named: "app-bg")
self.view.addSubview(appBackGround)
//构建地图视图
mapView = UIImageView(frame: CGRectMake(0, 132, self.view.frame.size.width,458))
mapView.image = UIImage(named: "map-arrow")
mapView.alpha = 0
CGAffineTransformScale(mapView.transform, 1.1, 1.1)
self.view.addSubview(mapView)
//构建执行动画的button
let right = UIBarButtonItem(image: UIImage(named: "map-icon"), style: UIBarButtonItemStyle.Plain, target: self, action: "right")
self.navigationItem.rightBarButtonItem = right
}
func right() {
//显示地图的阻尼值是16 隐藏地图的阻尼值24
let damplingStiffness:CGFloat = 8.0
let daplingStiffnessOut:CGFloat = 12
if mapShowing {
mapShowing = false
//隐藏地图
UIView.animateWithDuration(0.1, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
self.appBackGround.alpha = 1
}, completion: nil)
let jnw缩放 = JNWSpringAnimation(keyPath: "transform.scale")
jnw缩放.damping = damplingStiffness
jnw缩放.stiffness = damplingStiffness
jnw缩放.mass = 1
//将尺寸从0.9 还原成1
jnw缩放.fromValue = 0.9
jnw缩放.toValue = 1
appBackGround.layer.addAnimation(jnw缩放, forKey: jnw缩放.keyPath)
appBackGround.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0)
//地图视图的动画代码
UIView.animateWithDuration(0.1, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
self.mapView.alpha = 0
}, completion: nil)
let jnw地图缩放 = JNWSpringAnimation(keyPath: "transform.scale")
jnw地图缩放.damping = daplingStiffnessOut
jnw地图缩放.stiffness = daplingStiffnessOut
jnw地图缩放.mass = 1
jnw地图缩放.fromValue = 1.1
jnw地图缩放.toValue = 1
self.mapView.layer.addAnimation(jnw地图缩放, forKey: jnw地图缩放.keyPath)
self.mapView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1, 1)
//位移jnw对象
let jnw地图下移 = JNWSpringAnimation(keyPath: "transform.translation.y")
jnw地图下移.damping = daplingStiffnessOut
jnw地图下移.stiffness = daplingStiffnessOut
jnw地图下移.mass = 1
jnw地图下移.fromValue = 0
jnw地图下移.toValue = 30
self.mapView.layer.addAnimation(jnw地图下移, forKey: jnw地图下移.keyPath)
self.mapView.transform = CGAffineTransformTranslate(self.mapView.transform, 0, 0)
} else {
//显示地图
mapShowing = true
//第一视图的动画代码
UIView.animateWithDuration(0.1, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
self.appBackGround.alpha = 0.3
}, completion: nil)
let scale = JNWSpringAnimation(keyPath: "transform.scale")
scale.damping = damplingStiffness
scale.stiffness = damplingStiffness
scale.mass = 1
//初始值取得当前背景的缩放值
scale.fromValue = 1
scale.toValue = 0.9
self.appBackGround.layer.addAnimation(scale, forKey: scale.keyPath)
self.appBackGround.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9)
//地图动画代码
UIView.animateWithDuration(0.1, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
self.mapView.alpha = 1
}, completion: nil)
//利用jnw框架组合两个动画
let mapScale = JNWSpringAnimation(keyPath: "transform.scale")
mapScale.damping = daplingStiffnessOut
mapScale.stiffness = daplingStiffnessOut
mapScale.mass = 1
mapScale.fromValue = 1
mapScale.toValue = 1.1
self.mapView.layer.addAnimation(mapScale, forKey: mapScale.keyPath)
self.mapView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1)
//位移动画
let mapTranslate = JNWSpringAnimation(keyPath: "transform.translation.y")
mapTranslate.damping = daplingStiffnessOut
mapTranslate.stiffness = daplingStiffnessOut
mapTranslate.mass = 1
mapTranslate.fromValue = 30
mapTranslate.toValue = 0
self.mapView.layer.addAnimation(mapTranslate, forKey: mapTranslate.keyPath)
self.mapView.transform = CGAffineTransformTranslate(self.mapView.transform, 0, 0)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}