更改UIScrollView iPhone的内容

时间:2010-11-01 07:52:23

标签: iphone objective-c uiscrollview

我正在尝试创建一个可滚动的菜单栏(我正在成功),但现在我想添加一个大按钮,图像以按钮为中心,而不是占用按钮的总宽度和高度

这是我目前的代码:

[scrollView setBackgroundColor:[UIColor whiteColor]];

[scrollView setCanCancelContentTouches:NO];

scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;

scrollView.clipsToBounds = YES;

scrollView.scrollEnabled = YES;

UIImage *image;

UIImageView *iv;

UIButton *button;

count = [returned count];

for (int i=0; i<count; i++)

{

image = [UIImage imageNamed:@"coat2.jpg"];

iv = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,105,120)];

button = [[UIButton alloc] initWithFrame:CGRectMake(0,0,105,120)];

[button setAlpha:100];

[button setTag:1];

[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

iv.image = image;

iv.tag=@"coat2.jpg";

iv.userInteractionEnabled = YES;

[iv insertSubview:button atIndex:0];

[button release];

[scrollView addSubview:iv];

[iv release];

}

[scrollView setScrollEnabled:YES];

[self layoutScrollImages];

scrollView.delegate = self;

上面的代码在其上创建了一个图像和按钮,其中按钮和图像具有完全相同的宽度和高度。我希望按钮的高度和宽度都要大得多。

这可能吗?

谢谢!

第二次尝试(使用Joseph Tura的帮助)

[scrollView setBackgroundColor:[UIColor whiteColor]];

    [scrollView setCanCancelContentTouches:NO];

    scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;

    scrollView.clipsToBounds = YES;

    scrollView.scrollEnabled = YES;

    UIImage *image;

    count = [returned count];

    for (int i=0; i<count; i++)

    {
    image = [UIImage imageNamed:@"coat2.jpg"];
                iv = [[UIImageView alloc] initWithFrame:CGRectMake(0,20,105,120)];

        button = [[ProductButton alloc] initWithFrame:CGRectMake(0,20,105,120)];

        [button setAlpha:100];
        [button setTag:1];
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

        iv.tag=tag;
        iv.userInteractionEnabled = YES;
        iv.image = myimage;

        UIView *uiView = [[UIView alloc] initWithFrame:CGRectMake(0,20,105,120)];
        [uiView addSubview:iv];
        button = [[ProductButton alloc] initWithFrame:CGRectMake(0,0,210,240)];
        button.center = CGPointMake(uiView.frame.size.width /2, uiView.frame.size.height/2);
        [button setTag:1];

        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [uiView addSubview:button];

        [scrollView addSubview:uiView];
}
                [scrollView setScrollEnabled:YES];

    [self layoutScrollImages];

    scrollView.delegate = self;


-----

-(void)layoutScrollImages
{
    //UIImageView *view = nil;
    UIView *view = nil;
    NSArray *subviews = [scrollView subviews];
    CGFloat curXLoc = 0;
    for (view in subviews)
    {
        if ([view isKindOfClass:[UIView class]] && view.tag > 0)
        {
            CGRect frame = view.frame;
            frame.origin = CGPointMake(curXLoc, 0);
            view.frame = frame;

            curXLoc += 110;
        }
    }
    [scrollView setContentSize: CGSizeMake(count*110, [scrollView bounds].size.height)];
}

1 个答案:

答案 0 :(得分:1)

为什么不将两个元素都放在UIView中?

UIView *aView = [[UIView alloc] initWithFrame:iv.frame];

[aView addSubview:iv];

button = [[UIButton alloc] initWithFrame:CGRectMake(0,0,210,240)];
button.center = CGPointMake(aView.frame.size.width / 2, aView.frame.size.height / 2);
[aView addSubview:button];