ios 如何使“touchesBegan”方法适用于特定视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17467913/
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
How to make 'touchesBegan' method work for a specific view?
提问by Mano
In my add contact page, i have a view and a scrollview on it and again a view on it. and in that last view i ve textboxes, etc. i have given the 'touchesBegan' method but it is called only for the view at the bottom. how to point that method to another view i.e., the view at the top?
在我的添加联系人页面中,我有一个视图和一个滚动视图,还有一个视图。在最后一个视图中,我有文本框等。我给出了 'touchesBegan' 方法,但它只为底部的视图调用。如何将该方法指向另一个视图,即顶部的视图?
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.AddView endEditing:YES];
}
回答by Rushi
One way this is how you can do :
一种方法是您可以这样做:
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch= [touches anyObject];
if ([touch view] == image1)
{
//Action
}
}
Please Note :As you are using UIScrollView you might not able to get touches method for UIScrollView. In that case you might have to use UIGesture.
请注意:当您使用 UIScrollView 时,您可能无法获得 UIScrollView 的 touches 方法。在这种情况下,您可能必须使用 UIGesture。
回答by Andre Yonadam
Here is the answer in Swift:
这是斯威夫特的答案:
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
let touch = touches.first as! UITouch
if(touch.view == myView){
// Code
}
}
回答by Paras Joshi
First check the property UserInteractionEnabled
of that whole controls and set to YES
首先检查UserInteractionEnabled
整个控件的属性并设置为YES
After check out your bottom view frame that its not over on that views
检查您的底部视图框架后,该视图尚未结束
And after you can checkout that with bellow condition and do something with particular controls touch event..
在你可以用波纹管条件检查它并用特定的控件触摸事件做一些事情之后..
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
[touch locationInView:viewBoard];
if([touch.view isKindOfClass:[UIImageView class]])
{
UIImageView *tempImage=(UIImageView *) touch.view;
if (tempImage.tag == yourImageTag)
{
/// write your code here
}
}
}
回答by SAMIR RATHOD
try this :
尝试这个 :
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch1 = [touches anyObject];
CGPoint touchLocation = [touch1 locationInView:self.finalScore];
if(CGRectContainsPoint(YourView.frame, touchLocation));
{
//Do stuff.
}
}
回答by geo
try
尝试
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event touchesForView:vTouch] anyObject];
if(!touch)
return;
CGPoint pointNow = [touch locationInView:otherView];
{
// code here
}
}
回答by codeedoc
Referring to the Apple documentation on UIResponder, all UIView objects (which includes UIWindow), UIApplication object, UIViewController objects are all instances of UIResponder. To handle a specific type of event, a responder must override the corresponding methods.
参考苹果关于 UIResponder 的文档,所有 UIView 对象(包括 UIWindow)、UIApplication 对象、UIViewController 对象都是 UIResponder 的实例。要处理特定类型的事件,响应者必须覆盖相应的方法。
In our case touches
is our type of event. So our responder should implement the following methods.
touchesBegan(:with:), touchesMoved(:with:), touchesEnded(:with:), and touchesCancelled(:with:)
在我们的例子中touches
是我们的事件类型。所以我们的响应者应该实现以下方法。touchesBegan( :with:)、touchesMoved(:with:)、touchesEnded( :with:)和touchesCancelled(:with:)
Since we only wish to know when a user has touched a particular view, we only need to implement touchesBegan(_:with:)
. Since we are not overriding the other methods we must call super.touchesBegan(touches, with: event)
. If we were overriding ALLof the other methods, we wouldn't be required to call super
.
由于我们只想知道用户何时触摸了特定视图,因此我们只需要实现touchesBegan(_:with:)
. 由于我们没有覆盖其他方法,因此我们必须调用super.touchesBegan(touches, with: event)
. 如果我们覆盖所有其他方法,我们就不需要调用super
.
Looking at touchesBegan(_:with:)
the parameter touches
is a set of UITouch instances. Each instance represents the touches for the starting phase of the event, which is represented by the parameter event
.
查看touchesBegan(_:with:)
参数touches
是一组 UITouch 实例。每个实例表示事件开始阶段的触摸,由参数 表示event
。
For touches in a view, this set contains only one touch by default. Hence touches.first
is the only UITouch instance in the set. We then access the property view
, this represents the view or window in which the touch occurred. Lastly we compare the view that has been touched with your desired view.
对于视图中的触摸,此集合默认仅包含一个触摸。因此touches.first
是集合中唯一的 UITouch 实例。然后我们访问属性view
,它表示发生触摸的视图或窗口。最后,我们将触摸过的视图与您想要的视图进行比较。
It should be noted that if you wish to receive multiple touches- you must set the view's isMultipleTouchEnabled
property to true
. Then the set of touches
will have more than one UITouch instance and you will have to handle that accordingly.
应该注意的是,如果您希望接收多次触摸 - 您必须将视图的isMultipleTouchEnabled
属性设置为true
。那么一组touches
将有多个 UITouch 实例,您将不得不相应地处理它。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
if let touch = touches.first, touch.view == myView {
// Do something
}
}
回答by Mahmut K.
If your view is a parent view then can use this code:
如果您的视图是父视图,则可以使用以下代码:
if let touch = touches.first {
let position = touch.location(in: yourView)
let pnt: CGPoint = CGPoint(x: position.x, y: position.y)
if (yourView.bounds.contains(pnt)) {
//You can use: yourView.frame.contains(pnt)
//OK.
}
}