Изменение содержимого UIScrollView iPhone - PullRequest
0 голосов
/ 01 ноября 2010

Я пытаюсь создать прокручиваемую строку меню (что мне удается сделать), но теперь вместо этого я хочу добавить большую кнопку с изображением, центрированным по кнопке, вместо того, чтобы брать общую ширину и высоту кнопки

Это мой текущий код:

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

Приведенный выше код создает изображение и кнопку над ним, где кнопка и изображение имеют одинаковую ширину и высоту. Я хочу, чтобы кнопка была значительно больше как по высоте, так и по ширине.

Возможно ли это?

Спасибо!

2-я попытка (с помощью Джозефа Туры)

[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 Ответ

1 голос
/ 01 ноября 2010

Почему бы не поместить оба элемента в 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];
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...