使用UIScrollView从上到下滚动

时间:2012-09-22 10:04:10

标签: iphone objective-c ios xcode

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

有谁知道怎么做?

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

5 个答案:

答案 0 :(得分:3)

您好我用Apple代码解决了您的问题,

只需用

更改功能
- (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)];
}  

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

答案 1 :(得分: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);

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

Reference

在上面的代码中,它可以像:

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

答案 2 :(得分: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];
}

答案 3 :(得分:0)

我不知道我是否理解你想要完成什么,但基本上你可以使用setContentOffset设置scrollView的位置。

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

答案 4 :(得分:0)

有几种方法可以做到这一点,例如:

  1. 将UITableView与自定义单元格一起使用
  2. 添加UIScrollingView&添加内容/图像&增加 根据数组中对象的数量,scrollview的高度。
  3. 以下是为图片创建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准备好进行垂直滚动,您就可以相应地进行自定义。我希望这对你有用。