POP 動(dòng)畫

前段時(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。效果如下:

POP1.gif

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)下圖:

POP2.jpg

需要注意的地方就是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")
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市眨唬,隨后出現(xiàn)的幾起案子会前,更是在濱河造成了極大的恐慌,老刑警劉巖单绑,帶你破解...
    沈念sama閱讀 216,372評(píng)論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件回官,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡搂橙,警方通過(guò)查閱死者的電腦和手機(jī)歉提,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)区转,“玉大人苔巨,你說(shuō)我怎么就攤上這事》侠耄” “怎么了侄泽?”我有些...
    開(kāi)封第一講書(shū)人閱讀 162,415評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)蜻韭。 經(jīng)常有香客問(wèn)我悼尾,道長(zhǎng),這世上最難降的妖魔是什么肖方? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,157評(píng)論 1 292
  • 正文 為了忘掉前任闺魏,我火速辦了婚禮,結(jié)果婚禮上俯画,老公的妹妹穿的比我還像新娘析桥。我一直安慰自己,他們只是感情好艰垂,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,171評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布泡仗。 她就那樣靜靜地躺著,像睡著了一般猜憎。 火紅的嫁衣襯著肌膚如雪娩怎。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,125評(píng)論 1 297
  • 那天胰柑,我揣著相機(jī)與錄音截亦,去河邊找鬼辣辫。 笑死,一個(gè)胖子當(dāng)著我的面吹牛魁巩,可吹牛的內(nèi)容都是我干的急灭。 我是一名探鬼主播,決...
    沈念sama閱讀 40,028評(píng)論 3 417
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼谷遂,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼葬馋!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起肾扰,我...
    開(kāi)封第一講書(shū)人閱讀 38,887評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤畴嘶,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后集晚,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體窗悯,經(jīng)...
    沈念sama閱讀 45,310評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,533評(píng)論 2 332
  • 正文 我和宋清朗相戀三年偷拔,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了蒋院。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,690評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡莲绰,死狀恐怖欺旧,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情蛤签,我是刑警寧澤辞友,帶...
    沈念sama閱讀 35,411評(píng)論 5 343
  • 正文 年R本政府宣布,位于F島的核電站震肮,受9級(jí)特大地震影響称龙,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜戳晌,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,004評(píng)論 3 325
  • 文/蒙蒙 一鲫尊、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧躬厌,春花似錦马昨、人聲如沸竞帽。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,659評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)屹篓。三九已至疙渣,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間堆巧,已是汗流浹背妄荔。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,812評(píng)論 1 268
  • 我被黑心中介騙來(lái)泰國(guó)打工泼菌, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人啦租。 一個(gè)月前我還...
    沈念sama閱讀 47,693評(píng)論 2 368
  • 正文 我出身青樓哗伯,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親篷角。 傳聞我的和親對(duì)象是個(gè)殘疾皇子焊刹,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,577評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件恳蹲、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,093評(píng)論 4 62
  • 一虐块、基本dos命令: 1.打開(kāi)dos窗口:windows+r,鍵入cmd 2. 切換磁盤: 磁盤名: 如d: 3...
    簡(jiǎn)臾閱讀 6,836評(píng)論 0 0
  • 一部關(guān)于單車的影片
    七月紫蘇閱讀 99評(píng)論 0 0
  • 城市總會(huì)下雨儡率,風(fēng)會(huì)停,霧會(huì)起以清,情緒也沉入海底喉悴,好幾千米,悶悶的玖媚,難呼吸箕肃。 我們都曾困住自己,自說(shuō)自話今魔,自編自演勺像,陶...
    CalmEsae閱讀 712評(píng)論 0 0