Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 *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:

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

How should I go about resolving this issue? thanks!

like image 584
ddolce Avatar asked Jul 20 '14 00:07

ddolce


1 Answers

The action selector is a String ending in colon, which tells it there is one parameter (the sender):

let labelTap1=UITapGestureRecognizer(target: self, action: "labelTapped:")
like image 172
vacawama Avatar answered Oct 05 '22 23:10

vacawama