ScrollView包含带有很多按钮的ImageView

时间:2013-06-30 18:06:40

标签: ios uiscrollview uiimageview uibutton

我有一个ScrollView,里面有一个你可以滚动的地图。

在那张地图上我想放置自定义按钮,我将地图上的29个按钮放在ImageView后不要移动。

我已经放置了13个按钮,但只有8个按钮响应触摸,其他只是没有响应。

奇怪的是,你需要触摸按钮图像顶部的8个中的一个才能响应。

这是因为我需要将地图ImageView和按钮放在ContainerView中,然后放在ScrollView中吗?

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    self.positionArray = [[NSMutableArray alloc] init];

    CGRect node1 = CGRectMake(290, 90, 30, 30);
    [self.positionArray addObject:[NSValue valueWithCGRect:node1]];

    CGRect node2 = CGRectMake(238, 159, 30, 30);
    [self.positionArray addObject:[NSValue valueWithCGRect:node2]];

    CGRect node3 = CGRectMake(290, 136, 30, 30);
    [self.positionArray addObject:[NSValue valueWithCGRect:node3]];

    GRect node4 = CGRectMake(341, 159, 30, 30);
    [self.positionArray addObject:[NSValue valueWithCGRect:node4]];

    ...

    CGRect node11 = CGRectMake(166, 318, 30, 30);
    [self.positionArray addObject:[NSValue valueWithCGRect:node11]];

    CGRect node12 = CGRectMake(290, 264, 30, 30);
    [self.positionArray addObject:[NSValue valueWithCGRect:node12]];

    CGRect node13 = CGRectMake(413, 318, 30, 30);
    [self.positionArray addObject:[NSValue valueWithCGRect:node13]];

    //self.positionArray = nil;

    self.nodeArray = [[NSMutableArray alloc] init];

    for(int i = 0; i < [positionArray count]; i++)
    {
        //Create the button
        UIButton *button = [[UIButton alloc] initWithFrame:[[self.positionArray objectAtIndex:i] CGRectValue]];
        [button setImage:[UIImage imageNamed:@"GreenNode.gif"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"Bigger.gif"] forState:UIControlStateHighlighted];
        [button setUserInteractionEnabled:YES];
        [self.nodeArray addObject:button];
    }

    for(UIButton *button in self.nodeArray)
    {
        //add the button to the view
        [self.mapView addSubview:button];

        //add the action
        //[button addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchDown];
    }
}

2 个答案:

答案 0 :(得分:0)

确保所有按钮框架都在imageview的框架和滚动视图框架内。如果UIView在其父级框架之外,则它不响应触摸事件

答案 1 :(得分:0)

- (void)viewWillAppear:(BOOL)animated
{

[super viewWillAppear:animated];

int x=0;

// DataArray is your image array   
for(int i=0;i<[dataArray count];i++)
{       
    if (i==0) {

       // ImageView used for Display button image in ImageView(When button clicked on image)

        ImageView.image=[UIImage imageNamed:[dataArray objectAtIndex:i]];
    }

    UIButton *btnMap =[UIButton buttonWithType:UIButtonTypeCustom];
    btnMap.frame=CGRectMake(x, 0, 65, 65 );

    [btnMap setImage:[UIImage imageNamed:[dataArray objectAtIndex:i]] forState:UIControlStateNormal];

    btnMap.tag=i;

    [btnMap addTarget:self action:@selector(btnImage:) forControlEvents:UIControlEventTouchUpInside];

    [scrollview addSubview:btnMap];

    x=x+65;

    NSLog(@"print x is..%d",x);

    scrollview.contentSize=CGSizeMake(([dataArray count]*70), 50);
}

//When Button click on Image That time it Display in UiImageview

-(IBAction)btnImage:(id)sender
{

UIButton *btn =(UIButton *)sender;
NSLog(@"button id..%d",btn.tag);
//   return ;

if([dataArray count]>0)
{

    NSLog(@"button name is..%@",[dataArray objectAtIndex:btn.tag]);
   ImageView.image=[UIImage imageNamed:[dataArray objectAtIndex:btn.tag]];

}
}