Xamarin ios iCarousel 使用(我需要轮播来循环并设置循环时间)
Xamarin ios iCarousel use (I need carousel to cycle and set the cycle time )
我最近在工作中使用 Carousel。
下载的sample可以表现出我想要的感觉。
但是我需要轮播来循环播放和设置循环时间
我不知道如何使用 iCarouselDelegate 重写该方法。
请给我一些建议。
命名空间Xamarin.iOS.iCarouselExample
{
public 部分 class ViewController : UIViewController
{
protected ViewController(IntPtr handle) : 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;
}
}
}
}
您可以在后台线程中 运行 循环并将更改分配给 UI/main 线程中的 CurrentItemIndex
。
ViewDidLoad
(在 ICarousel 示例中)末尾的类似内容将起作用:
Task.Run(async () =>
{
while (carousel.CurrentItemIndex < 100)
{
InvokeOnMainThread(() => carousel.CurrentItemIndex++);
await Task.Delay(1000);
}
});
注意: Xamarin.iOS 遗憾的是从来没有实现 "Dynamic" (@dynamic/NSManaged) CoreData/CoreAnimation/etc... 可以实现的属性利用,但如果您查看下面的链接,我将展示我如何在 Xamarin 中实现它们,从而可以执行以下操作:
UIView.Animate(1.0, 1.0, UIViewAnimationOptions.Autoreverse,
() => { carousel.CurrentItemIndex = 100; },
() => { }
);
我最近在工作中使用 Carousel。 下载的sample可以表现出我想要的感觉。 但是我需要轮播来循环播放和设置循环时间 我不知道如何使用 iCarouselDelegate 重写该方法。 请给我一些建议。
命名空间Xamarin.iOS.iCarouselExample { public 部分 class ViewController : UIViewController { protected ViewController(IntPtr handle) : 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;
}
}
}
}
您可以在后台线程中 运行 循环并将更改分配给 UI/main 线程中的 CurrentItemIndex
。
ViewDidLoad
(在 ICarousel 示例中)末尾的类似内容将起作用:
Task.Run(async () =>
{
while (carousel.CurrentItemIndex < 100)
{
InvokeOnMainThread(() => carousel.CurrentItemIndex++);
await Task.Delay(1000);
}
});
注意: Xamarin.iOS 遗憾的是从来没有实现 "Dynamic" (@dynamic/NSManaged) CoreData/CoreAnimation/etc... 可以实现的属性利用,但如果您查看下面的链接,我将展示我如何在 Xamarin 中实现它们,从而可以执行以下操作:
UIView.Animate(1.0, 1.0, UIViewAnimationOptions.Autoreverse,
() => { carousel.CurrentItemIndex = 100; },
() => { }
);