|
2 | 2 | using System.Collections.Generic; |
3 | 3 | using System.Runtime.InteropServices; |
4 | 4 | using CoreGraphics; |
5 | | -using Foundation; |
6 | | -using Microsoft.Maui.Controls.Handlers.Items; |
7 | 5 | using UIKit; |
8 | 6 |
|
9 | 7 | namespace Microsoft.Maui.Controls.Handlers.Items2; |
@@ -263,136 +261,6 @@ public static UICollectionViewLayout CreateHorizontalGrid(GridItemsLayout gridIt |
263 | 261 | gridItemsLayout.Span); |
264 | 262 |
|
265 | 263 |
|
266 | | -#nullable disable |
267 | | - public static UICollectionViewLayout CreateCarouselLayout( |
268 | | - bool isHorizontal, |
269 | | - Thickness peekAreaInsets, |
270 | | - WeakReference<CarouselView> weakItemsView, |
271 | | - WeakReference<CarouselViewController2> weakController) |
272 | | - { |
273 | | - NSCollectionLayoutDimension itemWidth = NSCollectionLayoutDimension.CreateFractionalWidth(1); |
274 | | - NSCollectionLayoutDimension itemHeight = NSCollectionLayoutDimension.CreateFractionalHeight(1); |
275 | | - NSCollectionLayoutDimension groupWidth = NSCollectionLayoutDimension.CreateFractionalWidth(1); |
276 | | - NSCollectionLayoutDimension groupHeight = NSCollectionLayoutDimension.CreateFractionalHeight(1); |
277 | | - nfloat itemSpacing = 0; |
278 | | - NSCollectionLayoutGroup group = null; |
279 | | - |
280 | | - var layout = new UICollectionViewCompositionalLayout((sectionIndex, environment) => |
281 | | - { |
282 | | - if (!weakItemsView.TryGetTarget(out var itemsView) || !weakController.TryGetTarget(out var controller)) |
283 | | - { |
284 | | - return null; |
285 | | - } |
286 | | - |
287 | | - double sectionMargin = 0.0; |
288 | | - |
289 | | - if (!isHorizontal) |
290 | | - { |
291 | | - sectionMargin = peekAreaInsets.VerticalThickness / 2; |
292 | | - var newGroupHeight = environment.Container.ContentSize.Height - peekAreaInsets.VerticalThickness; |
293 | | - groupHeight = NSCollectionLayoutDimension.CreateAbsolute((nfloat)newGroupHeight); |
294 | | - groupWidth = NSCollectionLayoutDimension.CreateFractionalWidth(1); |
295 | | - } |
296 | | - else |
297 | | - { |
298 | | - sectionMargin = peekAreaInsets.HorizontalThickness / 2; |
299 | | - var newGroupWidth = environment.Container.ContentSize.Width - peekAreaInsets.HorizontalThickness; |
300 | | - groupWidth = NSCollectionLayoutDimension.CreateAbsolute((nfloat)newGroupWidth); |
301 | | - groupHeight = NSCollectionLayoutDimension.CreateFractionalHeight(1); |
302 | | - } |
303 | | - |
304 | | - // Each item has a size |
305 | | - var itemSize = NSCollectionLayoutSize.Create(itemWidth, itemHeight); |
306 | | - // Create the item itself from the size |
307 | | - var item = NSCollectionLayoutItem.Create(layoutSize: itemSize); |
308 | | - |
309 | | - //item.ContentInsets = new NSDirectionalEdgeInsets(0, itemInset, 0, 0); |
310 | | - |
311 | | - var groupSize = NSCollectionLayoutSize.Create(groupWidth, groupHeight); |
312 | | - |
313 | | - if (OperatingSystem.IsIOSVersionAtLeast(16)) |
314 | | - { |
315 | | - group = isHorizontal |
316 | | - ? NSCollectionLayoutGroup.GetHorizontalGroup(groupSize, item, 1) |
317 | | - : NSCollectionLayoutGroup.GetVerticalGroup(groupSize, item, 1); |
318 | | - } |
319 | | - else |
320 | | - { |
321 | | - group = isHorizontal |
322 | | - ? NSCollectionLayoutGroup.CreateHorizontal(groupSize, item, 1) |
323 | | - : NSCollectionLayoutGroup.CreateVertical(groupSize, item, 1); |
324 | | - } |
325 | | - |
326 | | - var section = NSCollectionLayoutSection.Create(group: group); |
327 | | - section.InterGroupSpacing = itemSpacing; |
328 | | - section.OrthogonalScrollingBehavior = isHorizontal |
329 | | - ? UICollectionLayoutSectionOrthogonalScrollingBehavior.GroupPagingCentered |
330 | | - : UICollectionLayoutSectionOrthogonalScrollingBehavior.None; |
331 | | - |
332 | | - section.VisibleItemsInvalidationHandler = (items, offset, env) => |
333 | | - { |
334 | | - if (!weakItemsView.TryGetTarget(out var itemsView) || !weakController.TryGetTarget(out var cv2Controller)) |
335 | | - { |
336 | | - return; |
337 | | - } |
338 | | - |
339 | | - var page = (offset.X + sectionMargin) / env.Container.ContentSize.Width; |
340 | | - |
341 | | - if (Math.Abs(page % 1) > (double.Epsilon * 100) || cv2Controller.ItemsSource.ItemCount <= 0) |
342 | | - { |
343 | | - return; |
344 | | - } |
345 | | - |
346 | | - var pageIndex = (int)page; |
347 | | - var carouselPosition = pageIndex; |
348 | | - |
349 | | - if (itemsView.Loop && cv2Controller.ItemsSource is ILoopItemsViewSource loopSource) |
350 | | - { |
351 | | - var maxIndex = loopSource.LoopCount - 1; |
352 | | - |
353 | | - //To mimic looping, we needed to modify the ItemSource and inserted a new item at the beginning and at the end |
354 | | - if (pageIndex == maxIndex) |
355 | | - { |
356 | | - //When at last item, we need to change to 2nd item, so we can scroll right or left |
357 | | - pageIndex = 1; |
358 | | - } |
359 | | - else if (pageIndex == 0) |
360 | | - { |
361 | | - //When at first item, need to change to one before last, so we can scroll right or left |
362 | | - pageIndex = maxIndex - 1; |
363 | | - } |
364 | | - |
365 | | - //since we added one item at the beginning of our ItemSource, we need to subtract one |
366 | | - carouselPosition = pageIndex - 1; |
367 | | - |
368 | | - if (itemsView.Position != carouselPosition) |
369 | | - { |
370 | | - //If we are updating the ItemsSource, we don't want to scroll the CollectionView |
371 | | - if (cv2Controller.IsUpdating()) |
372 | | - { |
373 | | - return; |
374 | | - } |
375 | | - |
376 | | - var goToIndexPath = cv2Controller.GetScrollToIndexPath(carouselPosition); |
377 | | - |
378 | | - //This will move the carousel to fake the loop |
379 | | - cv2Controller.CollectionView.ScrollToItem( |
380 | | - NSIndexPath.FromItemSection(pageIndex, 0), |
381 | | - UICollectionViewScrollPosition.Left, |
382 | | - false); |
383 | | - } |
384 | | - } |
385 | | - |
386 | | - //Update the CarouselView position |
387 | | - cv2Controller?.SetPosition(carouselPosition); |
388 | | - }; |
389 | | - |
390 | | - return section; |
391 | | - }); |
392 | | - |
393 | | - return layout; |
394 | | - } |
395 | | -#nullable enable |
396 | 264 | class CustomUICollectionViewCompositionalLayout : UICollectionViewCompositionalLayout |
397 | 265 | { |
398 | 266 | LayoutSnapInfo _snapInfo; |
|
0 commit comments