wxr
2020-07-06 23c075a9c27946773feccf05abc90489a6bf5203
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
using System;
using UIKit;
using System.IO;
using Foundation;
using CoreGraphics;
using Shared.IO;
 
namespace Shared
{
    /// <summary>
    /// 位置布局
    /// </summary>
    public class HorizontalSeekBar : View
    {
        MySlide currentRealView;
        /// <summary>
        /// 构造函数
        /// </summary>
        public HorizontalSeekBar()
        {
            currentRealView = new MySlide(this) { };
           
            uiView = currentRealView;
        }
 
        /// <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;
                if (!IsCanRefresh)
                {
                    return;
                }
                currentRealView.MaxValue = 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;
                if (!IsCanRefresh)
                {
                    return;
                }
                currentRealView.UserInteractionEnabled = value;
                if (value)
                {
                    //currentRealView.ThumbTintColor = UIColor.White;
                }
                else
                {
                    //currentRealView.ThumbTintColor = UIColor.Clear;
                }
            }
        }
 
        //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)currentRealView.Value;
            }
            set
            {
                progress = value;
                if (!IsCanRefresh)
                {
                    return;
                }
                //发送命令后3000毫秒内不更新状态,防止跳动
                if ((DateTime.Now - delayDateTime).TotalMilliseconds < DelayTime)
                {
                    return;
                }
                currentRealView.Value = value;
            }
        }
 
        public int SleepTime = 0;
        uint progressColor = 0xFFEB642C;
        /// <summary>
        /// 进度颜色
        /// </summary>
        /// <value>The color of the progress.</value>
        public uint ProgressColor
        {
            get
            {
                return progressColor;
            }
            set
            {
                progressColor = value;
                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);
                currentRealView.MinimumTrackTintColor = 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;
                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);
                currentRealView.MaximumTrackTintColor = 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;
                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);
                currentRealView.SetThumbImage(createImageWithColor(UIColor.FromRGBA(r, g, b, a)), UIControlState.Normal);
            }
        }
 
 
        /// <summary>
        /// 是否可以滑动
        /// </summary>
        public bool IsCanMove = true;
        public DateTime dateTime = DateTime.MinValue;
        internal override bool TouchEvent(EventActions eventAction, CGPoint point)
        {
            if (!IsCanMove)
            {
                return true;
            }
            delayDateTime = DateTime.Now;
            switch (eventAction)
            {
                case EventActions.Down:
                    dateTime = DateTime.MinValue;
                    setValue((float)point.X);
                    break;
                case EventActions.Move:
                    if (dateTime == DateTime.MinValue)
                    {
                        dateTime = DateTime.Now;
                    }
                    setValue((float)point.X);
                    break;
                case EventActions.Up:
                    dateTime = DateTime.MinValue;
                    setValue((float)point.X);
                    break;
                case EventActions.Cancel:
                    dateTime = DateTime.MinValue;
                    setValue((float)point.X);
                    break;
            }
            return base.TouchEvent(eventAction, point);
        }
        float beforeValue = -1;
        void setValue(float x)
        {
            if (x < 0)
            {
                x = 0;
            }
            if (Width - ThumbRadius < x)
            {
                x = Width - ThumbRadius;
            }
 
            currentRealView.Value = (x / (Width - ThumbRadius) * max);
 
            if (beforeValue != currentRealView.Value)
            {
                //点击下或者弹起或者滑动时间500毫秒的时候,就触发事件
                if (ProgressChanged != null
                    && SleepTime < (DateTime.Now - dateTime).TotalMilliseconds)
                {
                    beforeValue = currentRealView.Value;
                    dateTime = DateTime.MinValue;
                    delayDateTime = DateTime.Now;
                    ProgressChanged(this, (int)currentRealView.Value);
                }
            }
        }
 
        int thumbRadius = 9;// 12;
        public int ThumbRadius
        {
            get
            {
                return thumbRadius;
            }
            set
            {
                thumbRadius = value;
                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);
                currentRealView.SetThumbImage(createImageWithColor(UIColor.FromRGBA(r, g, b, a)), UIControlState.Normal);
            }
        }
 
        /// <summary>
        /// 刷新当前视图
        /// </summary>
        public override void Refresh()
        {
            base.Refresh();
            Max = max;
            ProgressColor = progressColor;
            Progress = progress;
            IsCanScrolled = isCanScrolled;
            ThumbRadius = thumbRadius;
        }
 
        UIImage createImageWithColor(UIColor color)
        {
            //设置长宽
            CGRect rect = new CGRect(0.0f, 0.0f, ThumbRadius * 2, ThumbRadius * 2);
            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, ThumbRadius).AddClip();
            // 绘制图片
            resultImage.Draw(rect);
            // 接受绘制成功的图片
            resultImage = UIGraphics.GetImageFromCurrentImageContext();
            UIGraphics.EndImageContext();
            return resultImage;
        }
 
        class MySlide : UISlider
        {
            [Weak] View view;
            public MySlide(View view) {
                this.view = view;
            }
            /// <summary>
            /// 点击开始
            /// </summary>
            /// <param name="touches">Touches.</param>
            /// <param name="evt">Evt.</param>
            public override void TouchesBegan(NSSet touches, UIEvent evt)
            {
                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)
            {
                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)
            {
                view?.TouchEvent(EventActions.Up, (touches.AnyObject as UITouch).LocationInView(this));
            }
 
            public override void TouchesCancelled(NSSet touches, UIEvent evt)
            {
                view?.TouchEvent(EventActions.Cancel, (touches.AnyObject as UITouch).LocationInView(this));
            }
        }
    }
}