简体   繁体   English

在Swift中获取UITapGestureRecognizer的发件人

[英]Getting UITapGestureRecognizer's sender in Swift

So I'm working on a selector that reminds users to pan the object instead of tapping it. 所以我正在研究一种选择器,它提醒用户平移对象而不是点击它。 The following code is the animation: 以下代码是动画:

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
                })
        })
}

now back to the object itself. 现在回到对象本身。 I'm allocating a UITapGestureRecognizer and adding it to the object, but i'm not so sure how it works, since back in Objective-C, we used to do: 我正在分配一个UITapGestureRecognizer并将其添加到对象中,但我不确定它是如何工作的,因为回到Objective-C,我们曾经做过:

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

where no parameter is needed. 其中不需要参数。 However in Swift, I'll have to put in a parameter in the parenthesis after labelTapped , as it will give me a Missing argument for parameter #1 in call error: 但是在Swift中,我必须在labelTapped之后的括号中加入一个参数,因为它会Missing argument for parameter #1 in call错误中给出Missing argument for parameter #1 in callMissing argument for parameter #1 in call

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

How should I go about resolving this issue? 我该如何解决这个问题? thanks! 谢谢!

动作选择器是一个以冒号结尾的字符串,它告诉它有一个参数(发送者):

let labelTap1=UITapGestureRecognizer(target: self, action: "labelTapped:")
  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)

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM