2014-07-20 61 views
4

因此,我正在研究一個提醒用戶平移對象而不是點擊它的選擇器。 以下代碼是動畫:在Swift中獲取UITapGestureRecognizer的發件人

func labelTapped(sender:UITapGestureRecognizer)->Void{ 
    UIView.animateWithDuration(0.2, delay: 0, options: UIViewAnimationOptions.CurveEaseIn, animations: { 
     sender.view.frame = CGRectMake(sender.view.frame.origin.x + 15, sender.view.frame.origin.y, sender.view.frame.width, sender.view.frame.height) 
     }, completion: 
     {(value:Bool) in 
      UIView.animateWithDuration(0.2, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: { 
       sender.view.frame = CGRectMake(sender.view.frame.origin.x - 15, sender.view.frame.origin.y, sender.view.frame.width, sender.view.frame.height) 
       }, completion: 
       {(value:Bool) in 
       }) 
     }) 
} 

現在回到對象本身。我分配UITapGestureRecognizer並將其添加到該對象,但我不能肯定它是如何工作的,因爲早在Objective-C中,我們用來做:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(labelTapped)]; 

這裏不需要參數。但是在斯威夫特,我得把一個參數在括號labelTapped後,因爲它會給我一個Missing argument for parameter #1 in call錯誤:

let labelTap1=UITapGestureRecognizer(target: self, action: labelTapped()) 

我應該如何去有關解決此問題?謝謝!

回答

4

的行動選擇在冒號結尾的字符串,告訴它有一個參數(發件人):

let labelTap1=UITapGestureRecognizer(target: self, action: "labelTapped:") 
+0

你搖滾!謝謝。 –

1
override func viewDidAppear(animated: Bool) { 
     super.viewDidAppear(animated) 

     let tapRecognizer = UITapGestureRecognizer(target: self, action: "handleSingleTap:") 
     tapRecognizer.numberOfTapsRequired = 1 
     self.view.addGestureRecognizer(tapRecognizer) 

    } 

func handleSingleTap(recognizer: UITapGestureRecognizer) { 

     self.view.endEditing(true) 

    } 
相關問題