单击直接查看时,ios locationInView无法显示正确的日志

时间:2015-05-08 04:05:05

标签: ios objective-c

我觉得很奇怪,我有黑暗视图(darkScreenView)和绿色视图。

我使用

 -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
 {
     UITouch *touch = [[event allTouches] anyObject];
     CGPoint touchViewPoint = [touch locationInView:touch.view];

      NSLog(@"[self.darkScreenView pointInside:touchViewPoint withEvent:event]:%d",[self.darkView pointInside:touchViewPoint withEvent:event]);
 }

当我点击暗视图或绿色视图时,

日志仍显示1.为什么?

我点击绿色视图应显示log 0,但我仍然得到日志结果1。

enter image description here

enter image description here

我正在使用xcode 6.3.1。

有谁知道这种情况有什么问题?

谢谢〜

4 个答案:

答案 0 :(得分:0)

尝试触摸不同的观点..

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
 {
  UITouch *touch = [[event allTouches] anyObject];
  if (touch.view==GREEN_VIEW)
  {
    //Write your code for green view here
  }
  else if(touch.view==DARK_VIEW)
  {
    //Write your code for dark view here
  }
}

答案 1 :(得分:0)

您的touchViewPoint的位置是相对于触摸的视图。因此,获取相对于self.darkView的位置,如下所示。

*** Fetching realm-cocoa
*** Downloading realm-cocoa at "v0.92.1"
Error Domain=NSCocoaErrorDomain Code=4 "The file “Realm.framework” doesn’t exist." 
UserInfo=0x7ffdb87739d0 {NSSourceFilePathErrorKey=/private/var/folders/zz/zyxvpxvq6csfxvn_n0000000000000/T/carthage-archive.khSkep/Carthage/Build/iOS/RealmSwift.framework/Frameworks/Realm.framework, NSUserStringVariant=(
Copy
),
NSDestinationFilePath=/Users/username/Documents/Projects/projName/Carthage/Build/iOS/Realm.framework, NSFilePath=/private/var/folders/zz/zyxvpxvq6csfxvn_n0000000000000/T/carthage-archive.khSkep/Carthage/Build/iOS/RealmSwift.framework/Frameworks/Realm.framework, NSUnderlyingError=0x7ffdb8762d50 "The operation couldn’t be completed. No such file or directory"}

答案 2 :(得分:0)

以下是您所指的View的问题更改。

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
 {
     UITouch *touch = [[event allTouches] anyObject];
     CGPoint touchViewPoint = [touch locationInView:darkView];   <----- Problem

      NSLog(@"[self.darkScreenView pointInside:touchViewPoint withEvent:event]:%d",[self.darkView pointInside:touchViewPoint withEvent:event]);
 }

答案 3 :(得分:0)

这将有效

UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchViewPoint = [touch locationInView:touch.view];
    CGPoint  pointOfDarkView = [touch.view convertPoint:touchViewPoint toView:self.darkview];
    NSLog(@"[self.darkScreenView pointInside:touchViewPoint withEvent:event]:%d",[self.darkview pointInside:pointOfDarkView withEvent:event]);

您的问题:

touchViewPoint相对于touch.view坐标

您必须先将点转换为 darkview坐标,然后才能使用pointInside:pointOfDarkView