forked from MikeCodesDotNET/Awesomizer
/
UIViewExtensions.cs
320 lines (261 loc) · 11.4 KB
/
UIViewExtensions.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
using System;
using CoreAnimation;
using Foundation;
using UIKit;
using CoreGraphics;
using System.Drawing;
using System.Linq;
namespace Awesomizer
{
public static class UIViewExtensions
{
#region Animations
public enum UIViewAnimationFlipDirection
{
Top,
Left,
Right,
Bottom
}
public enum UIViewAnimationRotationDirection
{
Right,
Left
}
public static void ShakeHorizontally(this UIView view)
{
CAKeyFrameAnimation animation = (CAKeyFrameAnimation)CAKeyFrameAnimation.FromKeyPath ("transform.translation.x");
animation.TimingFunction = CAMediaTimingFunction.FromName(CAMediaTimingFunction.Linear);
animation.Duration = 0.5;
animation.Values = new NSObject[]
{
NSNumber.FromFloat(-12),
NSNumber.FromFloat(12),
NSNumber.FromFloat(-8),
NSNumber.FromFloat(8),
NSNumber.FromFloat(-4),
NSNumber.FromFloat(4),
NSNumber.FromFloat(0)
};
view.Layer.AddAnimation(animation, "shake");
}
public static void ShakeVertically(this UIView view)
{
CAKeyFrameAnimation animation = CAKeyFrameAnimation.FromKeyPath("transform.translation.y");
animation.TimingFunction = CAMediaTimingFunction.FromName(CAMediaTimingFunction.Linear);
animation.Duration = 0.5;
animation.Values = new NSObject[]
{
NSNumber.FromFloat(-12),
NSNumber.FromFloat(12),
NSNumber.FromFloat(-8),
NSNumber.FromFloat(8),
NSNumber.FromFloat(-4),
NSNumber.FromFloat(4),
NSNumber.FromFloat(0)
};
view.Layer.AddAnimation(animation, "shake");
}
public static void Pop(this UIView view, double duration, int repeatCount, float force)
{
CAKeyFrameAnimation animation = CAKeyFrameAnimation.FromKeyPath("transform.scale");
animation.TimingFunction = CAMediaTimingFunction.FromName(CAMediaTimingFunction.EaseInEaseOut);
animation.KeyTimes = new NSNumber[]
{
NSNumber.FromFloat(0),
NSNumber.FromFloat(0.2f),
NSNumber.FromFloat(0.4f),
NSNumber.FromFloat(0.6f),
NSNumber.FromFloat(0.8f),
NSNumber.FromFloat(1)
};
animation.Duration = duration;
animation.Additive = true;
animation.RepeatCount = repeatCount;
animation.Values = new NSObject[]
{
NSNumber.FromFloat(0),
NSNumber.FromFloat(0.2f * force),
NSNumber.FromFloat(-0.2f * force),
NSNumber.FromFloat(0.2f * force),
NSNumber.FromFloat(0)
};
view.Layer.AddAnimation(animation, "pop");
}
public static void PulseToSize(this UIView view, float scale, double duration, bool repeat)
{
CABasicAnimation pulseAnimation = CABasicAnimation.FromKeyPath("transform.scale");
pulseAnimation.Duration = duration;
pulseAnimation.To = NSNumber.FromFloat(scale);
pulseAnimation.TimingFunction = CAMediaTimingFunction.FromName(CAMediaTimingFunction.EaseInEaseOut);
pulseAnimation.RepeatCount = repeat == false ? 0 : float.MaxValue;
view.Layer.AddAnimation(pulseAnimation, "pulse");
}
public static void FlipWithDuration(this UIView view, double duration, UIViewAnimationFlipDirection direction, int repeatCount, bool shouldAutoReverse)
{
var subType = string.Empty;
switch (direction)
{
case UIViewAnimationFlipDirection.Top:
subType = "fromTop";
break;
case UIViewAnimationFlipDirection.Left:
subType = "fromLeft";
break;
case UIViewAnimationFlipDirection.Bottom:
subType = "fromBottom";
break;
case UIViewAnimationFlipDirection.Right:
subType = "fromRight";
break;
default:
break;
}
CATransition transition = new CATransition();
transition.StartProgress = 0;
transition.EndProgress = 1;
transition.Type = "flip";
transition.Subtype = subType;
transition.Duration = duration;
transition.RepeatCount = repeatCount;
transition.AutoReverses = shouldAutoReverse;
view.Layer.AddAnimation(transition, "spin");
}
public static void RotateToAngle(this UIView view, float angle, double duration, UIViewAnimationRotationDirection direction, int repeatCount, bool shouldAutoReverse)
{
CABasicAnimation rotateAnimation = CABasicAnimation.FromKeyPath("transform.rotation.z");
rotateAnimation.To = direction == UIViewAnimationRotationDirection.Right ? NSNumber.FromFloat(angle) : NSNumber.FromFloat(-angle);
rotateAnimation.Duration = duration;
rotateAnimation.AutoReverses = shouldAutoReverse;
rotateAnimation.RepeatCount = repeatCount;
rotateAnimation.TimingFunction = CAMediaTimingFunction.FromName(CAMediaTimingFunction.EaseInEaseOut);
view.Layer.AddAnimation(rotateAnimation, "transform.rotation.z");
}
public static void ApplyMotionEffects(this UIView view)
{
int SystemVersion = Convert.ToInt16 (UIDevice.CurrentDevice.SystemVersion.Split ('.') [0]);
if (SystemVersion >= 7)
{
UIInterpolatingMotionEffect horizontalEffect = new UIInterpolatingMotionEffect("center.x", UIInterpolatingMotionEffectType.TiltAlongHorizontalAxis);
horizontalEffect.MinimumRelativeValue = NSNumber.FromFloat(-10);
horizontalEffect.MaximumRelativeValue = NSNumber.FromFloat(10);
UIInterpolatingMotionEffect verticalEffect = new UIInterpolatingMotionEffect("center.6", UIInterpolatingMotionEffectType.TiltAlongVerticalAxis);
verticalEffect.MinimumRelativeValue = NSNumber.FromFloat(-10);
verticalEffect.MaximumRelativeValue = NSNumber.FromFloat(10);
UIMotionEffectGroup motionEffectGroup = new UIMotionEffectGroup();
motionEffectGroup.MotionEffects = new UIMotionEffect[] {horizontalEffect, verticalEffect};
view.AddMotionEffect(motionEffectGroup);
}
}
public static void StopAnimation(this UIView view)
{
view.Layer.RemoveAllAnimations();
}
public static bool IsBeingAnimated(this UIView view)
{
return view.Layer.AnimationKeys.Count() > 0;
}
#endregion
#region Size & Positon
public static void SetWidth(this UIView view, nfloat width)
{
var existingFrame = view.Frame;
view.Frame = new CoreGraphics.CGRect(existingFrame.X, existingFrame.Y, width, existingFrame.Height);
}
public static void SetHeight(this UIView view, nfloat height)
{
var existingFrame = view.Frame;
view.Frame = new CoreGraphics.CGRect(existingFrame.X, existingFrame.Y, existingFrame.Width, height);
}
public static void SetOriginX(this UIView view, nfloat x)
{
var existingFrame = view.Frame;
view.Frame = new CoreGraphics.CGRect(x, existingFrame.Y, existingFrame.Width, existingFrame.Height);
}
public static void SetOriginY(this UIView view, nfloat y)
{
var existingFrame = view.Frame;
view.Frame = new CoreGraphics.CGRect(existingFrame.X, y, existingFrame.Width, existingFrame.Height);
}
public static CGSize StringSize(this UILabel label)
{
return new NSString(label.Text).StringSize(label.Font);
}
#endregion
#region UIImage
public static void MakeRounded(this UIImageView image, int cornerRadius) {
if (image == null)
return;
var layer = image.Layer;
layer.CornerRadius = cornerRadius;
layer.BorderWidth = 0f;
layer.BorderColor = UIColor.Clear.CGColor;
layer.MasksToBounds = true;
}
public static UIImage Tint(this UIImage img, UIColor tint, CGBlendMode blendMode) {
UIGraphics.BeginImageContextWithOptions(img.Size, false, 0f);
tint.SetFill();
var bounds = new CGRect(0, 0, img.Size.Width, img.Size.Height);
UIGraphics.RectFill(bounds);
img.Draw(bounds, blendMode, 1f);
if (blendMode != CGBlendMode.DestinationIn)
img.Draw(bounds, CGBlendMode.DestinationIn, 1f);
var tintedImage = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
return tintedImage;
}
#endregion
#region Colour
public static UIColor ToUIColor(this string hexString)
{
hexString = hexString.Replace("#", "");
if (hexString.Length == 3)
hexString = hexString + hexString;
if (hexString.Length != 6)
throw new Exception("Invalid hex string");
int red = Int32.Parse(hexString.Substring(0,2), System.Globalization.NumberStyles.AllowHexSpecifier);
int green = Int32.Parse(hexString.Substring(2,2), System.Globalization.NumberStyles.AllowHexSpecifier);
int blue = Int32.Parse(hexString.Substring(4,2), System.Globalization.NumberStyles.AllowHexSpecifier);
return UIColor.FromRGB(red, green, blue);
}
public static UIColor Lighten(this UIColor color, int steps)
{
int modifier = 16 * steps;
nfloat rF, gF, bF, aF;
color.GetRGBA(out rF, out gF, out bF, out aF);
int r, g, b, a;
r = (int)Math.Ceiling(rF * 255);
g = (int)Math.Ceiling(gF * 255);
b = (int)Math.Ceiling(bF * 255);
a = (int)Math.Ceiling(aF * 255);
r += modifier;
g += modifier;
b += modifier;
// leave 'a' alone?
r = r > 255 ? 255 : r;
g = g > 255 ? 255 : g;
b = b > 255 ? 255 : b;
return UIColor.FromRGBA(r, g, b, a);
}
public static UIColor Darken(this UIColor color, int steps)
{
int modifier = 16 * steps;
nfloat rF, gF, bF, aF;
color.GetRGBA(out rF, out gF, out bF, out aF);
int r, g, b, a;
r = (int)Math.Ceiling(rF * 255);
g = (int)Math.Ceiling(gF * 255);
b = (int)Math.Ceiling(bF * 255);
a = (int)Math.Ceiling(aF * 255);
r -= modifier;
g -= modifier;
b -= modifier;
// leave 'a' alone?
r = r < 0 ? 0 : r;
g = g < 0 ? 0 : g;
b = b < 0 ? 0 : b;
return UIColor.FromRGBA(r, g, b, a);
}
#endregion
}
}