在我的添加聯繫人頁面中,我有一個視圖和一個滾動視圖,並再次顯示視圖。並在最後一個視圖中,我已經給出了'touchesBegan'方法的文本框等,但它僅被稱爲底部的視圖。如何將該方法指向另一個視圖,即頂部的視圖?如何使'touchesBegan'方法適用於特定視圖?
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.AddView endEditing:YES];
}
在我的添加聯繫人頁面中,我有一個視圖和一個滾動視圖,並再次顯示視圖。並在最後一個視圖中,我已經給出了'touchesBegan'方法的文本框等,但它僅被稱爲底部的視圖。如何將該方法指向另一個視圖,即頂部的視圖?如何使'touchesBegan'方法適用於特定視圖?
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.AddView endEditing:YES];
}
嘗試
其中一種方法是你可以做的:
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch= [touches anyObject];
if ([touch view] == image1)
{
//Action
}
}
請注意:當你正在使用的UIScrollView你可能無法得到觸摸方法UIScrollView的。在這種情況下,您可能需要使用UIGesture。
豎起大拇指提不起'UIScrollView' :) – GoGreen
試試這個:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch1 = [touches anyObject];
CGPoint touchLocation = [touch1 locationInView:self.finalScore];
if(CGRectContainsPoint(YourView.frame, touchLocation));
{
//Do stuff.
}
}
首先檢查整個控件的屬性UserInteractionEnabled
,並設置爲YES
檢查出你的底視圖框架後,其不超過上的觀點
而且之後你可以檢查出波紋狀況,並做一些特定的控制觸摸事件..
-(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
}
}
}
這裏是斯威夫特了答案:
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
let touch = touches.first as! UITouch
if(touch.view == myView){
// Code
}
}
嘿馬諾,你必須接受以下任何答案。它會幫助那些搜索類似問題的人。 – Rushi