3

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

有谁知道如何做到这一点?

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

4

5 回答 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)];
}  

或者如果你想要样品,我会给你

于 2012-09-22T10:41:12.593 回答
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);

对于水平滚动,您需要将widthofcontentSize属性设置为比滚动视图的 f 更高的值rame width。同样,对于垂直滚动,您需要将heightofcontentSize属性设置为比滚动视图更高的值frame height

参考

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

scroll.contentSize = CGSizeMake(self.view.frame.size.width, numberOfViews * self.view.frame.size.height);
于 2012-09-22T10:21:43.463 回答
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];
}
于 2012-09-22T10:36:27.440 回答
0

我不知道我是否正确理解了您想要完成的任务,但基本上您可以使用 setContentOffset 设置滚动视图的位置。

CGPoint bottomOffset = CGPointMake(0, scrollView.contentSize.height - self.scrollView.bounds.size.height);
[scrollView setContentOffset:bottomOffset animated:YES];
于 2012-09-22T10:19:33.653 回答
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];

//这是用于Tapping的方法:

- (IBAction)chooseCustomImageTapped:(id)sender
{

} 

现在,一旦 scrollView 准备好进行垂直滚动,您就可以相应地进行自己的自定义。我希望这对你有用。

于 2012-09-22T11:10:19.757 回答