wxr
2020-06-15 b8e94316e41eba72d927d5ca7d931b26139ee8ff
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
using System;
using UIKit;
using Shared.IO;
using CoreGraphics;
using Foundation;
 
namespace Shared
{
    /// <summary>
    /// 位置布局
    /// </summary>
    public class CoverFlowLayout : ViewGroup
    {
        readonly MyCoverFlowLayout iosUIScrolView;
 
        readonly UIPageControl iosUIPageControl;
 
        /// <summary>
        /// 页面变化事件
        /// </summary>
        public Action<CoverFlowLayout,int> PageChange;
 
        /// <summary>
        /// 垂直方向滚动视图
        /// </summary>
        class MyCoverFlowLayout : UIScrollView
        {
            [Weak]  CoverFlowLayout CoverFlowLayout;
            public MyCoverFlowLayout(CoverFlowLayout CoverFlowLayout)
            {
         
                this.CoverFlowLayout = CoverFlowLayout;
                DecelerationEnded += (s, e) =>
                {
                    var tempUIScrolView = s as MyCoverFlowLayout;
                    //获取当前界面的索引
                    var tempPageIndex = Convert.ToInt32(tempUIScrolView.ContentOffset.X / (tempUIScrolView.Frame.Width - this.CoverFlowLayout._RowPadding));
                    //通知界面索引变化
                    if (this.CoverFlowLayout.PageIndex != tempPageIndex)
                    {
                        this.CoverFlowLayout.PageIndex = tempPageIndex;
                        Shared.HDLUtils.WriteLine($"pageIndex-------{tempPageIndex}");
                        this.CoverFlowLayout.iosUIPageControl.CurrentPage = tempPageIndex;
                        this.CoverFlowLayout.PageChange?.Invoke(this.CoverFlowLayout, this.CoverFlowLayout.pageIndex);
                    }
                };
            }
            /// <summary>
            /// 点击开始
            /// </summary>
            /// <param name="touches">Touches.</param>
            /// <param name="evt">Evt.</param>
            public override void TouchesBegan(NSSet touches, UIEvent evt)
            {
                CoverFlowLayout?.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)
            {
                CoverFlowLayout?.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)
            {
                CoverFlowLayout?.TouchEvent(EventActions.Up, (touches.AnyObject as UITouch).LocationInView(this));
            }
 
            public override void TouchesCancelled(NSSet touches, UIEvent evt)
            {
                CoverFlowLayout?.TouchEvent(EventActions.Cancel, (touches.AnyObject as UITouch).LocationInView(this));
            }
            /// <summary>
            /// 框架默认加了两个UIImageView ,这样可以去掉默认添加
            /// </summary>
            /// <param name="view">View.</param>
            public override void AddSubview(UIView view)
            {
                if (view.GetType() == typeof(UIImageView) && view.Tag != int.MinValue)
                {
                    return;
                }
 
                if (view.ToString().Contains("UIScrollViewScrollIndicator"))
                {
                    return;
                }
                base.AddSubview(view);
            }
        }
 
        /// <summary>
        /// 构造函数
        /// </summary>
        public CoverFlowLayout()
        {
            viewGroup = new UIView();
            iosUIPageControl = new UIPageControl { Enabled = false };
            iosUIScrolView = new MyCoverFlowLayout(this) {  };
           
            realViewGroup = iosUIScrolView;
 
            var frame = iosUIPageControl.Frame;
            frame.Height = 16;
            iosUIPageControl.Frame = frame;
            //启动翻页功能
            //iosUIScrolView.PagingEnabled = true;
    
            //iosUIScrolView.Bounces = false;
            iosUIScrolView.ShowsHorizontalScrollIndicator = false;
            viewGroup.AddSubview(iosUIScrolView);
            viewGroup.AddSubview(iosUIPageControl);
         
 
            _PagePadding = Application.GetRealWidth(41);
            _RowPadding = Application.GetRealWidth(108);
 
 
        }
 
         //两个Page之间的距离
        int _PagePadding;
        public int PagePadding
        {
            get
            {
                return _PagePadding;
            }
            set
            {
                _PagePadding = value;
                ReLocation();
            }
        }
        //page的外边距
        int _RowPadding;
        public int RowPadding
        {
            get
            {
                return _RowPadding;
            }
            set
            {
                _RowPadding = value;
                ReLocation();
            }
        }
 
 
        /// <summary>
        ///  是否显示下面一排的点
        /// </summary>
        /// <value><c>true</c> if is show point; otherwise, <c>false</c>.</value>
        public bool IsShowPoint
        {
            get
            {
                return !iosUIPageControl.Hidden;
            }
            set
            {
                iosUIPageControl.Hidden = !value;
            }
        }
 
        /// <summary>
        /// 是否允许滑动
        /// </summary>
        /// <value><c>true</c> if scroll enabled; otherwise, <c>false</c>.</value>
        public bool ScrollEnabled
        {
            get
            {
                return (iosUIScrolView as MyCoverFlowLayout).ScrollEnabled;
            }
            set
            {
                (iosUIScrolView as MyCoverFlowLayout).ScrollEnabled = value;
            }
        }
 
        int pageIndex;
        /// <summary>
        /// 设置或者获取当前的界面索引
        /// </summary>
        /// <value>The index of the page.</value>
        public int PageIndex
        {
            get
            {
                return pageIndex;
            }
            set
            {
                if (value < 0 || ChildrenCount <= value)
                {
                    return;
                }
                int beforePageIndex = pageIndex;
                pageIndex = value;
                if (!IsCanRefresh)
                {
                    return;
                }
              
                var viewSize = iosUIScrolView.Frame.Size;
 
 
                var rect = new CGRect(pageIndex * viewSize.Width, 0, viewSize.Width, viewSize.Height);
                if (pageIndex != 0) {
                    var frame = iosUIScrolView.Subviews[pageIndex].Frame;
                    var w = frame.X - _RowPadding;
 
                    rect = new CGRect(w, 0, viewSize.Width, viewSize.Height);
                    Shared.HDLUtils.WriteLine($"CGRect xx-------{w}");
                }
 
                Shared.HDLUtils.WriteLine($"pageIndex xx-------{pageIndex}");
                iosUIScrolView.ScrollRectToVisible(rect, false);
 
                if (beforePageIndex != pageIndex)
                {
                    iosUIPageControl.CurrentPage = pageIndex;
                    PageChange?.Invoke(this, pageIndex);
                }
            }
        }
        /// <summary>
        /// 增加子控件
        /// </summary>
        /// <param name="view">View.</param>
        public override void AddChidren(View view)
        {
            //var v = Application.AverageScale;
            //var vv = Application.CurrentHeight;
            //var dddd = Application.DesignWidth;
            base.AddChidren(view);
            iosUIPageControl.Pages = ChildrenCount;
            ReLocation();
        }
 
        /// <summary>
        /// 重新排位置及设备内容大小
        /// </summary>
        public virtual void ReLocation()
        {
            if (iosUIScrolView.Subviews.Length == 0)
            {
                return;
            }
 
            var frame0 = iosUIScrolView.Subviews[0].Frame;
            frame0.X = _RowPadding;
            iosUIScrolView.Subviews[0].Frame = frame0;
 
 
 
            for (int i = 1; i < iosUIScrolView.Subviews.Length; i++)
            {
                var frame = iosUIScrolView.Subviews[i].Frame;
                frame.X = iosUIScrolView.Subviews[i - 1].Frame.Right+_PagePadding;
                iosUIScrolView.Subviews[i].Frame = frame;
            }
 
            iosUIScrolView.ContentSize = new CoreGraphics.CGSize(iosUIScrolView.Subviews[iosUIScrolView.Subviews.Length - 1].Frame.Right+_RowPadding, iosUIScrolView.Frame.Height);
        }
 
 
 
        /// <summary>
        /// 控件宽度
        /// </summary>
        /// <value>The width.</value>
        public override int Width
        {
            get
            {
                return base.Width;
            }
            set
            {
                base.Width = value;
                if (!IsCanRefresh)
                    return;
                var frame = iosUIPageControl.Frame;
                frame.Width = Width;
                iosUIPageControl.Frame = frame;
            }
        }
 
        /// <summary>
        /// 控件的高度
        /// </summary>
        /// <value>The height.</value>
        public override int Height
        {
            get
            {
                return base.Height;
            }
            set
            {
                base.Height = value;
                if (!IsCanRefresh)
                    return;
                var frame = iosUIPageControl.Frame;
                frame.Y = Height - frame.Height;
                iosUIPageControl.Frame = frame;
            }
        }
 
        /// <summary>
        /// 移除当前控件
        /// </summary>
        /// <param name="view">View.</param>
        internal override void Remove(View view)
        {
            base.Remove(view);
            ReLocation();
            PageIndex = ChildrenCount - 1;
            iosUIPageControl.Pages = ChildrenCount;
            iosUIPageControl.CurrentPage = PageIndex;
        }
 
        /// <summary>
        /// 移除所有的控件
        /// </summary>
        public override void RemoveAll()
        {
            base.RemoveAll();
            ReLocation();
            iosUIPageControl.Pages = 0;
            PageIndex = 0;
            iosUIPageControl.CurrentPage = 0;
        }
 
        /// <summary>
        /// 根据索引移除控件
        /// </summary>
        /// <param name="index">Index.</param>
        public override void RemoveAt(int index)
        {
            Remove(viewList[index]);
        }
    }
}