Skip to main content
本文に 48 文字追加
ソース リンク
thirtyseven
  • 409
  • 3
  • 10
import UIKit

class ViewController: UIViewController {

    @IBOutlet var buttons: [keyboardButton]!
    var firstTouchInside: Bool = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view, typically from a nib.
        
        for button: keyboardButton in buttons {
            button.userInteractionEnabled = true
            button.backgroundColor = UIColor.whiteColor()
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        super.touchesBegan(touches, withEvent: event)
        
        let touch: UITouch = touches.anyObject() as UITouch
        switch touch.view.tag {
        case 1...3:
            (touch.view as keyboardButton).touchInside()
            self.firstTouchInside = true
        default:
            self.firstTouchInside = false
        }
    }
    
    override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
        super.touchesMoved(touches, withEvent: event)
        
        if self.firstTouchInside == true {
            let touch: UITouch = touches.anyObject() as UITouch
            let oldLocation: CGPoint = touch.previousLocationInView(self.view)
            let newLocation: CGPoint = touch.locationInView(self.view)
            var oldTag: Int?
            var newTag: Int?
            for button: keyboardButton in buttons {
                if CGRectContainsPoint(button.frame, oldLocation) {
                    oldTag = button.tag
                }
                if CGRectContainsPoint(button.frame, newLocation) {
                    newTag = button.tag
                }
            }
            
            if oldTag != newTag {
                if oldTag != nil {
                    (self.view.viewWithTag(oldTag!) as keyboardButton).touchCancel()
                }
                if newTag != nil {
                    (self.view.viewWithTag(newTag!) as keyboardButton).touchInside()
                }
            }
        }
    }
    
    override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
        super.touchesEnded(touches, withEvent: event)
        
        if self.firstTouchInside == true {
            let touch: UITouch = touches.anyObject() as UITouch
            let location: CGPoint = touch.locationInView(self.view)
            for button: keyboardButton in buttons {
                if CGRectContainsPoint(button.frame, location) {
                    button.touchUpInside()
                }
            }
        }
    }
    
    override func touchesCancelled(touches: NSSet!, withEvent event: UIEvent!) {
        super.touchesCancelled(touches, withEvent: event)
        
        if self.firstTouchInside == true {
            let touch: UITouch = touches.anyObject() as UITouch
            switch touch.view.tag {
            case 1...3:
                (touch.view as keyboardButton).touchCancel()
            default:
                break
            }
        }
    }
}

class keyboardButton: UILabel {
    var originalBackgroundColor: UIColor?
    
    func touchInside() {
        if self.originalBackgroundColor == nil {
            self.originalBackgroundColor = self.backgroundColor
        }
        var hue: CGFloat = 0
        var saturation: CGFloat = 0
        var brightness: CGFloat = 0
        var alpha: CGFloat = 0
        
        self.backgroundColor?.getHue(
            &hue,
            saturation: &saturation,
            brightness: &brightness,
            alpha: &alpha)
        if brightness > 0.5 {
            brightness = brightness - 0.10
        } else {
            brightness = brightness + 0.10
        }
        
        self.backgroundColor = UIColor(
            hue: hue,
            saturation: saturation,
            brightness: brightness,
            alpha: alpha)
    }
    
    func touchCancel() {
        self.backgroundColor = self.originalBackgroundColor
    }
    
    func touchUpInside() {
        UIView.transitionWithView(
            self,
            duration: 0.5,
            options: (UIViewAnimationOptions.AllowUserInteraction | UIViewAnimationOptions.TransitionCrossDissolve),
            animations: {self.backgroundColor = self.originalBackgroundColor},
            completion: nil)
    }
}
import UIKit

class ViewController: UIViewController {

    @IBOutlet var buttons: [keyboardButton]!
    var firstTouchInside: Bool = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view, typically from a nib.
        
        for button: keyboardButton in buttons {
            button.userInteractionEnabled = true
            button.backgroundColor = UIColor.whiteColor()
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        super.touchesBegan(touches, withEvent: event)
        
        let touch: UITouch = touches.anyObject() as UITouch
        switch touch.view.tag {
        case 1...3:
            (touch.view as keyboardButton).touchInside()
            self.firstTouchInside = true
        default:
            self.firstTouchInside = false
        }
    }
    
    override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
        super.touchesMoved(touches, withEvent: event)
        
        if self.firstTouchInside == true {
            let touch: UITouch = touches.anyObject() as UITouch
            let oldLocation: CGPoint = touch.previousLocationInView(self.view)
            let newLocation: CGPoint = touch.locationInView(self.view)
            var oldTag: Int?
            var newTag: Int?
            for button: keyboardButton in buttons {
                if CGRectContainsPoint(button.frame, oldLocation) {
                    oldTag = button.tag
                }
                if CGRectContainsPoint(button.frame, newLocation) {
                    newTag = button.tag
                }
            }
            
            if oldTag != newTag {
                if oldTag != nil {
                    (self.view.viewWithTag(oldTag!) as keyboardButton).touchCancel()
                }
                if newTag != nil {
                    (self.view.viewWithTag(newTag!) as keyboardButton).touchInside()
                }
            }
        }
    }
    
    override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
        super.touchesEnded(touches, withEvent: event)
        
        if self.firstTouchInside == true {
            let touch: UITouch = touches.anyObject() as UITouch
            let location: CGPoint = touch.locationInView(self.view)
            for button: keyboardButton in buttons {
                if CGRectContainsPoint(button.frame, location) {
                    button.touchUpInside()
                }
            }
        }
    }
    
    override func touchesCancelled(touches: NSSet!, withEvent event: UIEvent!) {
        super.touchesCancelled(touches, withEvent: event)
        
        if self.firstTouchInside == true {
            let touch: UITouch = touches.anyObject() as UITouch
            switch touch.view.tag {
            case 1...3:
                (touch.view as keyboardButton).touchCancel()
            default:
                break
            }
        }
    }
}

class keyboardButton: UILabel {
    var originalBackgroundColor: UIColor?
    
    func touchInside() {
        if self.originalBackgroundColor == nil {
            self.originalBackgroundColor = self.backgroundColor
        }
        var hue: CGFloat = 0
        var saturation: CGFloat = 0
        var brightness: CGFloat = 0
        var alpha: CGFloat = 0
        
        self.backgroundColor?.getHue(
            &hue,
            saturation: &saturation,
            brightness: &brightness,
            alpha: &alpha)
        if brightness > 0.5 {
            brightness = brightness - 0.10
        } else {
            brightness = brightness + 0.10
        }
        
        self.backgroundColor = UIColor(
            hue: hue,
            saturation: saturation,
            brightness: brightness,
            alpha: alpha)
    }
    
    func touchCancel() {
        self.backgroundColor = self.originalBackgroundColor
    }
    
    func touchUpInside() {
        UIView.transitionWithView(
            self,
            duration: 0.5,
            options: UIViewAnimationOptions.TransitionCrossDissolve,
            animations: {self.backgroundColor = self.originalBackgroundColor},
            completion: nil)
    }
}
import UIKit

class ViewController: UIViewController {

    @IBOutlet var buttons: [keyboardButton]!
    var firstTouchInside: Bool = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view, typically from a nib.
        
        for button: keyboardButton in buttons {
            button.userInteractionEnabled = true
            button.backgroundColor = UIColor.whiteColor()
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        super.touchesBegan(touches, withEvent: event)
        
        let touch: UITouch = touches.anyObject() as UITouch
        switch touch.view.tag {
        case 1...3:
            (touch.view as keyboardButton).touchInside()
            self.firstTouchInside = true
        default:
            self.firstTouchInside = false
        }
    }
    
    override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
        super.touchesMoved(touches, withEvent: event)
        
        if self.firstTouchInside == true {
            let touch: UITouch = touches.anyObject() as UITouch
            let oldLocation: CGPoint = touch.previousLocationInView(self.view)
            let newLocation: CGPoint = touch.locationInView(self.view)
            var oldTag: Int?
            var newTag: Int?
            for button: keyboardButton in buttons {
                if CGRectContainsPoint(button.frame, oldLocation) {
                    oldTag = button.tag
                }
                if CGRectContainsPoint(button.frame, newLocation) {
                    newTag = button.tag
                }
            }
            
            if oldTag != newTag {
                if oldTag != nil {
                    (self.view.viewWithTag(oldTag!) as keyboardButton).touchCancel()
                }
                if newTag != nil {
                    (self.view.viewWithTag(newTag!) as keyboardButton).touchInside()
                }
            }
        }
    }
    
    override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
        super.touchesEnded(touches, withEvent: event)
        
        if self.firstTouchInside == true {
            let touch: UITouch = touches.anyObject() as UITouch
            let location: CGPoint = touch.locationInView(self.view)
            for button: keyboardButton in buttons {
                if CGRectContainsPoint(button.frame, location) {
                    button.touchUpInside()
                }
            }
        }
    }
    
    override func touchesCancelled(touches: NSSet!, withEvent event: UIEvent!) {
        super.touchesCancelled(touches, withEvent: event)
        
        if self.firstTouchInside == true {
            let touch: UITouch = touches.anyObject() as UITouch
            switch touch.view.tag {
            case 1...3:
                (touch.view as keyboardButton).touchCancel()
            default:
                break
            }
        }
    }
}

class keyboardButton: UILabel {
    var originalBackgroundColor: UIColor?
    
    func touchInside() {
        if self.originalBackgroundColor == nil {
            self.originalBackgroundColor = self.backgroundColor
        }
        var hue: CGFloat = 0
        var saturation: CGFloat = 0
        var brightness: CGFloat = 0
        var alpha: CGFloat = 0
        
        self.backgroundColor?.getHue(
            &hue,
            saturation: &saturation,
            brightness: &brightness,
            alpha: &alpha)
        if brightness > 0.5 {
            brightness = brightness - 0.10
        } else {
            brightness = brightness + 0.10
        }
        
        self.backgroundColor = UIColor(
            hue: hue,
            saturation: saturation,
            brightness: brightness,
            alpha: alpha)
    }
    
    func touchCancel() {
        self.backgroundColor = self.originalBackgroundColor
    }
    
    func touchUpInside() {
        UIView.transitionWithView(
            self,
            duration: 0.5,
            options: (UIViewAnimationOptions.AllowUserInteraction | UIViewAnimationOptions.TransitionCrossDissolve),
            animations: {self.backgroundColor = self.originalBackgroundColor},
            completion: nil)
    }
}
本文から 37 文字削除
ソース リンク
thirtyseven
  • 409
  • 3
  • 10
import UIKit

class ViewController: UIViewController {

    @IBOutlet var buttons: [keyboardButton]!
    var firstTouchInside: Bool = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view, typically from a nib.
        
        for button: keyboardButton in buttons {
            button.userInteractionEnabled = true
            button.backgroundColor = UIColor.whiteColor()
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        super.touchesBegan(touches, withEvent: event)
        
        let touch: UITouch = touches.anyObject() as UITouch
        switch touch.view.tag {
        case 1...3:
            (touch.view as keyboardButton).touchInside()
            self.firstTouchInside = true
        default:
            self.firstTouchInside = false
        }
    }
    
    override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
        super.touchesMoved(touches, withEvent: event)
        
        if self.firstTouchInside == true {
            let touch: UITouch = touches.anyObject() as UITouch
            let oldLocation: CGPoint = touch.previousLocationInView(self.view)
            let newLocation: CGPoint = touch.locationInView(self.view)
            var oldTag: Int?
            var newTag: Int?
            for button: keyboardButton in buttons {
                if CGRectContainsPoint(button.frame, oldLocation) {
                    oldTag = button.tag
                }
                if CGRectContainsPoint(button.frame, newLocation) {
                    newTag = button.tag
                }
            }
            
            if oldTag != newTag {
                if oldTag != nil {
                    (self.view.viewWithTag(oldTag!) as keyboardButton).touchCancel()
                }
                if newTag != nil {
                    (self.view.viewWithTag(newTag!) as keyboardButton).touchInside()
                }
            }
        }
    }
    
    override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
        super.touchesEnded(touches, withEvent: event)
        
        if self.firstTouchInside == true {
            let touch: UITouch = touches.anyObject() as UITouch
            let location: CGPoint = touch.locationInView(self.view)
            for button: keyboardButton in buttons {
                if CGRectContainsPoint(button.frame, location) {
                    button.touchUpInside()
                }
            }
        }
    }
    
    override func touchesCancelled(touches: NSSet!, withEvent event: UIEvent!) {
        super.touchesCancelled(touches, withEvent: event)
        
        if self.firstTouchInside == true {
            let touch: UITouch = touches.anyObject() as UITouch
            switch touch.view.tag {
            case 1...3:
                (touch.view as keyboardButton).touchCancel()
            default:
                break
            }
        }
    }
}

class keyboardButton: UILabel {
    var originalBackgroundColor: UIColor?
    
    func touchInside() {
        println("touchInside")if self.originalBackgroundColor == nil {
            self.originalBackgroundColor = self.backgroundColor
        }
        var hue: CGFloat = 0
        var saturation: CGFloat = 0
        var brightness: CGFloat = 0
        var alpha: CGFloat = 0
        
        self.backgroundColor?.getHue(
            &hue,
            saturation: &saturation,
            brightness: &brightness,
            alpha: &alpha)
        if brightness > 0.5 {
            brightness = brightness - 0.10
        } else {
            brightness = brightness + 0.10
        }
        
        self.backgroundColor = UIColor(
            hue: hue,
            saturation: saturation,
            brightness: brightness,
            alpha: alpha)
    }
    
    func touchCancel() {
        println("touchCancel")
        self.backgroundColor = self.originalBackgroundColor
    }
    
    func touchUpInside() {
        println("touchUpInside")
        UIView.transitionWithView(
            self,
            duration: 0.5,
            options: UIViewAnimationOptions.TransitionCrossDissolve,
            animations: {self.backgroundColor = self.originalBackgroundColor},
            completion: nil)
    }
}
import UIKit

class ViewController: UIViewController {

    @IBOutlet var buttons: [keyboardButton]!
    var firstTouchInside: Bool = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view, typically from a nib.
        
        for button: keyboardButton in buttons {
            button.userInteractionEnabled = true
            button.backgroundColor = UIColor.whiteColor()
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        super.touchesBegan(touches, withEvent: event)
        
        let touch: UITouch = touches.anyObject() as UITouch
        switch touch.view.tag {
        case 1...3:
            (touch.view as keyboardButton).touchInside()
            self.firstTouchInside = true
        default:
            self.firstTouchInside = false
        }
    }
    
    override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
        super.touchesMoved(touches, withEvent: event)
        
        if self.firstTouchInside == true {
            let touch: UITouch = touches.anyObject() as UITouch
            let oldLocation: CGPoint = touch.previousLocationInView(self.view)
            let newLocation: CGPoint = touch.locationInView(self.view)
            var oldTag: Int?
            var newTag: Int?
            for button: keyboardButton in buttons {
                if CGRectContainsPoint(button.frame, oldLocation) {
                    oldTag = button.tag
                }
                if CGRectContainsPoint(button.frame, newLocation) {
                    newTag = button.tag
                }
            }
            
            if oldTag != newTag {
                if oldTag != nil {
                    (self.view.viewWithTag(oldTag!) as keyboardButton).touchCancel()
                }
                if newTag != nil {
                    (self.view.viewWithTag(newTag!) as keyboardButton).touchInside()
                }
            }
        }
    }
    
    override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
        super.touchesEnded(touches, withEvent: event)
        
        if self.firstTouchInside == true {
            let touch: UITouch = touches.anyObject() as UITouch
            let location: CGPoint = touch.locationInView(self.view)
            for button: keyboardButton in buttons {
                if CGRectContainsPoint(button.frame, location) {
                    button.touchUpInside()
                }
            }
        }
    }
    
    override func touchesCancelled(touches: NSSet!, withEvent event: UIEvent!) {
        super.touchesCancelled(touches, withEvent: event)
        
        if self.firstTouchInside == true {
            let touch: UITouch = touches.anyObject() as UITouch
            switch touch.view.tag {
            case 1...3:
                (touch.view as keyboardButton).touchCancel()
            default:
                break
            }
        }
    }
}

class keyboardButton: UILabel {
    var originalBackgroundColor: UIColor?
    
    func touchInside() {
        println("touchInside")
        self.originalBackgroundColor = self.backgroundColor
        var hue: CGFloat = 0
        var saturation: CGFloat = 0
        var brightness: CGFloat = 0
        var alpha: CGFloat = 0
        
        self.backgroundColor?.getHue(
            &hue,
            saturation: &saturation,
            brightness: &brightness,
            alpha: &alpha)
        if brightness > 0.5 {
            brightness = brightness - 0.10
        } else {
            brightness = brightness + 0.10
        }
        
        self.backgroundColor = UIColor(
            hue: hue,
            saturation: saturation,
            brightness: brightness,
            alpha: alpha)
    }
    
    func touchCancel() {
        println("touchCancel")
        self.backgroundColor = self.originalBackgroundColor
    }
    
    func touchUpInside() {
        println("touchUpInside")
        UIView.transitionWithView(
            self,
            duration: 0.5,
            options: UIViewAnimationOptions.TransitionCrossDissolve,
            animations: {self.backgroundColor = self.originalBackgroundColor},
            completion: nil)
    }
}
import UIKit

class ViewController: UIViewController {

    @IBOutlet var buttons: [keyboardButton]!
    var firstTouchInside: Bool = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view, typically from a nib.
        
        for button: keyboardButton in buttons {
            button.userInteractionEnabled = true
            button.backgroundColor = UIColor.whiteColor()
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        super.touchesBegan(touches, withEvent: event)
        
        let touch: UITouch = touches.anyObject() as UITouch
        switch touch.view.tag {
        case 1...3:
            (touch.view as keyboardButton).touchInside()
            self.firstTouchInside = true
        default:
            self.firstTouchInside = false
        }
    }
    
    override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
        super.touchesMoved(touches, withEvent: event)
        
        if self.firstTouchInside == true {
            let touch: UITouch = touches.anyObject() as UITouch
            let oldLocation: CGPoint = touch.previousLocationInView(self.view)
            let newLocation: CGPoint = touch.locationInView(self.view)
            var oldTag: Int?
            var newTag: Int?
            for button: keyboardButton in buttons {
                if CGRectContainsPoint(button.frame, oldLocation) {
                    oldTag = button.tag
                }
                if CGRectContainsPoint(button.frame, newLocation) {
                    newTag = button.tag
                }
            }
            
            if oldTag != newTag {
                if oldTag != nil {
                    (self.view.viewWithTag(oldTag!) as keyboardButton).touchCancel()
                }
                if newTag != nil {
                    (self.view.viewWithTag(newTag!) as keyboardButton).touchInside()
                }
            }
        }
    }
    
    override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
        super.touchesEnded(touches, withEvent: event)
        
        if self.firstTouchInside == true {
            let touch: UITouch = touches.anyObject() as UITouch
            let location: CGPoint = touch.locationInView(self.view)
            for button: keyboardButton in buttons {
                if CGRectContainsPoint(button.frame, location) {
                    button.touchUpInside()
                }
            }
        }
    }
    
    override func touchesCancelled(touches: NSSet!, withEvent event: UIEvent!) {
        super.touchesCancelled(touches, withEvent: event)
        
        if self.firstTouchInside == true {
            let touch: UITouch = touches.anyObject() as UITouch
            switch touch.view.tag {
            case 1...3:
                (touch.view as keyboardButton).touchCancel()
            default:
                break
            }
        }
    }
}

class keyboardButton: UILabel {
    var originalBackgroundColor: UIColor?
    
    func touchInside() {
        if self.originalBackgroundColor == nil {
            self.originalBackgroundColor = self.backgroundColor
        }
        var hue: CGFloat = 0
        var saturation: CGFloat = 0
        var brightness: CGFloat = 0
        var alpha: CGFloat = 0
        
        self.backgroundColor?.getHue(
            &hue,
            saturation: &saturation,
            brightness: &brightness,
            alpha: &alpha)
        if brightness > 0.5 {
            brightness = brightness - 0.10
        } else {
            brightness = brightness + 0.10
        }
        
        self.backgroundColor = UIColor(
            hue: hue,
            saturation: saturation,
            brightness: brightness,
            alpha: alpha)
    }
    
    func touchCancel() {
        self.backgroundColor = self.originalBackgroundColor
    }
    
    func touchUpInside() {
        UIView.transitionWithView(
            self,
            duration: 0.5,
            options: UIViewAnimationOptions.TransitionCrossDissolve,
            animations: {self.backgroundColor = self.originalBackgroundColor},
            completion: nil)
    }
}
ソース リンク
thirtyseven
  • 409
  • 3
  • 10

h2so5さんの回答をいただいた上で、自分でいろいろ試行錯誤し、最終的にUILabelのサブクラスを作成し、ViewControllerのtouchesBegan, touchesMoved, touchesEnded, touchesCancelledをオーバーライドすることで、同様の仕組みを再現してみました。

h2so5さんの回答をそのまま採用しなかったのは、コメントにも記載したようにUIButtonだと、最初にタップしたボタンの情報がずっと保持されているのが、少し気色悪かったからです。

ただ、自己回答の方法は、自分でもあまりスマートな方法とは思えませんし、不適切なところもあるかも知れません。もし、ご指摘いただける問題等ありましたら、ぜひご意見をいただければと思います。

繰り返しになりますが、h2so5さんと自身の回答のアプローチは大きく異なっていると思いますが、h2so5さんの回答をいただかなければ、自身の回答にはたどり着かなかったのは間違いありません。あらためて、h2so5さんに感謝いたします。

import UIKit

class ViewController: UIViewController {

    @IBOutlet var buttons: [keyboardButton]!
    var firstTouchInside: Bool = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view, typically from a nib.
        
        for button: keyboardButton in buttons {
            button.userInteractionEnabled = true
            button.backgroundColor = UIColor.whiteColor()
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        super.touchesBegan(touches, withEvent: event)
        
        let touch: UITouch = touches.anyObject() as UITouch
        switch touch.view.tag {
        case 1...3:
            (touch.view as keyboardButton).touchInside()
            self.firstTouchInside = true
        default:
            self.firstTouchInside = false
        }
    }
    
    override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
        super.touchesMoved(touches, withEvent: event)
        
        if self.firstTouchInside == true {
            let touch: UITouch = touches.anyObject() as UITouch
            let oldLocation: CGPoint = touch.previousLocationInView(self.view)
            let newLocation: CGPoint = touch.locationInView(self.view)
            var oldTag: Int?
            var newTag: Int?
            for button: keyboardButton in buttons {
                if CGRectContainsPoint(button.frame, oldLocation) {
                    oldTag = button.tag
                }
                if CGRectContainsPoint(button.frame, newLocation) {
                    newTag = button.tag
                }
            }
            
            if oldTag != newTag {
                if oldTag != nil {
                    (self.view.viewWithTag(oldTag!) as keyboardButton).touchCancel()
                }
                if newTag != nil {
                    (self.view.viewWithTag(newTag!) as keyboardButton).touchInside()
                }
            }
        }
    }
    
    override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
        super.touchesEnded(touches, withEvent: event)
        
        if self.firstTouchInside == true {
            let touch: UITouch = touches.anyObject() as UITouch
            let location: CGPoint = touch.locationInView(self.view)
            for button: keyboardButton in buttons {
                if CGRectContainsPoint(button.frame, location) {
                    button.touchUpInside()
                }
            }
        }
    }
    
    override func touchesCancelled(touches: NSSet!, withEvent event: UIEvent!) {
        super.touchesCancelled(touches, withEvent: event)
        
        if self.firstTouchInside == true {
            let touch: UITouch = touches.anyObject() as UITouch
            switch touch.view.tag {
            case 1...3:
                (touch.view as keyboardButton).touchCancel()
            default:
                break
            }
        }
    }
}

class keyboardButton: UILabel {
    var originalBackgroundColor: UIColor?
    
    func touchInside() {
        println("touchInside")
        self.originalBackgroundColor = self.backgroundColor
        var hue: CGFloat = 0
        var saturation: CGFloat = 0
        var brightness: CGFloat = 0
        var alpha: CGFloat = 0
        
        self.backgroundColor?.getHue(
            &hue,
            saturation: &saturation,
            brightness: &brightness,
            alpha: &alpha)
        if brightness > 0.5 {
            brightness = brightness - 0.10
        } else {
            brightness = brightness + 0.10
        }
        
        self.backgroundColor = UIColor(
            hue: hue,
            saturation: saturation,
            brightness: brightness,
            alpha: alpha)
    }
    
    func touchCancel() {
        println("touchCancel")
        self.backgroundColor = self.originalBackgroundColor
    }
    
    func touchUpInside() {
        println("touchUpInside")
        UIView.transitionWithView(
            self,
            duration: 0.5,
            options: UIViewAnimationOptions.TransitionCrossDissolve,
            animations: {self.backgroundColor = self.originalBackgroundColor},
            completion: nil)
    }
}