我在尝试通过UIView获取触摸事件时遇到了麻烦,并附上了儿童视图。
我想要做各种各样的图像,所以我有兴趣阅读它们上的触摸事件。它工作正常,直到我决定将它们分组为UIView,所以我猜我需要打开或关闭一些设置以让事件通过。
[TouchImageView.h] -------------------------------------------------------
@interface TouchImageView : UIImageView
[TouchImageView.m] -------------------------------------------------------
- (id)initWithFrame:(CGRect)aRect {
if (self = [super initWithFrame:aRect]) {
// We set it here directly for convenience
// As by default for a UIImageView it is set to NO
self.userInteractionEnabled = YES;
state = 0 ;
rect = aRect ;
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// Do what you want here
NSLog(@"touchesBegan!" );
}
[in my delegate's didFinishLaunchingWithOptions] -------------------------
NSArray *myImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"front-photos.jpg"],
[UIImage imageNamed:@"front-films.jpg"],
[UIImage imageNamed:@"front-presentations.jpg"],
mainMenuView = [[UIView alloc] init ] ;
mainMenuView.userInteractionEnabled = YES; // tried setting this to both YES and NO. Same result.
[self.viewController.view addSubview:mainMenuView] ;
myImage = [[TouchImageView alloc] initWithFrame:CGRectMake(0.0f, menuTopHeight, menuButtonWidth, menuButtonHeight)];
[myImage setImage:[myImages objectAtIndex:0]] ;
[mainMenuView addSubview:myImage] ;
有人可以告诉我我做错了什么吗?我在这个主题上看过很多关于SO的类似帖子,但大多数关于设置userInteractionEnabled(?)
答案 0 :(得分:1)
事实证明,我需要为UIView定义一个框架。所以我改变了
mainMenuView = [[UIView alloc] init] ;
进入这个:
mainMenuView = [[UIView alloc] initWithFrame:[UIScreen mainScreen] bounds] ;
它再次起作用。
答案 1 :(得分:0)
首先mainMenuView.userInteractionEnabled = NO;
应为YES
此外UIImageView
userInteractionEnabled
已将[mainMenuView addSubview:myImage] ;
//add
myImage.userInteractionEnabled = YES;
设为NO,因此您必须将其设置为YES
此行之后
{{1}}