-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathZFRippleButton.cs
216 lines (182 loc) · 7.01 KB
/
ZFRippleButton.cs
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
using System;
using CoreAnimation;
using CoreGraphics;
using Foundation;
using UIKit;
namespace FormsPinView.iOS
{
public class ZFRippleButton : UIButton
{
private UIView _rippleView = new UIView();
private UIView _rippleBackgroundView = new UIView();
public float _ripplePercent = 0.8f;
public float RipplePercent
{
get { return _ripplePercent; }
set
{
_ripplePercent = value;
SetupRippleView();
}
}
private UIColor _rippleColor;
public UIColor RippleColor
{
get { return _rippleColor; }
set
{
_rippleColor = value;
_rippleView.BackgroundColor = RippleColor;
}
}
private UIColor _rippleBackgroundColor;
public UIColor RippleBackgroundColor
{
get { return _rippleBackgroundColor; }
set
{
_rippleBackgroundColor = value;
_rippleView.BackgroundColor = value;
}
}
private float _buttonCornerRadius = 0f;
public float ButtonCornerRadius
{
get { return _buttonCornerRadius; }
set
{
_buttonCornerRadius = value;
Layer.CornerRadius = value;
}
}
public bool RippleOverBounds { get; set; } = false;
public float ShadowRippleRadius { get; set; } = 1f;
public bool ShadowRippleEnable { get; set; } = true;
public bool TrackTouchLocation { get; set; } = false;
public double TouchUpAnimationTime { get; set; } = 0.6d;
private nfloat _tempShadowRadius = 0f;
private float _tempShadowOpacity = 0f;
private CGPoint? _touchCenterLocation;
private CAShapeLayer RippleMask
{
get
{
if (!RippleOverBounds)
{
var maskLayer = new CAShapeLayer();
maskLayer.Path = UIBezierPath.FromRoundedRect(Bounds, Layer.CornerRadius).CGPath;
return maskLayer;
}
else
{
return null;
}
}
}
public ZFRippleButton(CGRect frame) : base(frame)
{
Setup();
}
private void Setup()
{
SetupRippleView();
_rippleBackgroundView.BackgroundColor = RippleBackgroundColor;
_rippleBackgroundView.Frame = Bounds;
/*
* Do not use sublayers as it can produce unexpected NSInternalInconsistencyException
* crashes: https://github.com/zoonooz/ZFRippleButton/issues/23
*
* Layer.AddSublayer(_rippleBackgroundView.Layer);
* _rippleBackgroundView.Layer.AddSublayer(_rippleView.Layer);
*/
_rippleBackgroundView.AddSubview(_rippleView);
AddSubview(_rippleBackgroundView);
_rippleBackgroundView.Alpha = 0;
Layer.ShadowRadius = 0;
Layer.ShadowOffset = new CGSize(width: 0, height: 1);
Layer.ShadowColor = new UIColor(white: 0.0f, alpha: 0.5f).CGColor;
}
private void SetupRippleView()
{
var size = Bounds.Width * RipplePercent;
var x = (Bounds.Width / 2) - (size / 2);
var y = (Bounds.Height / 2) - (size / 2);
var corner = size / 2;
_rippleView.BackgroundColor = RippleColor;
_rippleView.Frame = new CGRect(x, y, size, size);
_rippleView.Layer.CornerRadius = corner;
}
public override bool BeginTracking(UITouch touch, UIEvent uievent)
{
if (TrackTouchLocation)
{
_touchCenterLocation = touch.LocationInView(this);
}
else {
_touchCenterLocation = null;
}
UIView.Animate(0.1, 0, UIViewAnimationOptions.AllowUserInteraction, () =>
{
_rippleBackgroundView.Alpha = 1;
}, null);
_rippleView.Transform = CGAffineTransform.MakeScale(0.5f, 0.5f);
UIView.Animate(0.7, 0, UIViewAnimationOptions.CurveEaseOut | UIViewAnimationOptions.AllowUserInteraction, () =>
{
_rippleView.Transform = CGAffineTransform.MakeIdentity();
}, null);
if (ShadowRippleEnable)
{
_tempShadowRadius = Layer.ShadowRadius;
_tempShadowOpacity = Layer.ShadowOpacity;
var shadowAnim = new CABasicAnimation { KeyPath = "shadowRadius" };
shadowAnim.To = NSValue.FromObject(ShadowRippleRadius);
var opacityAnim = new CABasicAnimation { KeyPath = "shadowOpacity" };
opacityAnim.To = NSValue.FromObject(1);
var groupAnim = new CAAnimationGroup();
groupAnim.Duration = 0.7;
groupAnim.FillMode = CAFillMode.Forwards;
groupAnim.RemovedOnCompletion = false;
groupAnim.Animations = new[] { shadowAnim, opacityAnim };
Layer.AddAnimation(groupAnim, "shadow");
}
return base.BeginTracking(touch, uievent);
}
public override void CancelTracking(UIEvent uievent)
{
base.CancelTracking(uievent);
AnimateToNormal();
}
public override void EndTracking(UITouch uitouch, UIEvent uievent)
{
base.EndTracking(uitouch, uievent);
AnimateToNormal();
}
private void AnimateToNormal()
{
UIView.Animate(0.1, 0, UIViewAnimationOptions.AllowUserInteraction, () =>
{
_rippleBackgroundView.Alpha = 1;
}, () =>
{
UIView.Animate(TouchUpAnimationTime, 0, UIViewAnimationOptions.AllowUserInteraction, () =>
{
_rippleBackgroundView.Alpha = 0;
}, null);
});
UIView.Animate(0.7, 0, UIViewAnimationOptions.CurveEaseOut | UIViewAnimationOptions.BeginFromCurrentState | UIViewAnimationOptions.AllowUserInteraction, () =>
{
_rippleView.Transform = CGAffineTransform.MakeIdentity();
var shadowAnim = new CABasicAnimation { KeyPath = "shadowRadius" };
shadowAnim.To = NSObject.FromObject(_tempShadowRadius);
var opacityAnim = new CABasicAnimation { KeyPath = "shadowOpacity" };
opacityAnim.To = NSObject.FromObject(_tempShadowOpacity);
var groupAnim = new CAAnimationGroup();
groupAnim.Duration = 0.7;
groupAnim.FillMode = CAFillMode.Forwards;
groupAnim.RemovedOnCompletion = false;
groupAnim.Animations = new[] { shadowAnim, opacityAnim };
Layer.AddAnimation(groupAnim, "shadowBack");
}, null);
}
}
}