-
Notifications
You must be signed in to change notification settings - Fork 28
/
SecondViewController.swift
54 lines (45 loc) · 1.78 KB
/
SecondViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//
// SecondViewController.swift
// FrictionCurves
//
// Created by Victor Baro on 3/5/15.
// Copyright (c) 2015 Produkt. All rights reserved.
//
import UIKit
class SecondViewController: UIViewController {
@IBOutlet weak var topViewConstraint: NSLayoutConstraint!
let verticalLimit : CGFloat = -200
var totalTranslation : CGFloat = -200
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func viewDragged(sender: UIPanGestureRecognizer) {
let yTranslation = sender.translationInView(view).y
if (topViewConstraint.hasExceeded(verticalLimit)){
totalTranslation += yTranslation
topViewConstraint.constant = logConstraintValueForYPosition(totalTranslation)
if(sender.state == UIGestureRecognizerState.Ended ){
animateViewBackToLimit()
}
} else {
topViewConstraint.constant += yTranslation
}
sender.setTranslation(CGPointZero, inView: view)
}
func logConstraintValueForYPosition(yPosition : CGFloat) -> CGFloat {
return verticalLimit * (1 + log10(yPosition/verticalLimit))
}
func animateViewBackToLimit() {
self.topViewConstraint.constant = self.verticalLimit
UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 10, options: UIViewAnimationOptions.AllowUserInteraction, animations: { () -> Void in
self.view.layoutIfNeeded()
self.totalTranslation = -200
}, completion: nil)
}
}
private extension NSLayoutConstraint {
func hasExceeded(verticalLimit: CGFloat) -> Bool {
return self.constant < verticalLimit
}
}