陈嘉乐
2020-06-16 7167334c0e89dd84827d59e726123d14776e3a09
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
using System;
using UIKit;
using System.IO;
using Foundation;
using CoreGraphics;
using Shared.IO;
 
namespace Shared
{
    /// <summary>
    /// 位置布局
    /// </summary>
    public class VerticalSeekBar : View
    {
        /// <summary>
        /// 滑块
        /// </summary>
        UIButton btnThum = new UIButton(new CGRect(0, 0, 24, 24)) { Enabled = false };
        /// <summary>
        /// 进度条颜色
        /// </summary>
        UIButton btnProgressColor = new UIButton(new CGRect(0, 0, 1, 0)) { Enabled = false };
        /// <summary>
        /// 背景进度条颜色
        /// </summary>
        UIButton btnBackgroundColor = new UIButton(new CGRect(0, 0, 1, 0)) { Enabled = false };
 
        MyVerticalSeekBar currentRealView;
        /// <summary>
        /// 构造函数
        /// </summary>
        public VerticalSeekBar()
        {
            currentRealView = new MyVerticalSeekBar(this) { };
 
            uiView = currentRealView;
 
            uiView.AddSubview(btnBackgroundColor);
            uiView.AddSubview(btnProgressColor);
            uiView.AddSubview(btnThum);
 
        }
 
        /// <summary>
        /// 生成圆形的滑动
        /// </summary>
        /// <returns>The image with color.</returns>
        /// <param name="color">Color.</param>
        UIImage createImageWithColor(UIColor color)
        {
            //设置长宽
            CGRect rect = new CGRect(0.0f, 0.0f, btnThum.Frame.Width, btnThum.Frame.Width);
            UIGraphics.BeginImageContext(rect.Size);
            var context = UIGraphics.GetCurrentContext();
            context.SetFillColor(color.CGColor);
            context.FillRect(rect);
            UIImage resultImage = UIGraphics.GetImageFromCurrentImageContext();
            UIGraphics.EndImageContext();
            // 开始一个Image的上下文
            UIGraphics.BeginImageContextWithOptions(resultImage.Size, false, 0.0f);
            // 添加圆角
            UIBezierPath.FromRoundedRect(rect, btnThum.Frame.Width / 2).AddClip();
            // 绘制图片
            resultImage.Draw(rect);
            // 接受绘制成功的图片
            resultImage = UIGraphics.GetImageFromCurrentImageContext();
            UIGraphics.EndImageContext();
            return resultImage;
        }
 
        public override void Refresh()
        {
            base.Refresh();
            ProgressColor = progressColor;
            BackgroundColor = backgroundColor;
            ThumbColor = thumbColor;
            Progress = progress;
        }
 
        /// <summary>
        /// 当前视图的高度
        /// </summary>
        /// <value>The height.</value>
        public override int Height
        {
            get
            {
                return base.Height;
            }
            set
            {
                base.Height = value;
                if (!IsCanRefresh)
                {
                    return;
                }
                var frameBackgroundColor = btnBackgroundColor.Frame;
                frameBackgroundColor.Height = base.Height;
                btnBackgroundColor.Frame = frameBackgroundColor;
            }
        }
 
        /// <summary>
        /// 当前视图的宽度
        /// </summary>
        /// <value>The width.</value>
        public override int Width
        {
            get
            {
                return base.Width;
            }
            set
            {
                base.Width = value;
                if (!IsCanRefresh)
                {
                    return;
                }
 
                var frameBtnFrame = btnThum.Frame;
                frameBtnFrame.X = (base.Width - btnThum.Frame.Width) / 2;
                btnThum.Frame = frameBtnFrame;
 
                var frameBackgroundColor = btnBackgroundColor.Frame;
                frameBackgroundColor.X = (base.Width - frameBackgroundColor.Width) / 2;
                btnBackgroundColor.Frame = frameBackgroundColor;
 
                var frameProgressColor = btnProgressColor.Frame;
                frameProgressColor.X = (base.Width - frameProgressColor.Width) / 2;
                btnProgressColor.Frame = frameProgressColor;
            }
        }
 
 
        DateTime dateTime;
        /// <summary>
        ///  进度变化事件
        /// </summary>
        public Action<View, int> ProgressChanged;
 
        int max = 100;
        /// <summary>
        /// 最大值
        /// </summary>
        /// <value>The max.</value>
        public int Max
        {
            get
            {
                return max;
            }
            set
            {
                max = value;
            }
        }
        bool isCanScrolled = true;
        /// <summary>
        /// 是否允许滑动滑块
        /// </summary>
        /// <value><c>true</c> if is can scrolled; otherwise, <c>false</c>.</value>
        public bool IsCanScrolled
        {
            get
            {
                return isCanScrolled;
            }
            set
            {
                isCanScrolled = value;
            }
        }
 
        nfloat validHeight
        { 
            get {
                return uiView.Frame.Height - btnThum.Frame.Height;
            }
        }
 
        //2019-08-29 改100毫秒
        public int DelayTime = 100;
        //防止跳动,延时显示状态
        DateTime delayDateTime = DateTime.MinValue;
        int progress;
        /// <summary>
        /// 当前进度
        /// </summary>
        /// <value>The progress.</value>
        public int Progress
        {
            get
            {
                return (int)(Max * (validHeight  - btnThum.Frame.Y) / validHeight);
            }
            set
            {
                progress = value;
                if (!IsCanRefresh)
                {
                    return;
                }
                //发送命令后3000毫秒内不更新状态,防止跳动
                if ((DateTime.Now - delayDateTime).TotalMilliseconds < DelayTime)
                {
                    return;
                }
 
                if (Max < progress)
                {
                    progress = Max;
                }
                var frameThum = btnThum.Frame;
                frameThum.Y = 1.0f*(Max - progress) / Max * validHeight;
                btnThum.Frame = frameThum;
 
                var frameProgress = btnProgressColor.Frame;
                frameProgress.Y = frameThum.Y + frameThum.Height / 2;
                frameProgress.Height = uiView.Frame.Height - frameProgress.Y;
                btnProgressColor.Frame = frameProgress;
            }
        }
        /// <summary>
        /// 滑动休眠时间
        /// </summary>
        public int SleepTime = 0;
 
        uint progressColor = 0x99000099;
        /// <summary>
        /// 进度颜色
        /// </summary>
        /// <value>The color of the progress.</value>
        public uint ProgressColor
        {
            get
            {
                return progressColor;
            }
            set
            {
                progressColor = value;
                if (!IsCanRefresh)
                {
                    return;
                }
                int r = (byte)(progressColor / 256 / 256 % 256);
                int g = (byte)(progressColor / 256 % 256);
                int b = (byte)(progressColor % 256);
                int a = (byte)(progressColor / 256 / 256 / 256 % 256);
                btnProgressColor.BackgroundColor = UIKit.UIColor.FromRGBA(r, g, b, a);
            }
        }
 
        uint backgroundColor = 0xFFFFFFFF;
        /// <summary>
        /// 底部颜色
        /// </summary>
        /// <value>The color of the background.</value>
        public new uint BackgroundColor
        {
            get
            {
                return backgroundColor;
            }
            set
            {
                backgroundColor = value;
                if (!IsCanRefresh)
                {
                    return;
                }
                int r = (byte)(backgroundColor / 256 / 256 % 256);
                int g = (byte)(backgroundColor / 256 % 256);
                int b = (byte)(backgroundColor % 256);
                int a = (byte)(backgroundColor / 256 / 256 / 256 % 256);
                btnBackgroundColor.BackgroundColor = UIKit.UIColor.FromRGBA(r, g, b, a);
            }
        }
 
        uint thumbColor = 0xFFFFFFFF;
        /// <summary>
        /// 滑块颜色
        /// </summary>
        /// <value>The color of the background.</value>
        public uint ThumbColor
        {
            get
            {
                return thumbColor;
            }
            set
            {
                thumbColor = value;
                if (!IsCanRefresh)
                {
                    return;
                }
                int r = (byte)(thumbColor / 256 / 256 % 256);
                int g = (byte)(thumbColor / 256 % 256);
                int b = (byte)(thumbColor % 256);
                int a = (byte)(thumbColor / 256 / 256 / 256 % 256);
 
                btnThum.SetBackgroundImage(createImageWithColor(UIColor.FromRGBA(r, g, b, a)), UIControlState.Disabled);
            }
        }
 
        internal override bool TouchEvent(EventActions e, CGPoint point)
        {
            switch (e)
            {
                case EventActions.Down:
                    dateTime = DateTime.Now;
                    setButtonLoaction(point.Y);
                    break;
                case EventActions.Move:
                    setButtonLoaction(point.Y);
                    break;
                case EventActions.Cancel:
                case EventActions.Up:
                    dateTime = DateTime.MinValue;
                    setButtonLoaction(point.Y);
                    break;
            }
            return base.TouchEvent(e,point);
        }
        int beforValue = 0;
        void setButtonLoaction(nfloat y)
        {
            if (!IsCanScrolled)
            {
                return;
            }
            delayDateTime = DateTime.Now;
            if (y < 0)
            {
                y = 0;
            }
 
            if (validHeight < y)
            {
                y = validHeight;
            }
 
            var frameThum = btnThum.Frame;
            frameThum.Y = y;
            btnThum.Frame = frameThum;
 
            var frameProgress = btnProgressColor.Frame;
            frameProgress.Y = frameThum.Y + frameThum.Height / 2;
            frameProgress.Height = uiView.Frame.Height - frameProgress.Y;
            btnProgressColor.Frame = frameProgress;
 
            if (dateTime == DateTime.MinValue)
            {
                if (ProgressChanged != null && beforValue != Progress)
                {
                    dateTime = DateTime.Now;
                    delayDateTime = DateTime.Now;
                    ProgressChanged(this, beforValue = Progress);
                }
            }
            else
            {
                //变化大于500毫秒的时候才通知
                if (SleepTime < (DateTime.Now - dateTime).TotalMilliseconds)
                {
                    dateTime = DateTime.Now;
                    delayDateTime = DateTime.Now;
                    if (ProgressChanged != null && beforValue != Progress)
                    {
                        ProgressChanged(this, beforValue = Progress);
                    }
                }
            }
        }
 
 
        class MyVerticalSeekBar : UIView
        {
            [Weak] VerticalSeekBar view;
            public MyVerticalSeekBar(VerticalSeekBar view) {
                this.view = view;
            }
            public override UIView HitTest(CGPoint point, UIEvent uievent)
            {
                //如果点击是这个区域,不让其它视图执行事件
                if (PointInside(point, uievent))
                {
                    return this;
                }
                return null;
            }
            nfloat startPointX;
 
            /// <summary>
            /// 点击开始
            /// </summary>
            /// <param name="touches">Touches.</param>
            /// <param name="evt">Evt.</param>
            public override void TouchesBegan(NSSet touches, UIEvent evt)
            {
                parentScrolled(false);
                var cgpoint = (touches.AnyObject as UITouch).LocationInView(this);
                startPointX = cgpoint.X;
                view?.TouchEvent(EventActions.Down, (touches.AnyObject as UITouch).LocationInView(this));
            }
            /// <summary>
            ///  移动
            /// </summary>
            /// <param name="touches">Touches.</param>
            /// <param name="evt">Evt.</param>
            public override void TouchesMoved(NSSet touches, UIEvent evt)
            {
                var cgpoint = (touches.AnyObject as UITouch).LocationInView(this);
                if (20 < Math.Abs(cgpoint.X - startPointX))
                {
                    parentScrolled(true);
                }
                view?.TouchEvent(EventActions.Move, (touches.AnyObject as UITouch).LocationInView(this));
            }
 
            /// <summary>
            /// 点击弹起
            /// </summary>
            /// <param name="touches">Touches.</param>
            /// <param name="evt">Evt.</param>
            public override void TouchesEnded(NSSet touches, UIEvent evt)
            {
                parentScrolled(true);
                view?.TouchEvent(EventActions.Up, (touches.AnyObject as UITouch).LocationInView(this));
            }
 
            public override void TouchesCancelled(NSSet touches, UIEvent evt)
            {
                parentScrolled(true);
                view?.TouchEvent(EventActions.Cancel, (touches.AnyObject as UITouch).LocationInView(this));
            }
 
            /// <summary>
            /// 设置父视图是否可以滑动
            /// </summary>
            /// <param name="b">If set to <c>true</c> b.</param>
            void parentScrolled(bool b)
            {
                var tempParent = Superview;
                while (null != tempParent)
                {
                    if (tempParent.GetType() == typeof(UIScrollView))
                    {
                        (tempParent as UIScrollView).ScrollEnabled = b;
                    }
                    tempParent = tempParent.Superview;
                }
            }
        }
    }
}