Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Continuous scroll for scrollView for iphone using Mono Touch?

I am going to add 5 buttons to scrollview ,in that scrollview when i am scrolling that scrollview has to scroll roundly, after end of fitth button again first button has to come to picture.

thanks..

like image 208
KiShOrE Avatar asked Dec 04 '25 13:12

KiShOrE


1 Answers

I have actually writen a blog post about this. Check out:

http://blog.touch4apps.com/home/iphone-monotouch-development/monotouch-infinite-loop-image-scroll-view

I found out that it can be possible to do with a simple tweak to UIScrollView, handing the

scrollViewDidEndDecelerating from the UIScrollViewDelegate.

Guys from Monotouch team made the great job on this (as usual) and we have the delegate already available via built-in events, in this case DecelerationEnded.

So lets have a look at the implementation of the UIViewController class of some view, note we are adding the UI from the code, not from the nib file, just for simplicity. View has UIScrollView item and loads some images, last image is placed as the first one, then all images in the order and then first image as the last one.

Then the event for DecelerationEnded is handled to actually swap the position (fast - no animation) so user does not find out. For added more touch, the paging is enabled and of course the scroller is hidden, so it is not visible to the user where in the scrolling position he actually is.

public partial class ImageScrollViewController : UIViewController { #region Constructors

    // The IntPtr and initWithCoder constructors are required for items that need 
    // to be able to be created from a xib rather than from managed code

    public ImageScrollViewController (IntPtr handle) : base(handle)
    {
        Initialize ();
    }

    [Export("initWithCoder:")]
    public ImageScrollViewController (NSCoder coder) : base(coder)
    {
        Initialize ();
    }

    public ImageScrollViewController () : base("ImageScrollViewController", null)
    {
        Initialize ();
    }

    void Initialize ()
    {
    }

    #endregion

    UIScrollView scrollView;

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

        scrollView = new UIScrollView (new RectangleF (0, 0, 320, 480));
        this.View.AddSubview (scrollView);

        // add the last image (image4) into the first position
        this.AddImageWithName ("Images/image4.jpg", 0);

        // add all of the images to the scroll view
        for (int i = 1; i < 5; i++) {
            this.AddImageWithName (string.Format ("Images/image{0}.jpg", i), i);
        }

        // add the first image (image1) into the last position
        this.AddImageWithName ("Images/image1.jpg", 5);

        scrollView.PagingEnabled = true;
        scrollView.Bounces = true;
        scrollView.DelaysContentTouches = true;
        scrollView.ShowsHorizontalScrollIndicator = false;

        scrollView.ContentSize = new System.Drawing.SizeF (1920, 480);
        scrollView.ScrollRectToVisible (new RectangleF (320, 0, 320, 480), true);
        scrollView.DecelerationEnded += HandleDecelerationEnded;

    }

    void HandleDecelerationEnded (object sender, EventArgs e)
    {
        if (scrollView.ContentOffset.X == 0) 
        {         
            scrollView.ScrollRectToVisible(new RectangleF(1280, 0, 320, 480), false);
        }    
        else if (scrollView.ContentOffset.X == 1600) 
        {         
            scrollView.ScrollRectToVisible(new RectangleF(320, 0, 320, 480), false);
        }   
    }

    void AddImageWithName (string imageString, int position)
    {
        // add image to scroll view
        UIImage image = UIImage.FromFile (imageString);
        UIImageView imageView = new UIImageView (image);

        imageView.Frame = new System.Drawing.RectangleF (position * 320, 0, 320, 480);

        scrollView.AddSubview (imageView);
    }
}

That is actually all there is to it. And github link: http://github.com/sichy/ImageScrollView

Happy coding!

like image 186
Pavel Sich Avatar answered Dec 07 '25 04:12

Pavel Sich