【问题标题】:UIButton not calling event in imageViewUIButton没有在imageView中调用事件
【发布时间】:2013-10-30 04:19:15
【问题描述】:

我有imageview,我在其中添加UIButton,当我单击按钮时它会显示按钮,它不会调用按钮的操作。这是我正在使用的代码,我还设置了imageView.userInteraction=enabled,但它仍然没有调用操作。

UIImageView*myImageView=[[UIImageView alloc] initWithFrame:CGRectMake(75,10,200,150)];


myImageView.image=thumbnail;

myImageView.userInteractionEnabled=YES;


[scrollView addSubview:myImageView];

UIButton*playButton=[[UIButton alloc] initWithFrame:CGRectMake(20,20,128,128)];

//[playButton setImage:[UIImage imageNamed:@"playIconPlay.png"] forState:UIControlStateNormal];

[playButton.buttonType:UIButtonTypeRoundedRect];

scrollView.userInteractionEnabled=YES;
myImageView.userInteractionEnabled=YES;
[playButton setTitle:@"Play Click" forState:UIControlStateNormal];

[playButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];


[playButton addTarget:self action:@selector(playButtonAction) forControlEvents:UIControlEventTouchUpInside];


myImageView.userInteractionEnabled=YES;

[myImageView addSubview:playButton];

【问题讨论】:

  • 添加这一行 [myImageView bringSubviewToFront: playButton];然后尝试..
  • 这个也不行
  • imageView 我用作背景很大,这就是我使用按钮的原因
  • @QueenSolutions:用buttontype初始化按钮,然后设置frame。无需为 imageView 设置 userInteractionEnabled。
  • 如果你的imageview不在前面,那么如何将触摸事件传递给它?将您的图像视图放在前面或至少将播放按钮放在前面

标签: ios uiimageview uibutton


【解决方案1】:

你可以在 UIView 中添加 UIImageView 和 UIButton,希望对你有所帮助。

UIView* view = [[UIView alloc] initWithFrame:CGRectMake(75, 10, 200, 150)];

UIImageView*myImageView=[[UIImageView alloc] initWithFrame:CGRectMake(0,0,200,150)];


myImageView.image=thumbnail;


[view addSubview:myImageView];

[scrollView addSubview:view];

UIButton*playButton=[[UIButton alloc] initWithFrame:CGRectMake(20,20,128,128)];

//[playButton setImage:[UIImage imageNamed:@"playIconPlay.png"] forState:UIControlStateNormal];

[playButton.buttonType:UIButtonTypeRoundedRect];

[playButton setTitle:@"Play Click" forState:UIControlStateNormal];

[playButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];


[playButton addTarget:self action:@selector(playButtonAction) forControlEvents:UIControlEventTouchUpInside];


myImageView.userInteractionEnabled=YES;

[view addSubview:playButton];

【讨论】:

    【解决方案2】:
    UIButton*playButton = [UIButton buttonWithType:UIButtonTypeCustom];
    

    试试这个..它会完美地工作..

    [playButton setImage:[UIImage imageNamed:@"playIconPlay.png"] forState:UIControlStateNormal];
    
    [playButton setFrame:CGRectMake(20,20,128,128)];
    
    [playButton setTitle:@"Play Click" forState:UIControlStateNormal];
    
    [playButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [playButton addTarget:self action:@selector(playButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
    
    [scrollView addSubview:playButton];
    [scrollView bringSubviewToFront:playButton];
    

    【讨论】:

    • scoolView 检查命名?
    • @Palmyra:使用scrollView查看上面的女王解决方案。
    • @TaminlKing:[scrollView addSubview:playButton]; [scoolView 带来SubviewToFront:playButton];检查命名这两行是不同的
    【解决方案3】:

    在将您的 Button 添加为子视图之前,请将 exclusiveTouch 属性设置为 YES

    playButton.exclusiveTouch = YES
    [myImageView addSubview:playButton];
    

    希望它会帮助你....享受编码..!!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多