Abbath
Abbath

Reputation: 1012

scroll images with page control in Xamarin

I try to write application in Xamarin which uses page controll for switching images, I used this article as tutorial http://iosmadesimple.blogspot.ru/2013/01/page-control-for-switching-between-views.html but I can't scroll my images, and in debug it is not stoping at breakpoint in method scrolled. My code:

public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();;
        scrollView.PagingEnabled = true;
        scrollView.ContentSize = new SizeF(scrollView.Frame.Width * 2,scrollView.Frame.Height);
        scrollView.ShowsHorizontalScrollIndicator = false;
        scrollView.ShowsVerticalScrollIndicator = false;
        scrollView.ScrollsToTop = false;

        scrollView.Scrolled += delegate
        {
            var pageWidth = scrollView.Frame.Size.Width;
            var page = (int)Math.Floor((scrollView.ContentOffset.X - pageWidth / 2) / pageWidth) + 1;
            pageControl.CurrentPage = page;
        };

        var pic = new UIImage ("first.png");
        var img = new UIImageView (pic);

        var pic2 = new UIImage ("second.png");
        var img2 = new UIImageView (pic2);

        var imageArray = new UIImageView[]{img,img2};

        for (var i = 0; i < 2; i++) {


            RectangleF frame = new RectangleF ();
            frame.X = scrollView.Frame.Size.Width * i;
            frame.Y = 0;
            frame.Size = scrollView.Frame.Size;

            imageArray [i].Frame = frame;


            scrollView.AddSubview (imageArray[i]);
        }

        scrollView.ContentSize = new SizeF(scrollView.Frame.Size.Width*2,scrollView.Frame.Size.Height);

    }

also I tryed to create my own delegate class and set scrollView.Delegate = new ScrollViewDelegate(this).

can it be problem if I use storyboard? In connection inspector for scroll view: Outlets: delegate - Page control Referencing Outlets: scrollView FirstViewController for page control: Referencing Outlets: delegate = Scroll View

Upvotes: 0

Views: 3494

Answers (1)

TechGirl
TechGirl

Reputation: 488

I have done this once. What you need to do is: You need 2 view controllers,

UIPageviewController*(parent controller)* ImageViewController (controller that has UIImageView)

  1. Create a UIPageviewController(parent controller), override its GetNextViewController and GetPreviousViewController methods.
  2. Maintain a Page number counter in this parent controller page.
  3. I am assuming you would have a List< UIImage >
  4. Here in GetNextViewController method you will increment the counter and display the List< UIImage >(Page No Counter) and return ImageViewController
  5. Here in GetPreviousViewController method you will decrement the counter and display the List< UIImage >(Page No Counter) and return ImageViewController

Hope this helps

Upvotes: 1

Related Questions