使用UIScrollView实现从上到下的滚动

3

我正在寻找一个示例,展示iPhone中的垂直滚动。

有人知道如何做到这一点吗?

如果您可以向我发送教程链接,我将不胜感激。

5个回答

3

你好,我已经解决了你的苹果代码问题。

只需将函数修改为:

- (void)layoutScrollImages
{
    UIImageView *view = nil;
    NSArray *subviews = [scrollView1 subviews];

    // reposition all image subviews in a horizontal serial fashion
    CGFloat curXLoc = 0;
    for (view in subviews)
    {
        if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
        {
            CGRect frame = view.frame;
            frame.origin = CGPointMake(0,curXLoc);
            view.frame = frame;

            curXLoc += (kScrollObjHeight);
        }
    }

    // set the content size so it can be scrollable
    [scrollView1 setContentSize:CGSizeMake(([scrollView1 bounds].size.width),kNumImages * kScrollObjHeight)];
}  

或者如果您需要示例,则我可以为您提供。

是的,它可以正常工作,但我尝试在我的应用程序中使用时遇到了问题。你能否请检查一下:http://stackoverflow.com/questions/12538776/vertical-images-in-uiscrollview - Ali
这是一个关于垂直滚动的新问题。我会检查您的问题,但在此部分中,您必须接受答案。 - Rajneesh071
当然,我需要检查所有答案,然后选择最有用的一个。我会这样做的。感谢你到目前为止的帮助,+1 ;) - Ali
这个问题与垂直滚动有关,而且这个问题的要求已经得到满足...所以首先接受最有用的答案,然后我会给你解决那个问题的解决方案.. :) - Rajneesh071
@Rajneesh071,谢谢你,你应该要有耐心,我需要检查所有的答案。顺便请把你建议的样本发给我。 - Ali

1

针对您的目的,请参考您的链接:

http://idevzilla.com/2010/09/16/uiscrollview-a-really-simple-tutorial/

有一个类似于以下的代码:

scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);

要实现水平滚动,您需要将contentSize属性的width设置为比滚动视图的frame width更高的值。同样,要实现垂直滚动,您需要将contentSize属性的height设置为比滚动视图的frame height更高的值。

参考

在上面的代码中,可以这样做:

scroll.contentSize = CGSizeMake(self.view.frame.size.width, numberOfViews * self.view.frame.size.height);

是的,我尝试过了,但我遇到了一个无法解决的问题,请看这个链接:http://stackoverflow.com/questions/12538776/vertical-images-in-uiscrollview - Ali
我仍然找不到任何教程能够像Instagram应用程序一样展示垂直滚动(您可以在链接中查看)。 - Ali
@Ali:我回答了那个问题,请检查一下。 - Midhun MP
感谢您的帮助。+1。 - Ali

1
您可以使用以下代码将图像从上向下和从下向上滚动。
- (void)viewDidLoad
{
//....
UISwipeGestureRecognizer *recognizer;
//for scrolling up
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeUp)]; // calling method SwipeUp
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
    [[self view] addGestureRecognizer:recognizer];


  //for scrolling down  
    UISwipeGestureRecognizer *recognizer1;
    recognizer1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeDown)]; //calling method SwipeDown
    [recognizer1 setDirection:(UISwipeGestureRecognizerDirectionDown)];
    [[self view] addGestureRecognizer:recognizer1];

}

//初始化并合成 IBOutlet UIImageView *bgImage;

-(IBAction)SwipeUp
{


        [bgImage setImage:[UIImage imageNamed:@"yourImage1"] ];

          bgImage.opaque=YES;
    [self.view addSubview:bgImage];
}


-(IBAction)SwipeDown
{


        [bgImage setImage:[UIImage imageNamed:@"yourImage2"] ];

        bgImage.opaque=YES;
    [self.view addSubview:bgImage];
}

我使用手势滚动。 - Deepjyoti Roy
你也可以使用一个滚动视图,将你的视图添加为子视图。!! - Deepjyoti Roy
看这个:http://stackoverflow.com/questions/12538776/vertical-images-in-uiscrollview。我遇到了这个问题。 - Ali

0

我不确定我是否正确理解了您想要实现的内容,但基本上您可以使用setContentOffset来设置scrollView的位置。

CGPoint bottomOffset = CGPointMake(0, scrollView.contentSize.height - self.scrollView.bounds.size.height);
[scrollView setContentOffset:bottomOffset animated:YES];

看一下Instagram应用程序。您可以在图像之间垂直移动。看看我的链接,如果您不理解,请告诉我。 - Ali

0

有几种做法,例如:

  1. 使用带有自定义单元格的UITableView
  2. 添加UIScrollingView并添加内容/图像,并根据数组中对象数量增加滚动视图的高度。

这是创建2*2图像网格的代码片段:

//Over here adding the image names into the Array
NSMutableArray *imageArray=[[NSMutableArray alloc]init];
    [Arr addObject:[UIImage imageNamed:@"image_1.png"]];
    [Arr addObject:[UIImage imageNamed:@"image_2.png"]];
    [Arr addObject:[UIImage imageNamed:@"image_3.png"]];
    [Arr addObject:[UIImage imageNamed:@"image_4.png"]];
    [Arr addObject:[UIImage imageNamed:@"image_5.png"]];

// 初始化 ScrollView 并设置框架:

scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0,320, 460)];
[scrollView setBackgroundColor:[UIColor clearColor]];
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
int row = 0;
int column = 0;
for(int i = 0; i < imageArray.count; i++) {

    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor=[UIColor clearColor];
    button.frame = CGRectMake(column*160+0,row*210+10, 160, 190);
    [button setImage:[imageArray objectAtIndex:i] forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor clearColor]];
    [button addTarget:self action:@selector(chooseCustomImageTapped:) forControlEvents:UIControlEventTouchUpInside];
    button.tag = i; 
    [view1 addSubview:button];

    if (column == 1) {
        column = 0;
        row++;
    } else {
        column++;
    }

}

[scrollView setContentSize:CGSizeMake(320, (row+1) * 210)];

[self.view addSubview:view1];

//这是用于点击的方法:

- (IBAction)chooseCustomImageTapped:(id)sender
{

} 

现在,一旦scrollView准备好进行垂直滚动,您可以根据自己的需求进行自定义。希望这对您有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接