<commit_before>//
//  RAMPaperSwitch.swift
//  RAMPaperSwitch
//
//  Created by Ramotion on 20/11/14.
//  Copyright (c) 2014 Ramotion. All rights reserved.
//

import UIKit

class RAMPaperSwitch: UISwitch {
    
    @IBInspectable var duration: Double = 0.35
    
    var didAnimationStartClosure = {(onAnimation: Bool) -> Void in }
    var didAnimationStopClosure = {(onAnimation: Bool, finished: Bool) -> Void in }
    
    private var shape: CAShapeLayer! = CAShapeLayer()
    private var radius: CGFloat = 0.0
    
    
    override func setOn(on: Bool, animated: Bool) {
        var changed:Bool = on != self.on
        
        super.setOn(on, animated: animated)
        
        if changed {
            if animated {
                self.switchChanged(self)
            } else {
                self.showShape(on)
            }
        }
    }
    
    
    override func layoutSubviews() {
        let x:CGFloat = max(CGRectGetMidX(self.frame), self.superview!.frame.size.width - CGRectGetMidX(self.frame));
        let y:CGFloat = max(CGRectGetMidY(self.frame), self.superview!.frame.size.height - CGRectGetMidY(self.frame));
        self.radius = sqrt(x*x + y*y);
        
        self.shape.frame = CGRectMake(CGRectGetMidX(self.frame) - self.radius,  CGRectGetMidY(self.frame) - self.radius, self.radius * 2, self.radius * 2)
        self.shape.anchorPoint = CGPointMake(0.5, 0.5);
        self.shape.path = UIBezierPath(ovalInRect: CGRectMake(0, 0, self.radius * 2, self.radius * 2)).CGPath
    }


    override func awakeFromNib() {        
        var onTintColor:UIColor? = self.onTintColor
        if onTintColor == nil {
            onTintColor = UIColor.greenColor()
        }
        
        self.layer.borderWidth = 0.5
        self.layer.borderColor = UIColor.whiteColor().CGColor;
        self.layer.cornerRadius = self.frame.size.height / 2;
        
        self.shape.fillColor = onTintColor?.CGColor
        self.shape.masksToBounds = true
        
        self.superview?.layer.insertSublayer(self.shape, atIndex: 0)
        self.superview?.layer.masksToBounds = true
        
        self.showShape(self.on)
        
        self.addTarget(self, action: "switchChanged:", forControlEvents: UIControlEvents.ValueChanged)
    }
    
    
    private func showShape(show: Bool) {
        self.shape.transform = show ? CATransform3DMakeScale(1.0, 1.0, 1.0) : CATransform3DMakeScale(0.0001, 0.0001, 0.0001)
    }


    internal func switchChanged(sender: UISwitch){
        
        if sender.on {
            CATransaction.begin()
            
            self.shape.removeAnimationForKey("scaleDown")
            
            var scaleAnimation:CABasicAnimation  = self.animateKeyPath("transform",
                fromValue: NSValue(CATransform3D: CATransform3DMakeScale(0.0001, 0.0001, 0.0001)),
                toValue:NSValue(CATransform3D: CATransform3DMakeScale(1.0, 1.0, 1.0)),
                timing:kCAMediaTimingFunctionEaseIn);
            
            self.shape.addAnimation(scaleAnimation, forKey: "scaleUp")
            
            CATransaction.commit();
        }
        else {
            CATransaction.begin()
            self.shape.removeAnimationForKey("scaleUp")
            
            var scaleAnimation:CABasicAnimation  = self.animateKeyPath("transform",
                fromValue: NSValue(CATransform3D: CATransform3DMakeScale(1.0, 1.0, 1.0)),
                toValue:NSValue(CATransform3D: CATransform3DMakeScale(0.0001, 0.0001, 0.0001)),
                timing:kCAMediaTimingFunctionEaseOut);
                
            self.shape.addAnimation(scaleAnimation, forKey: "scaleDown")
            
            CATransaction.commit();
        }
    }
    
    
    private func animateKeyPath(keyPath: String, fromValue from: AnyObject, toValue to: AnyObject, timing timingFunction: String) -> CABasicAnimation {
    
        let animation:CABasicAnimation = CABasicAnimation(keyPath: keyPath)
        
        animation.fromValue = from
        animation.toValue = to
        animation.repeatCount = 1
        animation.timingFunction = CAMediaTimingFunction(name: timingFunction)
        animation.removedOnCompletion = false
        animation.fillMode = kCAFillModeForwards
        animation.duration = self.duration;
        animation.delegate = self
        
        return animation;
    }
    
    
    //CAAnimation delegate
    
    
    override func animationDidStart(anim: CAAnimation!){
        self.didAnimationStartClosure(self.on)
    }
    
    
    override func animationDidStop(anim: CAAnimation!, finished flag: Bool){
        self.didAnimationStopClosure(self.on, flag)
    }
}
<commit_msg>- Updated frame calculation syntax.<commit_after>//
//  RAMPaperSwitch.swift
//  RAMPaperSwitch
//
//  Created by Ramotion on 20/11/14.
//  Copyright (c) 2014 Ramotion. All rights reserved.
//

import UIKit

class RAMPaperSwitch: UISwitch {
    
    @IBInspectable var duration: Double = 0.35
    
    var didAnimationStartClosure = {(onAnimation: Bool) -> Void in }
    var didAnimationStopClosure = {(onAnimation: Bool, finished: Bool) -> Void in }
    
    private var shape: CAShapeLayer! = CAShapeLayer()
    private var radius: CGFloat = 0.0
    
    
    override func setOn(on: Bool, animated: Bool) {
        var changed:Bool = on != self.on
        
        super.setOn(on, animated: animated)
        
        if changed {
            if animated {
                self.switchChanged(self)
            } else {
                self.showShape(on)
            }
        }
    }
    
    
    override func layoutSubviews() {
        let x:CGFloat = max(self.frame.midX, self.superview!.frame.size.width - self.frame.midX);
        let y:CGFloat = max(self.frame.midY, self.superview!.frame.size.height - self.frame.midY);
        self.radius = sqrt(x*x + y*y);
        
        self.shape.frame = CGRectMake(self.frame.midX - self.radius,  self.frame.midY - self.radius, self.radius * 2, self.radius * 2)
        self.shape.anchorPoint = CGPointMake(0.5, 0.5);
        self.shape.path = UIBezierPath(ovalInRect: CGRectMake(0, 0, self.radius * 2, self.radius * 2)).CGPath
    }


    override func awakeFromNib() {        
        var onTintColor:UIColor? = self.onTintColor
        if onTintColor == nil {
            onTintColor = UIColor.greenColor()
        }
        
        self.layer.borderWidth = 0.5
        self.layer.borderColor = UIColor.whiteColor().CGColor;
        self.layer.cornerRadius = self.frame.size.height / 2;
        
        self.shape.fillColor = onTintColor?.CGColor
        self.shape.masksToBounds = true
        
        self.superview?.layer.insertSublayer(self.shape, atIndex: 0)
        self.superview?.layer.masksToBounds = true
        
        self.showShape(self.on)
        
        self.addTarget(self, action: "switchChanged:", forControlEvents: UIControlEvents.ValueChanged)
    }
    
    
    private func showShape(show: Bool) {
        self.shape.transform = show ? CATransform3DMakeScale(1.0, 1.0, 1.0) : CATransform3DMakeScale(0.0001, 0.0001, 0.0001)
    }


    internal func switchChanged(sender: UISwitch){
        
        if sender.on {
            CATransaction.begin()
            
            self.shape.removeAnimationForKey("scaleDown")
            
            var scaleAnimation:CABasicAnimation  = self.animateKeyPath("transform",
                fromValue: NSValue(CATransform3D: CATransform3DMakeScale(0.0001, 0.0001, 0.0001)),
                toValue:NSValue(CATransform3D: CATransform3DMakeScale(1.0, 1.0, 1.0)),
                timing:kCAMediaTimingFunctionEaseIn);
            
            self.shape.addAnimation(scaleAnimation, forKey: "scaleUp")
            
            CATransaction.commit();
        }
        else {
            CATransaction.begin()
            self.shape.removeAnimationForKey("scaleUp")
            
            var scaleAnimation:CABasicAnimation  = self.animateKeyPath("transform",
                fromValue: NSValue(CATransform3D: CATransform3DMakeScale(1.0, 1.0, 1.0)),
                toValue:NSValue(CATransform3D: CATransform3DMakeScale(0.0001, 0.0001, 0.0001)),
                timing:kCAMediaTimingFunctionEaseOut);
                
            self.shape.addAnimation(scaleAnimation, forKey: "scaleDown")
            
            CATransaction.commit();
        }
    }
    
    
    private func animateKeyPath(keyPath: String, fromValue from: AnyObject, toValue to: AnyObject, timing timingFunction: String) -> CABasicAnimation {
    
        let animation:CABasicAnimation = CABasicAnimation(keyPath: keyPath)
        
        animation.fromValue = from
        animation.toValue = to
        animation.repeatCount = 1
        animation.timingFunction = CAMediaTimingFunction(name: timingFunction)
        animation.removedOnCompletion = false
        animation.fillMode = kCAFillModeForwards
        animation.duration = self.duration;
        animation.delegate = self
        
        return animation;
    }
    
    
    //CAAnimation delegate
    
    
    override func animationDidStart(anim: CAAnimation!){
        self.didAnimationStartClosure(self.on)
    }
    
    
    override func animationDidStop(anim: CAAnimation!, finished flag: Bool){
        self.didAnimationStopClosure(self.on, flag)
    }
}

{'fraction_non_alphanumeric': 0.09935483870967741, 'fraction_numerical': 0.022150537634408603, 'mean_word_length': 2.1348163127738458, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '905218', 'n_tokens_mistral': 3020, 'n_tokens_neox': 2759, 'n_words': 500}