简体   繁体   English

UIViewController和UIView中的Touches方法

[英]Touches method in UIViewController and UIView

I'm working on iPad app. 我正在使用iPad应用程序。 My UIViewController contains only a custom UIView with a size of 500wx500h. 我的UIViewController仅包含大小为500wx500h的自定义UIView

I implemented the touches methods both in the UIViewController and the custom UIView in order to call the UIViewController touches methods when we touch all around the custom UIView and call the custom UIView touches methods when we touch inside it. 我在UIViewController和自定义UIView中都实现了touches方法,以便在我们触摸自定义UIView周围时调用UIViewController touches方法,并在内部触摸时调用自定义UIView touches方法。

UIViewController touchesMoved : UIViewController touchesMoved:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.view];

    NSLog(@"TEST"); // Added during edition
    if (!CGRectContainsPoint(self.drawingView.frame, point)) {
        NSLog(@"UIVIEWCONTROLLER");
    }

} }

Custom UIView touches Moved : 自定义UIView触动Moved:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"CUSTOM VIEW");
}

When I'm touching the custom UIView moving my finger, CUSTOM VIEW is logged until I removed the finger on the screen. 当我用手指触摸自定义UIView将记录CUSTOM VIEW ,直到我移开屏幕上的手指为止。 On the other hand, when I'm touching outside of the custom UIView, the UIVIEWCONTROLLER is called only one time in the touchesMoved method. 另一方面,当我触摸自定义UIView之外时,在touchesMoved方法中仅一次调用了UIVIEWCONTROLLER

Am I missing something wrong ? 我错过了什么地方吗?

EDIT : I added a log in my UIViewController touchesMoved method. 编辑:我在我的UIViewController touchesMoved方法中添加了一个日志。 When I touch inside the custom view, it logs the TEST during all the touching phase. 当我在自定义视图中触摸时,它将在所有触摸阶段记录“测试”。 But when I touch outside, I get the same behaviour. 但是当我在外面碰触时,我会得到相同的行为。

向视图添加ExclusiveTouch属性以禁用多点触摸

your view.exclusiveTouch=YES;

There is some likeliness that your self.view is intercepting touches and handling them, so they will not make it through to the view controller. 您的self.view有可能会拦截触摸并对其进行处理,因此它们不会传递给视图控制器。 You could try doing either of 2 things (or both) and see if it works: 您可以尝试执行以下两项操作之一(或同时执行两项操作),然后查看是否可行:

  1. self.view.exclusiveTouch = NO;

  2. self.view.userInteractionEnabled = NO;

I would also try to call [super touchesMoved: touches withEvent: event]; 我也会尝试调用[super touchesMoved: touches withEvent: event];

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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