shubhiSingh shubhiSingh - 1 year ago 92
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 Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download