shubhiSingh shubhiSingh - 5 months ago 24
Swift Question

How to use swipe guesture on two views in swift

When I swap from right to left anywhere on the simulator it is swapping but I want to swap only on the View of green.
I have Tried this..

class ViewController: UIViewController {

@IBOutlet weak var red: UIView!
@IBOutlet weak var green: UIView!

override func viewDidLoad() {
super.viewDidLoad()


var sswipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
sswipe.direction = .Left
view.addGestureRecognizer(sswipe)

var srwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
srwipe.direction = .Right
view.addGestureRecognizer(srwipe)

}

func handleSwipes(sender:UISwipeGestureRecognizer) {
if(sender.direction == .Left) {
var labelPosition = CGPointMake(self.view.frame.origin.x - 100.0, self.view.frame.origin.y)
self.view.frame = CGRectMake(labelPosition.x , labelPosition.y , self.view.frame.size.width, self.view.frame.size.height)
}
if(sender.direction == .Right) {
var labelPosition = CGPointMake(self.view.frame.origin.x + 100.0, self.view.frame.origin.y)
self.view.frame = CGRectMake(labelPosition.x , labelPosition.y , self.view.frame.size.width, self.view.frame.size.height);
}
}

Answer

You are adding your gesture to the main view, you need to add it to the desired view in your case its green so it should be

green.addGestureRecognizer(sswipe)
Comments