UIScrollView的起点

时间:2009-06-16 21:51:22

标签: iphone uiscrollview

我有一个iPhone应用程序,其主视图为480 x 510.当我从纵向转到landscapeRight时,我对坐标系如何变化感到困惑。

最初,我有一个480x480的内容区域,并希望在侧面添加一个按钮以显示菜单。但是,我能找到让应用程序左侧长度向下运行的唯一方法是将整个应用程序的方向更改为横向。

现在,在横向的方向上,滚动视图从左上角开始,当我真的希望它从原始方向点(从它处于纵向模式时)开始,这将是左下角。但我无法弄清楚如何更改scrollview的原始起点。我尝试过contentOffset,但它似乎没有用。

另一种解决这个问题的方法(我可能最终会这样做)就是忘记在按钮上设置标题,然后为按钮创建一个垂直方向文本的图形。

if (self = [super initWithCoder:coder]) {
    // Size of the scrollview
    self.contentSize = CGSizeMake(480, 510);
    [self setContentOffset:CGPointMake (0, -160)];
    self.showsHorizontalScrollIndicator = YES;
    self.showsVerticalScrollIndicator = YES;
    self.alwaysBounceVertical = NO;
    self.alwaysBounceHorizontal = NO;
    self.bounces = YES;
    self.userInteractionEnabled = YES;
    self.musicGridView = [[[MusicGridView alloc] initWithCoder:coder] autorelease];
    // Rectangle where I put the music grid (origin is left aligned, and down 160 pixels to account for landscape view)
    self.musicGridView.frame = CGRectMake(0, 160, 480, 480);
    [self addSubview: musicGridView];

    self.menuButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.menuButton.frame = CGRectMake(0, 480, 480, 32);
    [menuButton setTitle: @"Tap to get Menu" forState: UIControlStateNormal];
    [menuButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [menuButton addTarget:self action:@selector(showMenu) forControlEvents:UIControlEventTouchUpInside];

    [self addSubview: menuButton];

2 个答案:

答案 0 :(得分:1)

只需在

中设置点x,y即可
//it goes in particular pont of view

[scrollview setContentOffset:CGPointMake(x,y) animated:YES];

//if x=0 in loop, increase y value scrollview move only vertical direction
[scrollview setContentOffset:CGPointMake(0,y) animated:YES];

//if y=0 in loop, increase x values scrollview move only horizontal direction
[scrollview setContentOffset:CGPointMake(x,0) animated:YES];

答案 1 :(得分:0)

Andrey Tarantsov已经完成了UIScrollView类的各种怪癖的awesome rundown,包括针对常见问题的各种变通方法的代码。值得一看。