【Swift】ニョキっとでるSNSボタン

#はじめに
久しぶりにSwiftを触ったので、プラスボタンを押すとTwitterとFacebookに投稿するボタンが出てくるのを作りました(∩・∀・)∩

~~GIF作ったけど動かないので後でYouTubeにアップロードしときます。~~

YouTubeにあげときました(｡・∀・)ﾉ↓
[動画：【Swift】ニョキっとでるSNSボタン ](http://youtu.be/WufGIrpmGTE)


全てコードで書いているので画像さえ準備していただければ流用できます。
画像は[flaticon](http://www.flaticon.com/)というサイトからダウンロードしたものを加工して使ってます。

#ソースコード

```swift:ViewController.swift
import UIKit
import Social

class ViewController: UIViewController {
    let myButton:UIButton = UIButton()
    
    let icon_sizeR:CGFloat = 60
    
    let sns_Twitter_uiview:UIImageView = UIImageView()
    let sns_Facebook_uiview:UIImageView = UIImageView()
    
    var number = 0
    
    var image_add = UIImage(named: "add.png") as UIImage?
    var image_cancel = UIImage(named: "minus.png") as UIImage?
    var twitter_image = UIImage(named: "twitter.png") as UIImage?
    var facebook_image = UIImage(named: "facebook.png") as UIImage?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.blueColor()
        
        sns_Twitter_uiview.image = twitter_image
        sns_Twitter_uiview.backgroundColor = UIColor.whiteColor()
        sns_Twitter_uiview.frame = CGRectMake(0, 0, icon_sizeR, icon_sizeR)
        sns_Twitter_uiview.layer.masksToBounds = true
        sns_Twitter_uiview.layer.cornerRadius = icon_sizeR*0.5
        sns_Twitter_uiview.center = CGPointMake(self.view.frame.width/2, self.view.frame.height - 50)
        sns_Twitter_uiview.userInteractionEnabled = true
        sns_Twitter_uiview.tag = 1
        
        sns_Facebook_uiview.image = facebook_image
        sns_Facebook_uiview.backgroundColor = UIColor.whiteColor()
        sns_Facebook_uiview.frame = CGRectMake(0, 0, icon_sizeR, icon_sizeR)
        sns_Facebook_uiview.layer.masksToBounds = true
        sns_Facebook_uiview.layer.cornerRadius = icon_sizeR*0.5
        sns_Facebook_uiview.center = CGPointMake(self.view.frame.width/2, self.view.frame.height - 50)
        sns_Facebook_uiview.userInteractionEnabled = true
        sns_Facebook_uiview.tag = 2
        self.view.addSubview(sns_Twitter_uiview)
        self.view.addSubview(sns_Facebook_uiview)
        
        myButton.frame = CGRectMake(0,0,icon_sizeR,icon_sizeR)
        myButton.backgroundColor = UIColor.whiteColor();
        myButton.layer.masksToBounds = true
        myButton.setImage(image_add, forState: UIControlState.Normal)
        myButton.layer.cornerRadius = icon_sizeR*0.5
        myButton.layer.position = CGPoint(x: self.view.frame.width/2, y:self.view.frame.height - 50)
        myButton.addTarget(self, action: Selector("buttonWasTapped"), forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(myButton)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    func buttonWasTapped(){
        UIView.beginAnimations(nil, context: nil)
        UIView.setAnimationDuration(0.2)
        UIView.setAnimationDelay(0.0)
        UIView.setAnimationRepeatCount(1)
        UIView.setAnimationCurve(UIViewAnimationCurve.Linear)
        if((number%2)==0){
            icon_animationUp()
        }else{
            icon_animationDown()
        }
        UIView.commitAnimations()
        
        number++
    }
    
    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        for touch:AnyObject in touches{
            var t:UITouch = touch as UITouch
            switch t.view.tag{
            case 1:
                var twitterVC = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
                presentViewController(twitterVC, animated: true, completion: nil)
                icon_animationDown_whenSend()
                number++
                break
            case 2:
                var facebookVC = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
                presentViewController(facebookVC, animated: true, completion: nil)
                icon_animationDown_whenSend()
                number++
            default:
                break
            }
        }
    }
    
    func icon_animationUp(){
        sns_Twitter_uiview.center = CGPointMake(self.view.frame.width/2, self.view.frame.height-50-icon_sizeR*1.25*2)
        sns_Facebook_uiview.center = CGPointMake(self.view.frame.width/2, self.view.frame.height-50-icon_sizeR*1.25)
        myButton.setImage(image_cancel, forState: UIControlState.Normal)
    }
    
    func icon_animationDown(){
        sns_Twitter_uiview.center = CGPointMake(self.view.frame.width/2, self.view.frame.height-50)
        sns_Facebook_uiview.center = CGPointMake(self.view.frame.width/2, self.view.frame.height-50)
        myButton.setImage(image_add, forState: UIControlState.Normal)
    }
    
    func icon_animationDown_whenSend(){
        UIView.beginAnimations(nil, context: nil)
        UIView.setAnimationDuration(0.2)
        UIView.setAnimationDelay(0.0)
        UIView.setAnimationRepeatCount(1)
        UIView.setAnimationCurve(UIViewAnimationCurve.Linear)
        icon_animationDown()
        UIView.commitAnimations()
    }
}
```

#感想
最近ほとんどSwiftもObjective-Cも触ってなかったので少し作るのに時間かかりました( º言º` )
ただ、あ〜そっか...っと復習になることも多くて勉強にはなりました。もっと簡単に書こうと思えば書けるかもしれませんが、一応今回はやりたいことを全て実装できたのでこれで。