Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone UIImageView上的UIButton(UIScrollView上)_Iphone_Uiscrollview_Uiimageview_Uibutton - Fatal编程技术网

Iphone UIImageView上的UIButton(UIScrollView上)

Iphone UIImageView上的UIButton(UIScrollView上),iphone,uiscrollview,uiimageview,uibutton,Iphone,Uiscrollview,Uiimageview,Uibutton,我想显示一个用户可以滚动的图像(比iphone屏幕大)。 不是difficoult,我已经完成了以下代码: 在我的.h文件中 @interface mappa1 : UIViewController <UIScrollViewDelegate> { IBOutlet UIImageView *img; IBOutlet UIScrollView *scroller; } 及 } 一切都很好 现在我想在UIImage上添加UIButton,而不是在UIView上,因为我希望如果用户

我想显示一个用户可以滚动的图像(比iphone屏幕大)。 不是difficoult,我已经完成了以下代码: 在我的.h文件中

@interface mappa1 : UIViewController <UIScrollViewDelegate> {
IBOutlet UIImageView *img;
IBOutlet UIScrollView *scroller;
 }

}

一切都很好

现在我想在UIImage上添加UIButton,而不是在UIView上,因为我希望如果用户缩放或移动图像,UIButton将跟随UIImage的移动/缩放

因此,我添加了以下代码:

UIButton *btn = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
btn.frame = CGRectMake(0, 0, 100, 25);
[btn setTitle:@"Play" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(buttonClick)  forControlEvents:UIControlEventTouchUpInside];
[img addSubview:btn];
但是UIButton是“不可点击的”! 当然,UIView和UIImageView已将UserInteractionEnabled设置为YES

谢谢

编辑:如果我写

[scroller addSubView:btn];

它可以工作,但是,当然,如果用户放大/缩小,UIButton总是大100x25。

此代码可能对您有用

解决了! 我不知道为什么,但是如果我在Interface Builder中选中了
UserInteractionEnabled
(在UIImageView上),它就不起作用了。 如果我写

img.UserInteractionEnabled = YES;

它起作用了

我试过这个,它对我有效

UIImageView * imageView = [[UIImageView alloc]init];
[imageView setImage:[UIImage imageNamed:@"image.jpeg"]];
[imageView setFrame:CGRectMake(10,10,300,300)];
[imageView setContentMode:UIViewContentModeScaleToFill];
[imageView setUserInteractionEnabled:TRUE];

UIButton * ButtonOnImageView = [[UIButton alloc]init];
[ButtonOnImageView setFrame:CGRectMake(135,120,60,30)];
 [ButtonOnImageView setImage:[UIImage imageNamed:@"image.jpeg"]  forState:UIControlStateHighlighted];
 [ButtonOnImageView setImage:[UIImage imageNamed:@"image2.jpeg"] forState:UIControlStateNormal];
 [    ButtonOnImageView addTarget:self action:@selector(OnClickOfTheButton) forControlEvents:UIControlEventTouchUpInside];

 [imageView addSubview:ButtonOnImageView];
 [self.view addSubview:imageView];

ScrollView有一个属性“可取消的内容接触”。您可以在IB中的属性检查器中找到它。取消选中该选项,然后重试。谢谢,但不行,它不起作用!此外,如果我取消选中“Cancelabla content Touchs”,缩放将不起作用!您可能需要根据UIImage上的缩放级别手动调整按钮的大小。因此,每当有人在图像上缩放时,您都可以调整按钮大小。是的,在uiscrollview上使用uibutton。问题是:我如何知道图像的缩放程度?但是没有办法在uiimageview上添加uibutton(就像在我的代码中一样)并使uibutton工作?因此,你必须继承滚动视图来检测子视图,并基于此,你可以将动作事件赋予相同的对象,我给你类似的链接,但你必须按照你的要求解决所有问题
img.UserInteractionEnabled = YES;
UIImageView * imageView = [[UIImageView alloc]init];
[imageView setImage:[UIImage imageNamed:@"image.jpeg"]];
[imageView setFrame:CGRectMake(10,10,300,300)];
[imageView setContentMode:UIViewContentModeScaleToFill];
[imageView setUserInteractionEnabled:TRUE];

UIButton * ButtonOnImageView = [[UIButton alloc]init];
[ButtonOnImageView setFrame:CGRectMake(135,120,60,30)];
 [ButtonOnImageView setImage:[UIImage imageNamed:@"image.jpeg"]  forState:UIControlStateHighlighted];
 [ButtonOnImageView setImage:[UIImage imageNamed:@"image2.jpeg"] forState:UIControlStateNormal];
 [    ButtonOnImageView addTarget:self action:@selector(OnClickOfTheButton) forControlEvents:UIControlEventTouchUpInside];

 [imageView addSubview:ButtonOnImageView];
 [self.view addSubview:imageView];