0

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

在该地图上,我想放置自定义按钮,并且在将它们放置在 ImageView 上之后,我需要地图上的 29 个按钮不要移动。

我已经放置了 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];
    }
}
4

2 回答 2

0

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

于 2013-07-02T09:12:38.200 回答
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]];

}
}
于 2013-07-27T06:35:04.857 回答