iphone - 使用 Mono Touch 连续滚动 iphone 的scrollView?

标签 iphone xamarin.ios

我将向 ScrollView 添加 5 个按钮,在 ScrollView 中,当我滚动时, ScrollView 必须圆形滚动,在适合按钮结束后,第一个按钮必须出现在图片上。

谢谢..

最佳答案

我实际上已经写了一篇关于此的博客文章。查看:

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

我发现可以通过对 UIScrollView 进行简单的调整来完成

scrollViewDidEndDecelerating 从 UIScrollViewDelegate。

Monotouch 团队的人员在这方面做得非常出色(像往常一样),我们已经通过内置事件提供了委托(delegate),在本例中为 DecelerationEnded。

所以让我们看一下某些 View 的 UIViewController 类的实现,请注意,我们是从代码而不是 nib 文件中添加 UI,只是为了简单起见。 View 有 UIScrollView 项目并加载一些图像,最后一个图像作为第一个图像放置,然后按顺序放置所有图像,然后将第一个图像作为最后一个图像。

然后处理 DecelerationEnded 事件以实际交换位置(快速 - 无动画),因此用户不会发现。为了增加更多触摸,启用分页,当然滚动条被隐藏,因此用户看不到他实际所在的滚动位置。

公共(public)分部类 ImageScrollViewController : UIViewController { #region 构造函数

    // 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);
    }
}

这实际上就是全部内容了。和github链接:http://github.com/sichy/ImageScrollView

祝你编码愉快!

关于iphone - 使用 Mono Touch 连续滚动 iphone 的scrollView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8755571/

相关文章:

c++ - 在 iPhone 上使用 C++ 中的 Unicode 的最佳方式是什么?

iphone - 我如何在 UIView 手势下检测 UIButton 的 TouchUp 事件

ios - 在显示 UIAlertView 时处理摇动手势

c# - 字体大小改变 KerningAdjustment,为什么?

ios - xib 中的 UINavigationController 不显示

Xamarin.Forms更改alertDialog中按钮的颜色和边框颜色

iphone - Objective-C中的JSON解析

ios - 获取推送通知的设备 token

iPhone UIView 动画禁用 UIButton subview

c# - 帮助解决 iOS 应用程序崩溃与 “Out Of Memory”