1
1
//
2
- // Test15ViewController .m
2
+ // AllTest4ViewController .m
3
3
// MyLayout
4
4
//
5
5
// Created by oybq on 15/7/6.
@@ -178,9 +178,10 @@ -(UIView*)createCellLayout1:(NSString*)image title:(NSString*)title
178
178
179
179
#pragma mark -- HandleMethod
180
180
181
- -(void )handleCellLayoutTap : (UIView*)sender
181
+
182
+ // 显示点击提示框
183
+ -(void )showClikAlert : (UIView*)sender inFlowLayout : (MyFlowLayout*)flowLayout
182
184
{
183
-
184
185
NSInteger supplementaryIndex = sender.tag / sBaseTag ;
185
186
NSInteger cellIndex = sender.tag % sBaseTag ;
186
187
@@ -189,7 +190,75 @@ -(void)handleCellLayoutTap:(UIView*)sender
189
190
UIAlertView *alertView = [[UIAlertView alloc ] initWithTitle: nil message: message delegate: nil cancelButtonTitle: @" OK" otherButtonTitles: nil ];
190
191
191
192
[alertView show ];
192
-
193
+ }
194
+
195
+
196
+ -(void )exchangeSubview : (UIView*)sender from : (MyBaseLayout*)fromLayout to : (MyBaseLayout*)toLayout
197
+ {
198
+ // 往下移动时,有可能会上面缩小而下面整体往上移动,结束位置不正确。 也就是移动的视图开始位置正确,结束位置不正确。
199
+ // 往上移动时,有可能会上面撑大而下面整体往下移动,开始位置不正确。 也就是移动的视图开始位置不正确,结束位置正确。
200
+ // 因此为了解决这个问题,我们在删除子视图时不让布局视图进行布局,这可以通过设置布局视图的autoresizesSubviews为NO。
201
+
202
+
203
+ // 得到当前的sender在self.view中的frame,这里进行坐标的转换。
204
+ CGRect rectOld = [fromLayout convertRect: sender.frame toView: self .view];
205
+
206
+ // 得到将sender加入到toLayout后的评估的frame值,注意这时候sender还没有加入到toLayout。因为是加入到最后面,因此只能用这个方法来评估加入后的值,如果不是添加到最后则是可以很简单的得到应该插入的位置的。
207
+ CGRect rectNew = [toLayout subview: sender estimatedRectInLayoutSize: CGSizeZero ];
208
+ rectNew = [toLayout convertRect: rectNew toView: self .view]; // 将新位置的评估的frame值,进行坐标转换。
209
+
210
+ // 在动画的过程中,我们将sender作为self.view的子视图来实现移动的效果。
211
+ fromLayout.autoresizesSubviews = NO ;
212
+ [sender removeFromSuperview ];
213
+ sender.frame = rectOld;
214
+ sender.useFrame = YES ; // 设置为YES表示sender不再受到布局视图的约束,而是可以自由设置frame值。
215
+ [self .view addSubview: sender];
216
+
217
+ [UIView animateWithDuration: 0.3 animations: ^{
218
+
219
+ sender.frame = rectNew;
220
+
221
+ } completion: ^(BOOL finished) {
222
+
223
+ // 恢复重新布局。
224
+ fromLayout.autoresizesSubviews = YES ;
225
+ [fromLayout setNeedsLayout ];
226
+
227
+ // 动画结束后再将sender移植到toLayout中。
228
+ [sender removeFromSuperview ];
229
+ sender.useFrame = NO ; // 还原useFrame,因为加入到toLayout后将受到布局视图的约束。
230
+ [toLayout addSubview: sender];
231
+
232
+ }];
233
+
234
+
235
+ }
236
+
237
+ -(void )handleCellLayoutTap : (UIView*)sender
238
+ {
239
+ // 这里是为了节省,所以将两个不同的功能放在一起。。。
240
+
241
+ MyFlowLayout *superFlowLayout = (MyFlowLayout*)sender.superview ;
242
+
243
+ // 第一个和第二个里面的子视图点击后弹出当前点击的cell的提示信息。
244
+ if (superFlowLayout == self.containerLayouts [0 ] || superFlowLayout == self.containerLayouts [1 ])
245
+ {
246
+ [self showClikAlert: sender inFlowLayout: superFlowLayout];
247
+ }
248
+ else
249
+ {
250
+ // 第三个第四个里面的子视图点击后进行互相移动的场景。
251
+ MyFlowLayout *flowLayout2 = self.containerLayouts [2 ];
252
+ MyFlowLayout *flowLayout3 = self.containerLayouts [3 ];
253
+ if (superFlowLayout == flowLayout2)
254
+ {
255
+ [self exchangeSubview: sender from: flowLayout2 to: flowLayout3];
256
+ }
257
+ else
258
+ {
259
+ [self exchangeSubview: sender from: flowLayout3 to: flowLayout2];
260
+ }
261
+ }
193
262
}
194
263
195
264
-(void )handleReverse : (id )sender
0 commit comments