iPhone Blur effect box

BlurEffectをかけて足し算の答えを隠すiPhoneアプリのサンプルコードを描いてみます

import UIKit

class ViewController: UIViewController {

    

    var ansBox = [UILabel]()

    

    override func viewDidLoad() {

        super.viewDidLoad()

        

        func mLabel(str: String, isBlur: Bool) -> UILabel {

            var label = UILabel()

            label.font = UIFont.boldSystemFontOfSize(70)

            label.text = str

            label.sizeToFit()

            label.textColor = UIColor.redColor()

            self.view.addSubview(label)

            

            if isBlur {

                var blurView = UIVisualEffectView(effect: UIBlurEffect(style: .Light))

                blurView.frame = CGRectMake(0, 0, 70, 70)

                blurView.center = label.convertPoint(label.center, fromView: self.view)

                label.addSubview(blurView)

            }

            

            return label

        }

        

        for i in 14 {

            var quest = mLabel(\(i) + \(i+1) = “, false)

            quest.frame = CGRect(origin: CGPoint(x:20, y:i*100), size: quest.frame.size)

            

            var ans = mLabel(\(i+i+1), true)

            var x = CGRectGetMaxX(quest.frame)

            var y = CGRectGetMinY(quest.frame)

            ans.frame = CGRect(origin: CGPoint(x:x, y:y), size: ans.frame.size)

            

            ansBox.append(ans)

        }

    }

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

    

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {

        var p = touches.anyObject()!.locationInView(self.view)

        for ans in self.ansBox {

            if CGRectContainsPoint(ans.frame, p) {

                var effectv = ans.subviews[0] as UIVisualEffectView

                UIView.animateWithDuration(1, animations: { () -> Void in

                    effectv.center = CGPointMake(80, effectv.center.y)

                    }, completion: { (Bool) -> Void in

                        effectv.removeFromSuperview()

                })

            }

        }

    }

}