简体   繁体   中英

About tap gesture in uiscrollview

I have an imageview in my scrollview,and i add a tap gesture for my uiscrollview. 在此处输入图片说明

only the imageview area can respond tap gesture.

why when i set the backgroudcolor property of scrollview,then the whole scrollview can respond tap gesture。

here is my code。

UIImageView * imgview=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bizhi1.png"]];    
imgview.frame=CGRectMake(0, 140, 320, 200);    

UIScrollView * scrollview=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[scrollview addSubview:imgview];

//when i set the backgroudcolor,then the whole scrollview can respond tap gesture
//scrollview.layer.backgroundColor=[UIColor redColor].CGColor;

//add a TapGesture for scrollview
UITapGestureRecognizer * tapGesture=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SwitchTabBarHidden)];    
[scrollview addGestureRecognizer:tapGesture];
[tapGesture release];

[imgview release];
[self.view addSubview:scrollview];
[scrollview release];

我确定这段代码是正确的,因为我已经实现了这段代码,不需要设置scrollview的backgroundColor属性。

确保在UIImageView上将userInteractionEnabled设置为YES:

frame.userInteractionEnabled = YES;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM