1

我有许多图像按钮的滚动视图,对于按钮 1-8,按钮行为没有问题。它可以是可点击的日志。

问题是第二页上的按钮看起来不像在选择器上听(按钮 9-10)。页面控制可能有问题吗?或者我在这里错过了什么?

    // button paging
myImages = [NSArray arrayWithObjects:
                     [UIImage imageNamed:@"img_1.png"],
                     [UIImage imageNamed:@"img_2png"],
                     [UIImage imageNamed:@"img_3.png"],
                     [UIImage imageNamed:@"img_4.png"],
                     [UIImage imageNamed:@"img_5.png"],
                     [UIImage imageNamed:@"img_6.png"],
                     [UIImage imageNamed:@"img_7.png"],
                     [UIImage imageNamed:@"img_8.png"],
                     [UIImage imageNamed:@"img_9.png"],
                     [UIImage imageNamed:@"img_10.png"],
                     nil
            ];

// prepare counter
int posX, posY = 5, counter = 0, pages = 0;
BOOL reset = NO;

// uiviewscroller
CGRect scrollFrame;
scrollFrame.origin.x = self.myScrollView.frame.size.width * pages;
scrollFrame.origin.y = 0;
scrollFrame.size = self.myScrollView.frame.size;

UIView *subView = [[UIView alloc] initWithFrame:scrollFrame];
subView.backgroundColor = [UIColor colorWithRed:232.0f/255.0f green:233.0f/255.0f blue:235.0f/255.0f alpha: 1];

// loop over images
for (int i = 0; i < myImages.count; i++)
{
    counter++;

    // reposition X
    if (counter == 1 || counter == 5)
    {
        posX = 4;

        // more than 1 page
        if (pages > 0)
            posX = 320 * pages;
    }
    else
    {
        posX += 80;
    }

    // reposition Y
    posY = (counter <= 4) ? 5 : 85;

    if (reset == YES)
    {
        // reflag
        reset = NO;

        // redefined size
        scrollFrame.origin.x = self.myScrollView.frame.size.width * pages;            
    }

    // create image view for button
    UIImage *myIcon = [myImages objectAtIndex:i];
    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];

    // manipulate button behavior
    myButton.frame = CGRectMake(posX, posY, 80, 80);
    [myButton setTitle:[NSString stringWithFormat:@"%d", i] forState:UIControlStateNormal];
    [myButton setImage:myIcon forState:UIControlStateNormal];
    [myButton addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];

    // add image to subview
    [subView addSubview:myButton];

    if (counter == 8)
    {
        // reset position
        posX = 4;
        posY = 5;

        // populate counter
        counter = 0;
        pages++;

        // reset section
        reset = YES;

        // add subview to view
        [self.myScrollView addSubview:subView];
    }

    // add balance subview to scroller
    if (counter > 0 && i == myImages.count-1)
    {
        // add subview to view
        [self.myScrollView addSubview:subView];
        pages++;
    }
}

scrollFrame.origin.x = self.myScrollView.frame.size.width * pages;
self.myScrollView.contentSize = CGSizeMake(self.myScrollView.frame.size.width * pages, self.myScrollView.frame.size.height);
self.myPaging.numberOfPages = pages;
4

1 回答 1

0

您正在一个名为 subView 的视图上添加按钮。最后两个按钮被添加到该视图的边界之外,因为视图的框架等于滚动视图的框架 ( UIView *subView = [[UIView alloc] initWithFrame:scrollFrame];)。

添加按钮后更改其框架-

在更改滚动视图的内容大小的末尾添加以下行。

[subView setFrame:CGRectMake(subView.frame.origin.x, subView.frame.origin.y, _scrollView.frame.size.width * pages, _scrollView.frame.size.height)];
于 2012-11-22T13:38:24.377 回答