1

我有一个带有 imageview 和半透明按钮的滚动视图。使用 [addsubview] 将按钮和图像视图添加到滚动视图。现在我需要更改图像视图和按钮位置(更改框架属性)

埃斯:

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

完成此操作后,按钮隐藏。如果我只更改按钮位置,它会完美运行。

PS:我刚刚尝试使用 Bringtofront。

滚动视图内容创建:

   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;
        }
    }


}
4

2 回答 2

0

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

于 2013-01-04T10:33:53.603 回答
0

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

并在 didRotateFromInterfaceOrientation 中更改 UIButtons 位置。

这似乎没有解释,但它有效。

于 2013-01-07T11:08:23.767 回答