Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.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 touchevents未从内部子视图触发_Iphone_Objective C_Uiview_Event Handling_Uiimageview - Fatal编程技术网

Iphone touchevents未从内部子视图触发

Iphone touchevents未从内部子视图触发,iphone,objective-c,uiview,event-handling,uiimageview,Iphone,Objective C,Uiview,Event Handling,Uiimageview,我在试图通过UIView获取一个连接到它的ChildView的触摸事件时遇到了一些麻烦 我有很多图像,我想用它们做各种事情,所以我对阅读它们上的触摸事件感兴趣。在我决定在UIView中对它们进行分组之前,它工作得很好,所以我猜我需要打开或关闭一些设置来让事件通过 [TouchImageView.h] ------------------------------------------------------- @interface TouchImageView : UIImageView [

我在试图通过UIView获取一个连接到它的ChildView的触摸事件时遇到了一些麻烦

我有很多图像,我想用它们做各种事情,所以我对阅读它们上的触摸事件感兴趣。在我决定在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] ;

有人能告诉我我做错了什么吗?我在这里看到了很多关于这个主题的类似帖子,但大多数都是关于设置userInteractionEnabled(?)

首先
mainMenuView.userInteractionEnabled=NO应为
YES
另外,
UIImageView
userInteractionEnabled
被deafolt设置为NO,因此您必须将其设置为YES

在这条线之后

[mainMenuView addSubview:myImage] ;
//add
myImage.userInteractionEnabled = YES;

首先
mainMenuView.userInteractionEnabled=NO应为
YES
另外,
UIImageView
userInteractionEnabled
被deafolt设置为NO,因此您必须将其设置为YES

在这条线之后

[mainMenuView addSubview:myImage] ;
//add
myImage.userInteractionEnabled = YES;

事实证明,我需要为UIView定义一个框架。所以我改变了

mainMenuView = [[UIView alloc] init] ;
为此:

mainMenuView = [[UIView alloc] initWithFrame:[UIScreen mainScreen] bounds] ;

它又起作用了。

结果是我需要为UIView定义一个框架。所以我改变了

mainMenuView = [[UIView alloc] init] ;
为此:

mainMenuView = [[UIView alloc] initWithFrame:[UIScreen mainScreen] bounds] ;


它又起作用了。

为什么不使用UIGesturerecognizer@AlbrahimZ我不知道一个比另一个好(?)。我认为重载TouchesStart将是最简单的解决方案。没想到它不起作用。我对UIGesturerecognizer不太熟悉-是什么让它成为更好的选择?手势识别器用于高级手势,因此它们在所有应用程序中的行为都是一样的。对于低级别控制和识别器无法完成的操作,您仍然必须在TouchsBegind、TouchsEnded等中实现逻辑。因此,您告诉我,如果我用手势识别器替换TouchsBegin,那么它就不会因为在子视图中而被阻止?为什么不使用UIGesturerecognizer@AlbrahimZ我不知道一个比另一个好(?)。我认为重载TouchesStart将是最简单的解决方案。没想到它不起作用。我对UIGesturerecognizer不太熟悉-是什么让它成为更好的选择?手势识别器用于高级手势,因此它们在所有应用程序中的行为都是一样的。对于低级控制和识别器无法执行的操作,您仍然必须在touchesBegind、toucheSend等中实现逻辑。因此,您告诉我,如果我使用手势识别器替换touchesBegin,那么它不会被子视图阻止?我在其构造函数中将其设置为“是”。关于mainMenuView,我尝试了YES和NO两种方法,但都不起作用。很抱歉,我没有注意到它们,menuButtonWidth和menuButtonHeight的值是什么?这些值只是图像的尺寸,为了示例,它们可以定义为0,02000-正如我在开始时提到的,事件工作正常,直到我将它们添加到mainMenuView,而不是self.viewcontroller.view视图。mainMenuView的框架是什么?它没有框架-显然是它导致它无法触发的原因。一旦我用rect将代码更改为init,它就开始工作了-谢谢!我在其构造函数中将其设置为“是”。关于mainMenuView,我尝试了YES和NO两种方法,但都不起作用。很抱歉,我没有注意到它们,menuButtonWidth和menuButtonHeight的值是什么?这些值只是图像的尺寸,为了示例,它们可以定义为0,02000-正如我在开始时提到的,事件工作正常,直到我将它们添加到mainMenuView,而不是self.viewcontroller.view视图。mainMenuView的框架是什么?它没有框架-显然是它导致它无法触发的原因。一旦我用rect将代码更改为init,它就开始工作了-谢谢!