-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHJButton.swift
166 lines (132 loc) · 5.76 KB
/
HJButton.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//
// HJButton.swift
// HJButton
//
// Created by 研究院01 on 15/3/31.
// Copyright (c) 2015年 HJ. All rights reserved.
//
// @@@ 修改增加注释
import UIKit
import Foundation
class HJButton: UIView,UIGestureRecognizerDelegate {
enum HJFlashButtonType{
case hjFlashButtonTypeInner
case hjFlashButtonTypeOutter
}
let HJFlashInnerCircleInitialRaius:CGFloat = 20.0
var flashColor : UIColor
var color: UIColor?
var name: String?
var buttonType : HJFlashButtonType = HJFlashButtonType.hjFlashButtonTypeInner
{
willSet
{
if buttonType == HJFlashButtonType.hjFlashButtonTypeInner{
self.clipsToBounds = true
}
else{
self.clipsToBounds = false
}
}
didSet
{
if buttonType == HJFlashButtonType.hjFlashButtonTypeInner{
self.clipsToBounds = true
}
else{
self.clipsToBounds = false
}
}
}
fileprivate var textLable:UILabel
override init(frame: CGRect) {
flashColor = UIColor.red
textLable = UILabel(frame: frame)
super.init(frame: frame)
setInit()
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setInit(){
textLable.backgroundColor = UIColor.clear
textLable.textColor = UIColor.white
textLable.textAlignment = NSTextAlignment.center
textLable.isUserInteractionEnabled = true
addSubview(textLable)
backgroundColor = UIColor.gray
isUserInteractionEnabled = true
let tap : UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(HJButton.didTap(_:)))
addGestureRecognizer(tap)
}
func didTap(_ tapGestureHandler:UITapGestureRecognizer){
var tapLocation : CGPoint = tapGestureHandler.location(in: self)
var circleShape : CAShapeLayer
var scale : CGFloat = 1.0
let width:CGFloat = self.bounds.width
let height:CGFloat = self.bounds.height
if self.buttonType == HJFlashButtonType.hjFlashButtonTypeInner{
let biggerEdge:CGFloat = width > height ? width : height
let smallerEdge = width > height ? height : width
let radius = smallerEdge/2 > HJFlashInnerCircleInitialRaius ? HJFlashInnerCircleInitialRaius:smallerEdge/2
scale = biggerEdge / radius + 0.5
println(biggerEdge)
println(radius)
println(scale)
circleShape = createCircleShapeWithPosition(CGPoint(x: tapLocation.x - radius, y: tapLocation.y - radius), rect: CGRect(x: 0, y: 0, width: radius * 2, height: radius * 2), radius: radius)
}else{
scale = 2.5
circleShape = createCircleShapeWithPosition(CGPoint(x: width/2, y: height/2), rect: CGRect(x: -self.bounds.midX, y: -self.bounds.midY, width: width, height: height), radius: layer.cornerRadius)
}
layer.addSublayer(circleShape)
var groupAnimation:CAAnimationGroup = createFlashAnimationWithScale(scale, duration: 2.5)
groupAnimation.setValue(circleShape, forKey: "circleShaperLayer")
circleShape.add(groupAnimation, forKey: nil)
circleShape.delegate = self
}
func createCircleShapeWithPosition(_ position:CGPoint,rect:CGRect,radius:CGFloat) -> CAShapeLayer{
let circleShape:CAShapeLayer = CAShapeLayer()
circleShape.path = UIBezierPath(roundedRect: rect, cornerRadius: radius).cgPath
circleShape.position = position
if self.buttonType == HJFlashButtonType.hjFlashButtonTypeInner{
circleShape.bounds = CGRect(x: 0, y: 0, width: radius * 2, height: radius * 2)
if flashColor == UIColor.clear {
circleShape.fillColor = self.flashColor.cgColor
}else{
circleShape.fillColor = UIColor.white.cgColor
}
}else{
circleShape.fillColor = UIColor.clear.cgColor
circleShape.strokeColor = self.flashColor == UIColor.clear ? flashColor.cgColor : UIColor.purple.cgColor
}
circleShape.opacity = 0
circleShape.lineWidth = 1
return circleShape
}
func createFlashAnimationWithScale(_ scale:CGFloat,duration:CGFloat) ->CAAnimationGroup{
var scaleAnimation : CABasicAnimation = CABasicAnimation(keyPath: "transform.scale") // 可能有错
scaleAnimation.fromValue = NSValue(caTransform3D: CATransform3DIdentity)
scaleAnimation.toValue = NSValue(caTransform3D: CATransform3DMakeScale(scale, scale, 1))
var alphaAnimation:CABasicAnimation = CABasicAnimation(keyPath: "opacity")
println(alphaAnimation)
alphaAnimation.fromValue = 1
alphaAnimation.toValue = 0
var animation:CAAnimationGroup = CAAnimationGroup()
animation.animations = [scaleAnimation,alphaAnimation]
animation.delegate = self as! CAAnimationDelegate
animation.duration = CFTimeInterval(duration)
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
return animation
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code
}
*/
override func animationDidStop(_ anim: CAAnimation!, finished flag: Bool) {
let layer: CALayer = anim.value(forKey: "circleShaperLayer") as! CALayer
layer.removeFromSuperlayer()
}
}