xcode iOS:在子视图内滑动手势
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8659186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS: swipe gesture inside a subview
提问by cyclingIsBetter
I should implement a swipe gesture inside a subview; this subview is a oblique view
我应该在子视图中实现滑动手势;这个子视图是一个斜视图
view1.transform = CGAffineTransformMakeRotation( ( 54 * -M_PI ) / 180 );
I want implement the swipe gesture inside this view, if it happens I should have a NSLog that say that swipe happens inside this view, is it possible?
我想在这个视图中实现滑动手势,如果发生这种情况我应该有一个 NSLog 说在这个视图中发生滑动,这可能吗?
回答by Daan van Hasselt
Just as with any other UIView, it's possible to add a gesture recognizer:
就像任何其他 UIView 一样,可以添加手势识别器:
UISwipeGestureRecognizer *swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
[view1 addGestureRecognizer:swipeGestureRecognizer];
The problem lies in the UISwipeGestureRecognizer's property 'direction'. Apple documentation about this property:
问题在于 UISwipeGestureRecognizer 的属性“方向”。关于此属性的 Apple 文档:
The permitted direction of the swipe for this gesture recognizer.
此手势识别器允许的滑动方向。
Because the view is rotated, directions rotate along. If the view is rotated 180 degrees and the user swipes right, the gesture recognizer sees it as a left swipe. I'd suggest using a wrapper view on which the gesture recognizer should be placed. Try this:
因为视图是旋转的,所以方向也随之旋转。如果视图旋转 180 度并且用户向右滑动,则手势识别器会将其视为向左滑动。我建议使用应该放置手势识别器的包装视图。尝试这个:
UIView *view2 = [[UIView alloc] initWithFrame:view1.frame];
[view1.superview addSubview:view2];
view2.backgroundColor = [UIColor clearColor];
UISwipeGestureRecognizer *swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
[view2 addGestureRecognizer:swipeGestureRecognizer];
The disadvantage is that there are certain areas within view2 but outside view1 which will respond to the gesture recognizer.
缺点是在 view2 内但在 view1 外有某些区域会响应手势识别器。