<commit_before>//
//  SwipeView.swift
//  WeeDate
//
//  Created by <NAME> on 6/5/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import Foundation
import UIKit

class SwipeView: UIView {
  private let card: CardView = CardView()
  private var originalPoint: CGPoint?
  
  //MARK: - Initializers
  required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    initialize()
  }
  
  override init(frame: CGRect) {
    super.init(frame: frame)
    initialize()
  }
  
  init() {
    super.init(frame: CGRectZero)
    initialize()
  }
  
  //MARK: - Helper functions
  private func initialize() {
    self.backgroundColor = UIColor.clearColor()
    addSubview(card)
    
    self.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: "dragged:"))
    
    card.setTranslatesAutoresizingMaskIntoConstraints(false)
    setConstraints()
  }
  
  func dragged(gestureRecognizer: UIPanGestureRecognizer) {
    let distance = gestureRecognizer.translationInView(self)
    println("Distance x:\(distance.x) y: \(distance.y)")
    
    switch gestureRecognizer.state {
    case UIGestureRecognizerState.Began:
      originalPoint = center
    case UIGestureRecognizerState.Changed:
      center = CGPointMake(originalPoint!.x + distance.x, originalPoint!.y + distance.y)
    case UIGestureRecognizerState.Ended:
      resetViewPositionAndTransformations()
    default:
      println("Default trigged for GestureRecognizer: \(gestureRecognizer.state)")
      break
    }
  }
  
  private func resetViewPositionAndTransformations() {
    UIView.animateWithDuration(0.2, animations: { () -> Void in
      self.center = self.originalPoint!
    })
  }
  
  private func setConstraints() {
    // Card View
    addConstraint(NSLayoutConstraint(item: card, attribute: .Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: .Top, multiplier: 1.0, constant: 0))
    addConstraint(NSLayoutConstraint(item: card, attribute: .Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: .Bottom, multiplier: 1.0, constant: 0))
    addConstraint(NSLayoutConstraint(item: card, attribute: .Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: .Leading, multiplier: 1.0, constant: 0))
    addConstraint(NSLayoutConstraint(item: card, attribute: .Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: .Trailing, multiplier: 1.0, constant: 0))
  }
}<commit_msg>auto layout screws up transforms<commit_after>//
//  SwipeView.swift
//  WeeDate
//
//  Created by <NAME> on 6/5/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import Foundation
import UIKit

class SwipeView: UIView {
  private let card: CardView = CardView()
  private var originalPoint: CGPoint?
  
  //MARK: - Initializers
  required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    initialize()
  }
  
  override init(frame: CGRect) {
    super.init(frame: frame)
    initialize()
  }
  
  init() {
    super.init(frame: CGRectZero)
    initialize()
  }
  
  //MARK: - Helper functions
  private func initialize() {
    //TODO: set back to clearColor when we have images
    self.backgroundColor = UIColor.redColor()
    addSubview(card)
    
    self.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: "dragged:"))
    card.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)
  }
  
  func dragged(gestureRecognizer: UIPanGestureRecognizer) {
    let distance = gestureRecognizer.translationInView(self)
    println("Distance x:\(distance.x) y: \(distance.y)")
    
    switch gestureRecognizer.state {
    case UIGestureRecognizerState.Began:
      originalPoint = center
    case UIGestureRecognizerState.Changed:
      //distance.x/(self.superview!.frame.width/2)
      let rotationPercent = min(distance.x/(self.superview!.frame.width/2), 1)
      let rotationAngle = (CGFloat(2*M_PI/16)*rotationPercent)
      transform = CGAffineTransformRotate(transform, rotationAngle)
      center = CGPointMake(originalPoint!.x + distance.x, originalPoint!.y + distance.y)
    case UIGestureRecognizerState.Ended:
      resetViewPositionAndTransformations()
    default:
      println("Default trigged for GestureRecognizer: \(gestureRecognizer.state)")
      break
    }
  }
  
  private func resetViewPositionAndTransformations() {
    UIView.animateWithDuration(0.2, animations: { () -> Void in
      self.center = self.originalPoint!
      self.transform = CGAffineTransformMakeRotation(0)
    })
  }
}
{'fraction_non_alphanumeric': 0.11247216035634744, 'fraction_numerical': 0.00913140311804009, 'mean_word_length': 3.6538860103626942, 'pattern_counts': {'":': 0, '<': 7, '<?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': '2305086', 'n_tokens_mistral': 1487, 'n_tokens_neox': 1399, 'n_words': 308}