forked from richzertuche/ZMaterialDesignUIButton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZMaterialButton.swift
123 lines (99 loc) · 4.01 KB
/
ZMaterialButton.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//
// ZMaterialButton.swift
// ZMaterialButton
//
// Created by Ricardo Zertuche on 2/16/15.
// Copyright (c) 2015 Ricardo Zertuche. All rights reserved.
//
import UIKit
@objc protocol ZMaterialButtonDelegate {
func ZMaterialButtonDidExpand(button: ZMaterialButton, expanded: Bool)
}
class ZMaterialButton: UIButton {
var endPoint:CGPoint!
var changeToImage:UIImage!
var expandBy:CGFloat = 9.0
private var originalFrame:CGRect!
private var originalImage: UIImage!
private var originalColor: UIColor!
var Zdelegate: ZMaterialButtonDelegate?
var expanded: Bool!
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
fatalError("init(coder:) has not been implemented")
}
override init(frame: CGRect) {
super.init(frame: frame)
super.layer.cornerRadius = frame.width/2
super.imageView?.contentMode = UIViewContentMode.Center
self.expanded = false
self.originalFrame = frame
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
if self.expanded == true {
self.buttonReduce()
}
else {
self.buttonMove()
}
}
func setEndPont(point: CGPoint){
endPoint = point
}
private func buttonMove(){
self.originalColor = self.superview!.backgroundColor
self.originalImage = self.imageView?.image
UIView.animateWithDuration(0.3, delay: 0, options: .CurveEaseOut, animations: {
self.frame = CGRect(x: self.endPoint.x, y: self.endPoint.y, width: self.frame.width, height: self.frame.height)
}, completion:{
finished in
self.buttonExpand()
})
}
private func buttonExpand(){
let parentView: UIView = self.superview!
self.imageView?.image = nil
let dummyImageView = UIImageView(frame: self.frame)
dummyImageView.image = self.changeToImage
dummyImageView.contentMode = UIViewContentMode.Center
parentView.addSubview(dummyImageView)
UIView.animateWithDuration(0.3, delay: 0, options: .CurveEaseOut, animations: {
self.transform = CGAffineTransformMakeScale(self.expandBy,self.expandBy)
}, completion:{
finished in
self.transform = CGAffineTransformMakeScale(1.0,1.0)
self.imageView?.image = self.changeToImage
parentView.backgroundColor = self.backgroundColor
dummyImageView.removeFromSuperview()
self.expanded = true
self.Zdelegate?.ZMaterialButtonDidExpand(self, expanded: self.expanded)
})
}
private func buttonReduce(){
let parentView: UIView = self.superview!
let dummyImageView = UIImageView(frame: self.frame)
dummyImageView.image = self.changeToImage
dummyImageView.contentMode = UIViewContentMode.Center
parentView.addSubview(dummyImageView)
self.alpha = 0
self.imageView?.image = nil
self.transform = CGAffineTransformMakeScale(self.expandBy,self.expandBy)
parentView.backgroundColor = originalColor
self.alpha = 1
self.expanded = false
self.Zdelegate?.ZMaterialButtonDidExpand(self, expanded: self.expanded)
UIView.animateWithDuration(0.3, delay: 0, options: .CurveEaseOut, animations: {
self.transform = CGAffineTransformMakeScale(1,1)
}, completion:{
finished in
self.imageView?.image = self.originalImage
dummyImageView.removeFromSuperview()
self.buttonGetBack()
})
}
private func buttonGetBack(){
UIView.animateWithDuration(0.6, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: .CurveEaseOut, animations: {
self.frame = self.originalFrame
}, completion:nil)
}
}