前段時(shí)間看了些動(dòng)畫方面的文章饭玲,發(fā)現(xiàn)一個(gè)很不錯(cuò)的教程:iOS-Core-Animation-Advanced-Techniques,看會(huì)這個(gè)動(dòng)畫方面基本上差不多了乘粒,剩下的就是舉一反三了。
疏于原先沒(méi)有太過(guò)關(guān)注Facebook的POP動(dòng)畫引擎,做的效果這么好,于是就研究了下赶熟,找到了些幾篇很好的文章:
POP使用指南:
http://geeklu.com/2014/05/facebook-pop-usage/
很好的POP示例教程:
https://github.com/schneiderandre/popping
https://github.com/kejinlu/facebook-pop-sample
上面都是Objective-C寫的,況且Swift出來(lái)已經(jīng)一年了陷嘴,由于公司項(xiàng)目一直使用OC映砖,Swift也得加強(qiáng)學(xué)習(xí),故以后做Demo就用Swift了灾挨。
OK邑退,那就仿Popping,用Swift寫一個(gè)POP動(dòng)畫:SwiftPopping。效果如下:
0.橋接
POP動(dòng)畫引擎是用Objective-C寫的劳澄,想要用在Swift項(xiàng)目中地技,需要用到橋接。
開(kāi)始橋接項(xiàng)目:前提是創(chuàng)建了SwiftPopping項(xiàng)目并且添加了POP秒拔。
第一步:創(chuàng)建橋接文件:SwiftPopping-Bridging-Header.h
第二步:在SwiftPopping-Bridging-Header.h文件中寫入:#import <pop/POP.h>
第三步:詳見(jiàn)下圖:
需要注意的地方就是1中 SwiftPopping-Bridging-Header.h
所在項(xiàng)目的位置莫矗。
OK,編譯運(yùn)行,成功!
如果想在Objective-C項(xiàng)目中調(diào)用Swift代碼作谚,可以參考下面的文章:
http://www.shimingwei.com/iOS/OC_Use_Swift.html
1.創(chuàng)建按鈕FlatButton
由于我們需要自定義UIButton的一些效果(點(diǎn)進(jìn)去縮放三娩、出來(lái)還原效果),故創(chuàng)建一個(gè)FlatButton妹懒,代碼如下:
import UIKit
class FlatButton: UIButton {
//標(biāo)題居上下左右間距
override var titleEdgeInsets: UIEdgeInsets{
get{
return UIEdgeInsetsMake(4.0,28.0,4.0,28.0)
}
set{
self.titleEdgeInsets = newValue
}
}
class func button() -> FlatButton{
var button = buttonWithType(UIButtonType.Custom) as! FlatButton
return button
}
override init(frame: CGRect) {
super.init(frame: frame)
setup()//設(shè)置按鈕
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
//內(nèi)容大小
override func intrinsicContentSize() -> CGSize {
var size = super.intrinsicContentSize()
return CGSizeMake(size.width + self.titleEdgeInsets.left + self.titleEdgeInsets.right,
size.height + self.titleEdgeInsets.top + self.titleEdgeInsets.bottom)
}
/**
設(shè)置
*/
func setup(){
self.backgroundColor = self.tintColor;
self.layer.cornerRadius = 4.0
self.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
self.titleLabel?.font = UIFont(name: "Avenir-Medium", size: 22)
//添加事件
self.addTarget(self, action: "scaleToSmall", forControlEvents: UIControlEvents.TouchDown | UIControlEvents.TouchDragEnter)
self.addTarget(self, action: "scaleAnimation", forControlEvents: UIControlEvents.TouchUpInside)
self.addTarget(self, action: "scaleToDefault", forControlEvents: UIControlEvents.TouchDragExit)
}
//縮小
func scaleToSmall(){
var scaleAnimation = POPBasicAnimation(propertyNamed: kPOPLayerScaleXY)
scaleAnimation.toValue = NSValue(CGPoint:CGPointMake(0.95, 0.95))
self.layer.pop_addAnimation(scaleAnimation, forKey: "scaleToSmallAnimation")
}
//還原
func scaleToDefault(){
var scaleAnimation = POPBasicAnimation(propertyNamed: kPOPLayerScaleXY)
scaleAnimation.toValue = NSValue(CGPoint:CGPointMake(1, 1))
self.layer.pop_addAnimation(scaleAnimation, forKey: "scaleToDefaultAnimation")
}
//動(dòng)畫
func scaleAnimation(){
var scaleAnimation = POPSpringAnimation(propertyNamed: kPOPLayerScaleXY)
scaleAnimation.toValue = NSValue(CGPoint: CGPointMake(1.0, 1.0))
scaleAnimation.velocity = NSValue(CGPoint:CGPointMake(3.0, 3.0))
scaleAnimation.springBounciness = 18.0
self.layer.pop_addAnimation(scaleAnimation, forKey: "scaleAnimation")
}
}
2.創(chuàng)建ButtonViewController
用于實(shí)現(xiàn)我們Gif圖的效果雀监。
import UIKit
class ButtonViewController: UIViewController {
var flatButton = FlatButton.button()//按鈕
var errorLabel = UILabel()//錯(cuò)誤標(biāo)簽
var activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)//導(dǎo)航欄右側(cè)Item
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
addButton()//添加按鈕
addLabel()//添加標(biāo)簽
addActivityIndicatorView()//添加活動(dòng)指示視圖
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
errorLabel.layer.transform = CATransform3DMakeScale(0.5, 0.5, 1)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
/**
添加按鈕
*/
func addButton(){
flatButton.backgroundColor = UIColor(red: 52/255.0, green: 152/255.0, blue: 219/255.0, alpha: 1.0)
flatButton.setTitle("Log in", forState: UIControlState.Normal)
flatButton.setTranslatesAutoresizingMaskIntoConstraints(false)
flatButton.addTarget(self, action: "touchUpInside:", forControlEvents: UIControlEvents.TouchUpInside)
view.addSubview(flatButton)
//添加約束
self.view.addConstraint(NSLayoutConstraint(item: flatButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0.0))
self.view.addConstraint(NSLayoutConstraint(item: flatButton, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0.0))
}
/**
添加標(biāo)簽
*/
func addLabel(){
errorLabel.font = UIFont(name: "Avenir-Light", size: 18)
errorLabel.textColor = UIColor(red: 231/255.0, green: 76/255.0, blue: 60/255.0, alpha: 1.0)
errorLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
errorLabel.text = "Just a serious login error."
errorLabel.textAlignment = NSTextAlignment.Center
view.insertSubview(errorLabel, belowSubview: flatButton)
//添加約束
self.view.addConstraint(NSLayoutConstraint(item: errorLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: flatButton, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0.0))
self.view.addConstraint(NSLayoutConstraint(item: errorLabel, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: flatButton, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0.0))
errorLabel.layer.transform = CATransform3DMakeScale(0.5, 0.5, 1)
}
/**
添加活動(dòng)指示圖
*/
func addActivityIndicatorView(){
activityIndicatorView.hidesWhenStopped = true
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: activityIndicatorView)
}
/**
按鈕點(diǎn)擊時(shí)
:param: button 按鈕
*/
func touchUpInside(button: FlatButton){
button.userInteractionEnabled = false
activityIndicatorView.startAnimating()
hiddenErrorLabel()
let delayTime = dispatch_time(DISPATCH_TIME_NOW,
Int64(1 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
self.shakeFlatButton()
self.showErrorLabel()
self.activityIndicatorView.stopAnimating()
}
}
/**
搖晃按鈕
*/
func shakeFlatButton(){
var shakeSpringAnimation = POPSpringAnimation(propertyNamed: kPOPLayerPositionX)
shakeSpringAnimation.velocity = NSNumber(float: 2000.0)
shakeSpringAnimation.springBounciness = 20.0
shakeSpringAnimation.completionBlock = {(animation, finished) in
self.flatButton.userInteractionEnabled = true
}
flatButton.layer.pop_addAnimation(shakeSpringAnimation, forKey: "shakeSpringAnimation")
}
/**
顯示錯(cuò)誤提示標(biāo)簽
*/
func showErrorLabel(){
//縮放
var scaleSpringAnimation = POPSpringAnimation(propertyNamed: kPOPLayerScaleXY)
scaleSpringAnimation.toValue = NSValue(CGPoint: CGPointMake(1, 1))
scaleSpringAnimation.velocity = NSValue(CGPoint: CGPointMake(3, 3))
scaleSpringAnimation.springBounciness = 20.0
errorLabel.layer.pop_addAnimation(scaleSpringAnimation, forKey: "scaleSpringAnimation")
//position Y
var positionSpringAnimation = POPSpringAnimation(propertyNamed: kPOPLayerPositionY)
positionSpringAnimation.toValue = NSNumber(float: Float(flatButton.layer.position.y + flatButton.intrinsicContentSize().height))
positionSpringAnimation.springBounciness = 20.0
errorLabel.layer.pop_addAnimation(positionSpringAnimation, forKey: "positionSpringAnimation")
}
/**
隱藏錯(cuò)誤提示標(biāo)簽
*/
func hiddenErrorLabel(){
//縮放
var scaleSpringAnimation = POPSpringAnimation(propertyNamed: kPOPLayerScaleXY)
scaleSpringAnimation.toValue = NSValue(CGPoint: CGPointMake(0.5, 0.5))
scaleSpringAnimation.velocity = NSValue(CGPoint: CGPointMake(3, 3))
scaleSpringAnimation.springBounciness = 20.0
errorLabel.layer.pop_addAnimation(scaleSpringAnimation, forKey: "scaleSpringAnimation")
//position Y
var positionSpringAnimation = POPSpringAnimation(propertyNamed: kPOPLayerPositionY)
positionSpringAnimation.toValue = NSNumber(float: Float(flatButton.layer.position.y))
positionSpringAnimation.springBounciness = 20.0
errorLabel.layer.pop_addAnimation(positionSpringAnimation, forKey: "positionSpringAnimation")
}
}