使用Xamarin ios iCarousel(我需要轮播来循环并设置循环时间)

时间:2018-12-27 15:42:21

标签: ios xamarin xamarin.ios icarousel

我最近在工作中使用了Carousel。 下载的样本可以显示我想要的感觉。 但是我需要轮播以进行单车游戏并设置单车时间 我不知道如何使用iCarouselDelegate重写该方法。 请给我一些建议。

命名空间Xamarin.iOS.iCarouselExample             {                 公共局部类ViewController:UIViewController                 {                     受保护的ViewController(IntPtr句柄):base(句柄)                     {                     }

                private List<int> items;

                public override void ViewDidLoad()
                {
                    base.ViewDidLoad();

                    items = Enumerable.Range(1, 100).ToList();

                    // Setup iCarousel view
                    var carousel = new iCarousel
                    {
                        Bounds = View.Bounds,

                        ContentMode = UIViewContentMode.Center,
                        Type = iCarouselType.TimeMachine,
                        Frame = View.Frame,
                        CenterItemWhenSelected = true,
                        DataSource = new SimpleDataSource(items),
                        Delegate = new SimpleDelegate(this)
                    };

                    View.AddSubview(carousel);
                    ViewDidLayoutSubviews();
                }

                public class SimpleDataSource : iCarouselDataSource
                {
                    private readonly List<int> _data;

                    public SimpleDataSource(List<int> data)
                    {
                        _data = data;
                    }

                    public override nint NumberOfItemsInCarousel(iCarousel carousel) => _data.Count;

                    public override UIView ViewForItemAtIndex(iCarousel carousel, nint index, UIView view)
                    {
                        UILabel label;

                        // create new view if no view is available for recycling
                        if (view == null)
                        {
                            var imgView = new UIImageView(new RectangleF(0, 200, 200, 200))
                            {
                                BackgroundColor = UIColor.Orange,
                                ContentMode = UIViewContentMode.Center
                            };

                            label = new UILabel(imgView.Bounds)
                            {
                                BackgroundColor = UIColor.Clear,
                                TextAlignment = UITextAlignment.Center,
                                Tag = 1
                            };
                            imgView.AddSubview(label);
                            view = imgView;
                        }
                        else
                        {
                            // get a reference to the label in the recycled view
                            label = (UILabel)view.ViewWithTag(1);
                        }

                        label.Text = _data[(int)index].ToString();

                        return view;
                    }
                }

                public class SimpleDelegate : iCarouselDelegate
                {
                    private readonly ViewController _viewController;

                    public SimpleDelegate(ViewController vc)
                    {
                        _viewController = vc;
                    }

                    public override void DidSelectItemAtIndex(iCarousel carousel, nint index)
                    {
                        var alert = UIAlertController.Create("Clicked index:", index.ToString(), UIAlertControllerStyle.Alert);
                        alert.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Cancel, null));

                        _viewController.PresentViewController(alert, animated: true, completionHandler: null);
                    }

                    public override nfloat ValueForOption(iCarousel carousel, iCarouselOption option, nfloat value)
                    {
                        option.Wrap = true;
                    }

                }
            }
        }

1 个答案:

答案 0 :(得分:0)

您只需在后台线程中运行一个循环,然后将更改分配给UI /主线程中的CurrentItemIndex

ViewDidLoad(在ICarousel示例中)末尾,类似的事情将起作用:

 Task.Run(async () =>
 {
     while (carousel.CurrentItemIndex < 100)
     {
         InvokeOnMainThread(() => carousel.CurrentItemIndex++);
         await Task.Delay(1000);
     }
 });

注意:很遗憾,Xamarin.iOS从未实现CoreData / CoreAnimation / etc等可以使用的“动态”(@ dynamic / NSManaged)属性,但是如果您查看下面的链接, ,我将展示如何在Xamarin中实现它们,从而可以执行以下操作:

UIView.Animate(1.0, 1.0, UIViewAnimationOptions.Autoreverse,
    () => { carousel.CurrentItemIndex = 100; },
    () => { }
);