UIButton没有在imageView中调用事件

时间:2013-10-30 04:19:16

标签: ios uiimageview uibutton

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

3 个答案:

答案 0 :(得分: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];

答案 1 :(得分:1)

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

答案 2 :(得分:0)

在将按钮添加为子视图之前,请将exclusiveTouch属性设置为YES

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

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