简体   繁体   English

使用Mono Touch连续滚动滚动浏览iphone?

[英]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. 我要在scrollview中添加5个按钮,在滚动视图中滚动时,滚动视图必须滚动,在fitth按钮结束后,第一个按钮必须进入画面。

thanks.. 谢谢..

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 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 我发现可以通过对UIScrollView进行简单的调整来实现

scrollViewDidEndDecelerating from the UIScrollViewDelegate. 来自UIScrollViewDelegate的scrollViewDidEndDecelerating。

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. 来自Monotouch团队的人在这方面做得很好(像往常一样),我们已经通过内置事件提供了代表,在这种情况下是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. 因此,让我们看看一些视图的UIViewController类的实现,注意我们是从代码中添加UI,而不是从nib文件中添加UI,只是为了简单起见。 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. View有UIScrollView项并加载一些图像,最后一个图像作为第一个放置,然后是所有图像,然后是第一个图像作为最后一个。

Then the event for DecelerationEnded is handled to actually swap the position (fast - no animation) so user does not find out. 然后处理DecelerationEnded的事件以实际交换位置(快速 - 没有动画),因此用户没有找到。 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 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 和github链接: http//github.com/sichy/ImageScrollView

Happy coding! 快乐的编码!

I did something similar with a PickerView to make it seem like the elements were on a loop. 我使用PickerView做了类似的事情,使它看起来像是循环中的元素。

For this I would try making a longer Scroll View with the 5 buttons repeated in 3 groups. 为此,我会尝试制作一个更长的滚动视图,其中5个按钮重复分为3组。 Call them groups A, B, C. Then, when the ScrollView reaches, say C3, have it scroll to B3 animated:NO. 将它们称为A,B,C组。然后,当ScrollView到达时,比如C3,让它滚动到B3动画:NO。

If the end of the list appears when the user scrolls, you may need to add a dummy group at the beginning and at the end of the Scroll View just for appearances sake. 如果在用户滚动时出现列表末尾,则可能需要在滚动视图的开头和末尾添加一个虚拟组,以便出现。

Edit: Here's the code as requested. 编辑:这是所要求的代码。

ViewController.h ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIScrollViewDelegate>
{
    UIScrollView *myScrollView;
}

@property (nonatomic, retain) IBOutlet UIScrollView *myScrollView;

@end

ViewController.m ViewController.m

#import "ViewController.h"

@implementation ViewController

@synthesize myScrollView;

- (void)viewDidLoad
{
    [super viewDidLoad];

    myScrollView.contentSize = CGSizeMake(320, 2300);
    myScrollView.delegate = self;

    UIView *buttonBox = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 2300)];

    UIButton *buttonA1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonA1.frame = CGRectMake(0, 0, 320, 92);
    [buttonA1 setTitle:@"A" forState:UIControlStateNormal];
    UIButton *buttonB1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonB1.frame = CGRectMake(0, 92, 320, 92);
    [buttonB1 setTitle:@"B" forState:UIControlStateNormal];
    UIButton *buttonC1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonC1.frame = CGRectMake(0, 184, 320, 92);
    [buttonC1 setTitle:@"C" forState:UIControlStateNormal];
    UIButton *buttonD1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonD1.frame = CGRectMake(0, 276, 320, 92);
    [buttonD1 setTitle:@"D" forState:UIControlStateNormal];
    UIButton *buttonE1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonE1.frame = CGRectMake(0, 368, 320, 92);
    [buttonE1 setTitle:@"E" forState:UIControlStateNormal];

    UIButton *buttonA2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonA2.frame = CGRectMake(0, 460, 320, 92);
    [buttonA2 setTitle:@"A" forState:UIControlStateNormal];
    UIButton *buttonB2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonB2.frame = CGRectMake(0, 552, 320, 92);
    [buttonB2 setTitle:@"B" forState:UIControlStateNormal];
    UIButton *buttonC2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonC2.frame = CGRectMake(0, 644, 320, 92);
    [buttonC2 setTitle:@"C" forState:UIControlStateNormal];
    UIButton *buttonD2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonD2.frame = CGRectMake(0, 736, 320, 92);
    [buttonD2 setTitle:@"D" forState:UIControlStateNormal];
    UIButton *buttonE2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonE2.frame = CGRectMake(0, 828, 320, 92);
    [buttonE2 setTitle:@"E" forState:UIControlStateNormal];

    UIButton *buttonA3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonA3.frame = CGRectMake(0, 920, 320, 92);
    [buttonA3 setTitle:@"A" forState:UIControlStateNormal];
    UIButton *buttonB3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonB3.frame = CGRectMake(0, 1012, 320, 92);
    [buttonB3 setTitle:@"B" forState:UIControlStateNormal];
    UIButton *buttonC3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonC3.frame = CGRectMake(0, 1104, 320, 92);
    [buttonC3 setTitle:@"C" forState:UIControlStateNormal];
    UIButton *buttonD3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonD3.frame = CGRectMake(0, 1196, 320, 92);
    [buttonD3 setTitle:@"D" forState:UIControlStateNormal];
    UIButton *buttonE3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonE3.frame = CGRectMake(0, 1288, 320, 92);
    [buttonE3 setTitle:@"E" forState:UIControlStateNormal];

    UIButton *buttonA4 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonA4.frame = CGRectMake(0, 1380, 320, 92);
    [buttonA4 setTitle:@"A" forState:UIControlStateNormal];
    UIButton *buttonB4 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonB4.frame = CGRectMake(0, 1472, 320, 92);
    [buttonB4 setTitle:@"B" forState:UIControlStateNormal];
    UIButton *buttonC4 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonC4.frame = CGRectMake(0, 1564, 320, 92);
    [buttonC4 setTitle:@"C" forState:UIControlStateNormal];
    UIButton *buttonD4 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonD4.frame = CGRectMake(0, 1656, 320, 92);
    [buttonD4 setTitle:@"D" forState:UIControlStateNormal];
    UIButton *buttonE4 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonE4.frame = CGRectMake(0, 1748, 320, 92);
    [buttonE3 setTitle:@"E" forState:UIControlStateNormal];

    UIButton *buttonA5 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonA5.frame = CGRectMake(0, 1840, 320, 92);
    [buttonA5 setTitle:@"A" forState:UIControlStateNormal];
    UIButton *buttonB5 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonB5.frame = CGRectMake(0, 1932, 320, 92);
    [buttonB5 setTitle:@"B" forState:UIControlStateNormal];
    UIButton *buttonC5 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonC5.frame = CGRectMake(0, 2024, 320, 92);
    [buttonC5 setTitle:@"C" forState:UIControlStateNormal];
    UIButton *buttonD5 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonD5.frame = CGRectMake(0, 2116, 320, 92);
    [buttonD5 setTitle:@"D" forState:UIControlStateNormal];
    UIButton *buttonE5 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonE5.frame = CGRectMake(0, 2208, 320, 92);
    [buttonE5 setTitle:@"E" forState:UIControlStateNormal];

    [buttonBox addSubview:buttonA1];
    [buttonBox addSubview:buttonB1];
    [buttonBox addSubview:buttonC1];
    [buttonBox addSubview:buttonD1];
    [buttonBox addSubview:buttonE1];
    [buttonBox addSubview:buttonA2];
    [buttonBox addSubview:buttonB2];
    [buttonBox addSubview:buttonC2];
    [buttonBox addSubview:buttonD2];
    [buttonBox addSubview:buttonE2];
    [buttonBox addSubview:buttonA3];
    [buttonBox addSubview:buttonB3];
    [buttonBox addSubview:buttonC3];
    [buttonBox addSubview:buttonD3];
    [buttonBox addSubview:buttonE3];
    [buttonBox addSubview:buttonA4];
    [buttonBox addSubview:buttonB4];
    [buttonBox addSubview:buttonC4];
    [buttonBox addSubview:buttonD4];
    [buttonBox addSubview:buttonE4];
    [buttonBox addSubview:buttonA5];
    [buttonBox addSubview:buttonB5];
    [buttonBox addSubview:buttonC5];
    [buttonBox addSubview:buttonD5];
    [buttonBox addSubview:buttonE5];

    [myScrollView addSubview:buttonBox];

    myScrollView.contentOffset = CGPointMake(0, 920);
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y < 460) myScrollView.contentOffset = CGPointMake(0, scrollView.contentOffset.y + 460);
    if (scrollView.contentOffset.y > 1380) myScrollView.contentOffset = CGPointMake(0, scrollView.contentOffset.y - 460);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM