【问题标题】:ScrollView containing ImageView with a lot of buttons包含带有很多按钮的 ImageView 的 ScrollView
【发布时间】:2013-06-30 18:06:39
【问题描述】:

我有一个 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];
    }
}

【问题讨论】:

  • 如何创建图像视图?
  • 我使用storyboard在其中创建了ScrollView和ImageView。

标签: ios uiscrollview uiimageview uibutton


【解决方案1】:

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

【讨论】:

    【解决方案2】:
    - (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]];
    
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-06-11
      • 1970-01-01
      • 2014-12-05
      • 1970-01-01
      • 2017-03-26
      • 1970-01-01
      • 2018-06-23
      • 1970-01-01
      • 2012-07-04
      相关资源
      最近更新 更多