-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIView+AnimateUp.swift
72 lines (47 loc) · 1.94 KB
/
UIView+AnimateUp.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//
// UIView+AnimateUp.swift
// Odd
//
// Created by subhajit halder on 01/10/16.
// Copyright © 2016 Tanmoy Khanra. All rights reserved.
//
import Foundation
import UIKit
extension UIView {
func animateUpToTargetYOrigin(with yValue: CGFloat) {
NotificationCenter.default.addObserver(forName: NSNotification.Name.UIKeyboardDidChangeFrame, object: nil, queue: OperationQueue.main) { (note) in
var yOrigin : CGFloat?
yOrigin = 0
let keyboardOr = (note.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.origin.y
if yValue > keyboardOr! {
yOrigin = yValue - keyboardOr!
}
UIView.animate(withDuration: 0.3, animations: {
self.frame = CGRect(x: self.frame.origin.x, y: -yOrigin!, width: self.frame.size.width, height: self.frame.size.height)
self.layoutIfNeeded()
})
}
}
}
/*
- (void)animateUpToTargetYOrigin:(CGFloat)yValue
{
[[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardDidChangeFrameNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
CGFloat yOrigin = 0;
CGFloat keyboardOr = [[[note userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].origin.y;
if (yValue > keyboardOr) {
yOrigin = yValue - keyboardOr;
}
[UIView animateWithDuration:0.3f animations:^{
// if (up) {
self.frame = CGRectMake(self.frame.origin.x, -yOrigin, self.frame.size.width, self.frame.size.height);
// }
// else
// {
// self.frame = CGRectMake(self.frame.origin.x, 0, self.frame.size.width, self.frame.size.height);
// }
}];
// NSLog(@"%f yo yo", [[[note userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].origin.y);
}];
}
*/