陈嘉乐
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
using System;
using CoreGraphics;
using Foundation;
using UIKit;
 
namespace Shared
{
    public class LongPressMoveVerticalScrolViewLayout:ViewGroup
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public LongPressMoveVerticalScrolViewLayout()
        {
            viewGroup = new MyLongPressMoveVerticalScrolViewLayout(this);
            realViewGroup = viewGroup;
            viewGroup.AddGestureRecognizer(new MyUILongPressGestureRecognizer(sender =>
            {
                (sender as MyUILongPressGestureRecognizer).longPressMoveVerticalScrolViewLayout?.uiViewLongPressed(sender);
            })
            { longPressMoveVerticalScrolViewLayout = this });
        }
 
        class MyUILongPressGestureRecognizer : UILongPressGestureRecognizer {
            [Weak] internal LongPressMoveVerticalScrolViewLayout longPressMoveVerticalScrolViewLayout;
            public MyUILongPressGestureRecognizer(Action<UILongPressGestureRecognizer> action):base(action) {
               
            }
        }
 
        public override void AddChidren(View view)
        {
            base.AddChidren(view);
            int maxHeight = 0;
            foreach (View tempView in viewList)
            {
                if (maxHeight < tempView.Bottom)
                {
                    maxHeight = tempView.Bottom;
                }
            }
            (realViewGroup as MyLongPressMoveVerticalScrolViewLayout).ContentSize = new CGSize(Width, maxHeight);
        }
 
        /// <summary>
        /// 根据点击位置找出点击到的视图
        /// </summary>
        /// <returns>The view of point.</returns>
        /// <param name="point">Point.</param>
        /// <param name="uiview">Uiview.</param>
        UIView replaceUIViewOfPoint(CGPoint point, UIView uiview)
        {
            //第一个是背景图
            for (int i = 1; i < realViewGroup.Subviews.Length; i++)
            {
                var tempUIView = realViewGroup.Subviews[i];
                if (tempUIView != uiview)
                {
                    if (tempUIView.Frame.Contains(point))
                    {
                        return tempUIView;
                    }
                }
            }
            return null;
        }
 
        /// <summary>
        /// 对换位置的两个视图
        /// </summary>
        public Action<View,View> ReplaceChanged;
 
        /// <summary>
        /// 根据点击位置找出点击到的视图
        /// </summary>
        /// <returns>返回点击到的视图</returns>
        /// <param name="point">当前点击位置</param>
        UIView selectedUIViewOfPoint(CGPoint point)
        {
            foreach (UIView uiView in realViewGroup.Subviews)
            {
                if (uiView.GetType() != typeof(UIImageView) && uiView.Frame.Contains(point))
                {
                    return uiView;
                }
            }
            return null;
        }
 
 
        CGPoint originPoint;
        bool contain;
        UIView selectedUIView;
        void uiViewLongPressed(UILongPressGestureRecognizer sender)
        {
            //长按开始
            if (sender.State == UIGestureRecognizerState.Began)
            {
                //找出当前点击位置的视图
                selectedUIView = selectedUIViewOfPoint(sender.LocationInView(sender.View));
                if (selectedUIView == null)
                {
                    return;
                }
                originPoint = selectedUIView.Center;
                UIView.Animate(1.0f, () =>
                {
                    selectedUIView.Transform = CGAffineTransform.MakeScale(1.1f, 1.1f);
                    selectedUIView.Alpha = 0.7f;
                });
            }
            else if (sender.State == UIGestureRecognizerState.Changed)
            {
                if (selectedUIView == null)
                {
                    return;
                }
                //移动当前视图
                selectedUIView.Center = sender.LocationInView(sender.View);
                //获取当前视图进入了哪个视图的区域
                var replaceUIView = replaceUIViewOfPoint(selectedUIView.Center, selectedUIView);
                if (replaceUIView ==null)
                {
                    contain = false;
                }
                else
                {
                    //进入区域后切换位置
                    UIView.Animate(1.0f, () =>
                    {
                        var temp = replaceUIView.Center;
                        replaceUIView.Center = originPoint;
                        selectedUIView.Center = temp;
                        originPoint = selectedUIView.Center;
                        contain = true;
                    }, () =>
                    {
                        View selectedView = null;
                        foreach (View view in viewList)
                        {
                            if (view.RealView == selectedUIView)
                            {
                                selectedView = view;
                                break;
                            }
                        }
                        View replaceView = null;
                        foreach (View view in viewList)
                        {
                            if (view.RealView == replaceUIView)
                            {
                                replaceView = view;
                                break;
                            }
                        }
                        if(ReplaceChanged!=null){
                            ReplaceChanged(selectedView, replaceView);
                        }
 
                    });
                }
            }
            else if (sender.State == UIGestureRecognizerState.Ended)
            {
                if (selectedUIView == null)
                {
                    return;
                }
                UIView.Animate(1.0f, () =>
                   {
                       selectedUIView.Transform = CGAffineTransform.MakeIdentity();
                       selectedUIView.Alpha = 1.0f;
                       //如果不需要移动位置
                       if (!contain)
                       {
                           selectedUIView.Center = originPoint;
                       }
                   });
            }
        }
 
        /// <summary>
        /// 是否允许滑动
        /// </summary>
        /// <value><c>true</c> if scroll enabled; otherwise, <c>false</c>.</value>
        public bool ScrollEnabled
        {
            get
            {
                return (viewGroup as MyLongPressMoveVerticalScrolViewLayout).ScrollEnabled;
            }
            set
            {
                (viewGroup as MyLongPressMoveVerticalScrolViewLayout).ScrollEnabled = value;
            }
        }
 
        /// <summary>
        /// 是否正在滑动
        /// </summary>
        /// <value><c>true</c> if decelerating; otherwise, <c>false</c>.</value>
        public bool Decelerating
        {
            get
            {
                return (viewGroup as MyLongPressMoveVerticalScrolViewLayout).Decelerating;
            }
        }
 
        /// <summary>
        /// 滑动事件
        /// </summary>
        public  EventHandler<MouseEventArgs> ScrolledEventHandler;
 
        /// <summary>
        /// 垂直方向滚动视图
        /// </summary>
        class MyLongPressMoveVerticalScrolViewLayout : UIScrollView
        {
            [Weak] LongPressMoveVerticalScrolViewLayout  view;
            public MyLongPressMoveVerticalScrolViewLayout(LongPressMoveVerticalScrolViewLayout view)
            {
                this.view = view;
               
                Scrolled += (sender, e) =>
                {
                   this.view?.ScrolledEventHandler?.Invoke(this, new MouseEventArgs { X = (float)(sender as MyLongPressMoveVerticalScrolViewLayout).ContentOffset.X, Y = (float)(sender as MyLongPressMoveVerticalScrolViewLayout).ContentOffset.Y });
                };
            }
            /// <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));
            }
            /// <summary>
            /// 框架默认加了两个UIImageView ,这样可以去掉默认添加
            /// </summary>
            /// <param name="view">View.</param>
            public override void AddSubview(UIView view)
            {
                if (view.GetType() == typeof(UIImageView) && view.Tag != int.MinValue)
                {
                    return;
                }
                base.AddSubview(view);
            }
        }
    }
}