// Update is called once per frame private void Update() { if (this.state == CarouselState.Stopped) { return; } else if (this.state == CarouselState.Next) { this.currentTransitionTime -= Time.deltaTime; if (this.currentTransitionTime < 0) { this.currentTransitionTime = 0; this.state = CarouselState.Stopped; this.nextButton.interactable = true; this.prevButton.interactable = true; } this.UpdateItemPos(1 - (this.currentTransitionTime / this.transitionTime), CarouselState.Next); } else if (this.state == CarouselState.Previous) { this.currentTransitionTime -= Time.deltaTime; if (this.currentTransitionTime < 0) { this.currentTransitionTime = 0; this.state = CarouselState.Stopped; this.nextButton.interactable = true; this.prevButton.interactable = true; } this.UpdateItemPos(1 - (this.currentTransitionTime / this.transitionTime), CarouselState.Previous); } }
//Method called externally to rotate to the next carousel element public void Next() { this.currentIndex++; if (this.currentIndex >= this.carouselItems.Count) { this.currentIndex = 0; } this.currentTransitionTime = this.transitionTime; this.nextButton.interactable = false; this.prevButton.interactable = false; this.state = CarouselState.Next; }
//Method called externally to rotate to the previous carousel element public void Previous() { this.currentIndex--; if (this.currentIndex < 0) { this.currentIndex = this.carouselItems.Count - 1; } this.currentTransitionTime = this.transitionTime; this.nextButton.interactable = false; this.prevButton.interactable = false; this.state = CarouselState.Previous; }
//Method called from Update to shift the positions of all carousel items private void UpdateItemPos(float interpPercent_, CarouselState direction_) { //Looping through each element in the carousel for (int i = 0; i < this.carouselItems.Count; i++) { //Finding the position that this item is moving toward based on our currently selected index int currIndex = i + this.currentIndex; if (currIndex >= this.carouselItems.Count) { currIndex -= this.carouselItems.Count; } //Finding the position that this item is moving away from based on our direction int prevIndex = currIndex; if (direction_ == CarouselState.Next) { prevIndex--; if (prevIndex < 0) { prevIndex = this.carouselItems.Count - 1; } } else if (direction_ == CarouselState.Previous) { prevIndex++; if (prevIndex >= this.carouselItems.Count) { prevIndex = 0; } } //Finding the new position based on the interp percent between the two positions Vector3 currPos = this.itemPositions[currIndex]; Vector3 prevPos = this.itemPositions[prevIndex]; Vector3 newPos = prevPos + ((currPos - prevPos) * interpPercent_); this.carouselItems[i].localPosition = newPos; //Finding the scale based on the depth of the item float scalePercent = 1 - (newPos.z / this.depth); scalePercent = this.depthScale.Evaluate(scalePercent); this.carouselItems[i].localScale = new Vector3(1, 1, 1) * scalePercent; } }
void CreateCarousel() { ScrollView scrollView = new ScrollView(); StackLayout stkWrapper = new StackLayout(); StackLayout editStatusBar = new StackLayout() { Orientation = StackOrientation.Horizontal }; Image imgEdit = new Image() { HorizontalOptions = LayoutOptions.EndAndExpand, Source = "tick.ico", HeightRequest = WidthRequest = 50 }; editStatusBar.Children.Add(imgEdit); StackLayout actionStatusBar = new StackLayout(); TapGestureRecognizer recognizer = new TapGestureRecognizer() { NumberOfTapsRequired = 1 }; recognizer.Tapped += (sender, e) => { if (CurrentState.Equals(CarouselState.Edit)) { CurrentState = CarouselState.View; } else { CurrentState = CarouselState.Edit; } CreateCarousel(); }; imgEdit.GestureRecognizers.Add(recognizer); editStatusBar.Children.Add(imgEdit); stkWrapper.Children.Add(editStatusBar); Grid grdMain = new Grid() { // Margin = 20,RowSpacing=5 // HeightRequest=100 }; int index = 0; for (int i = 0; i < Source.Count; i++) { int originalivalue = i; ItemPerRow = ItemPerRow >= Source.Count ? Source.Count : ItemPerRow; for (int j = 0; j < ItemPerRow; j++) { if (Source.Count > index) { grdMain.RowDefinitions.Add(new RowDefinition() { Height = 50 // Height=new GridLength(1,GridUnitType.Auto) }); RelativeLayout rtlThumbnailContent = new RelativeLayout() { Margin = 0, VerticalOptions = HorizontalOptions = LayoutOptions.FillAndExpand }; Image ThumbNail = new Image() { Source = Source[i].FilePath, Aspect = Aspect.AspectFit, ClassId = Source[i].Id }; rtlThumbnailContent.Children.Add(ThumbNail, Constraint.RelativeToParent((parent) => { return(parent.X * 0); }), Constraint.RelativeToParent((parent) => { return(parent.Y * 0); }), Constraint.RelativeToParent((parent) => { return(parent.Width); }), Constraint.RelativeToParent((parent) => { return(parent.Height); })); CustomImage imgSelect = new CustomImage() { ID = System.IO.Path.GetFileName(Source[i].FilePath), IsVisible = CurrentState.Equals(CarouselState.Edit) //WidthRequest= HeightRequest=10 }; rtlThumbnailContent.Children.Add(imgSelect, Constraint.RelativeToView(ThumbNail, (parent, view) => { return(view.Width * .8); }), Constraint.RelativeToView(ThumbNail, (parent, view) => { return(view.Height * .0); }) // , Constraint.RelativeToView(ThumbNail, (parent, view) => //{ // return view.Height * .4; //}), Constraint.RelativeToView(ThumbNail, (parent, view) => //{ // return view.Height * .4; //}) ); var sdf = new StackLayout(); sdf.Children.Add(rtlThumbnailContent); TapGestureRecognizer imgtap = new TapGestureRecognizer(); imgtap.Tapped += Imgtap_Tapped; imgSelect.GestureRecognizers.Add(imgtap); TapGestureRecognizer imgThumbNailtap = new TapGestureRecognizer() { NumberOfTapsRequired = 1 }; imgThumbNailtap.Tapped += async(sender, e) => { if (CurrentState.Equals(CarouselState.View)) { Image img = sender as Image; CarouselModal modal = Source.FirstOrDefault(ec => ec.Id == img.ClassId); await Navigation.PushModalAsync(new MediaPlayerPage(modal)); } else { Imgtap_Tapped(imgSelect, e); } }; ThumbNail.GestureRecognizers.Add(imgThumbNailtap); i++; grdMain.Children.Add(rtlThumbnailContent, j, originalivalue); } } } StackLayout stkStatusbar = new StackLayout() { BackgroundColor = Color.Gray }; lblStatus.Text = $"{SelectedItems?.Count} item(s) selected."; stkStatusbar.Children.Add(lblStatus); stkWrapper.Children.Add(grdMain); stkWrapper.Children.Add(stkStatusbar); scrollView.Content = stkWrapper; this.Content = scrollView; }