所以我正在开发一个选择器,它提醒用户平移对象而不是点击它。 以下代码是动画:
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)];
其中不需要参数。但是在Swift中,我必须在labelTapped
之后在括号中输入一个参数,因为它会给我一个Missing argument for parameter #1 in call
错误:
let labelTap1=UITapGestureRecognizer(target: self, action: labelTapped())
我该如何解决这个问题?谢谢!
答案 0 :(得分:4)
动作选择器是一个以冒号结尾的字符串,它告诉它有一个参数(发送者):
let labelTap1=UITapGestureRecognizer(target: self, action: "labelTapped:")
答案 1 :(得分: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)
}