在滚动视图中更改位置时,UIButton会隐藏

时间:2013-01-04 10:25:14

标签: iphone ios ipad

我在这张图片上有一个带有imageview和半透明按钮的scrollView。 使用[addsubview]将按钮和imageview添加到scrollview。 现在我需要更改imageview和按钮位置(更改框架属性)

ES:

button.frame = CGRectMake( x, y, w, h );

执行此操作后按钮隐藏。如果我只改变按钮位置就可以完美地工作。

PS:我刚刚尝试使用了前线。

scrollview内容创建:

   int index = 0;
    int x = OFFSETXCOPERTINA;
    int y = 0;
    int w = LARGHEZZACOPERTINA;
    int h = ALTEZZACOPERTINA;
    int riga = 0;
    int indexriga = 0;
    int testindex = 0;

    if (self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation ==UIInterfaceOrientationPortraitUpsideDown) {
        testindex = NRCopertineVerticale;
    }
    else {
        testindex = NRCopertineOrizzontale;
    }

    for (NSDictionary *rigaordine in ElencoOrdini)
    {

        if (indexriga > 0) x = x + OFFSETXCOPERTINA + w;
        y = OFFSETYCOPERTINA + (riga * (h + OFFSETYCOPERTINA));

        UIButton *buttonCopertina = [UIButton buttonWithType:UIButtonTypeCustom];
        buttonCopertina.frame = CGRectMake(x, y, w, h);

        [buttonCopertina setBackgroundColor:[UIColor colorWithRed:(255/255.0) green:(255/255.0) blue:(0/255.0) alpha:0.3]];

        UIImage *image = [[UIImage alloc] initWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: [NSString stringWithFormat: @"%@image.php?CODe=%@", URLXXX, [rigaordine objectForKey:@"cod_isb"]]]]];

        UIImageView *copertina = [[UIImageView alloc] initWithImage: image];
        [copertina setTag:TAGCopertine + index];
        [copertina setFrame:CGRectMake(x, y, w, h)];

        [buttonCopertina setTag:TAGButtonCopertine + index];
        [buttonCopertina addTarget:self action:@selector(buttonCopertinaClicked:) forControlEvents:UIControlEventTouchUpInside];

        [scrollView addSubview:copertina];
        [scrollView addSubview:buttonCopertina];

        index++;
        indexriga++;

        if (indexriga >= testindex) {
            indexriga = 0;
            riga++;
            x = 10;
        }

    }

当我旋转iPad时:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

    int index = 0;
    int x = OFFSETXCOPERTINA;
    int y = 0;
    int w = LARGHEZZACOPERTINA;
    int h = ALTEZZACOPERTINA;
    int riga = 0;
    int indexriga = 0;
    int testindex = 0;

    if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation ==UIInterfaceOrientationPortraitUpsideDown) {
        testindex = NRCopertineVerticale;
    }
    else {
        testindex = NRCopertineOrizzontale;
    }

    for(int j = 0; j< 999; j++) {
        if (indexriga > 0) x = x + OFFSETXCOPERTINA + w;
        y = OFFSETYCOPERTINA + (riga * (h + OFFSETYCOPERTINA));

        UIImageView *copertina = (UIImageView *)[self.scrollView viewWithTag:(TAGCopertine + j)];
        if (copertina != nil) {
            copertina.frame = CGRectMake( x, y, LARGHEZZACOPERTINA, ALTEZZACOPERTINA );
        }

        UIButton *button = (UIButton *)[self.scrollView viewWithTag:(TAGButtonCopertine + j)];
        if (button != nil) {
            button.frame = CGRectMake( x, y, LARGHEZZACOPERTINA, ALTEZZACOPERTINA );
        }

        index++;
        indexriga++;

        if (indexriga >= testindex) {
            indexriga = 0;
            riga++;
            x = 10;
        }
    }


}

2 个答案:

答案 0 :(得分:0)

可能你正在设置一些不正确的按钮框,它会离开屏幕。这里正确的方法是创建一个UIView子类,其中包含UIImageViewUIButton。公开属性以进行设置和图像,并创建delegate以触摸按钮。然后,使用按钮实例化您的视图,设置图像,设置其委托并将其添加为UIScrollView的子视图,并仅使用按钮更改视图的框架 - 所有内容都将相应地重新定位。

答案 1 :(得分:0)

经过数千次测试后,我发现的唯一方法是在willRotateToInterfaceOrientation中更改UIImageViews的位置。

并更改了didRotateFromInterfaceOrientation中的UIButtons位置。

这似乎没有解释但是有效。

相关问题