I'm trying to implement swipeGestureRecognizer for all four directions in a swift app, on a bunch of dynamically generated UIViews. So basically, I want each of these views to respond to swipes in all four directions. Here is my code
Code:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//create a view to represent the main box
let mainBox = UIView(frame: CGRectMake(0,0, 320, 40 ))
mainBox.backgroundColor = UIColor.redColor()
mainBox.layer.cornerRadius = 0
mainBox.layer.borderWidth=1
self.view.addSubview(mainBox) //Add the newly created view to the main view(self)
//Create some UIViews on the fly
for var i=0; i<8; ++i{
//Add an image view
let tile = UIImageView(frame: CGRectMake(CGFloat(i)*CGFloat(40),0,40,40 )) //Create a new view
//Style the imageview
tile.backgroundColor=UIColor.greenColor()
tile.layer.cornerRadius=2
tile.layer.borderWidth=1
tile.userInteractionEnabled = true
let swipeRight = UISwipeGestureRecognizer(target: self, action:Selector("tileRightSwiped:"))
swipeRight.direction = .Right
tile.addGestureRecognizer(swipeRight)
let swipeLeft = UISwipeGestureRecognizer(target: self, action: Selector("tileLeftSwiped"))
swipeLeft.direction = .Left
tile.addGestureRecognizer(swipeLeft)
let swipeDown = UISwipeGestureRecognizer(target: self, action: Selector("tileDownSwiped"))
swipeDown.direction = .Down
tile.addGestureRecognizer(swipeDown)
let swipeUp = UISwipeGestureRecognizer(target: self, action: Selector("tileUpSwiped"))
swipeUp.direction = .Up
tile.addGestureRecognizer(swipeUp)
mainBox.addSubview(tile) //Add the newly created view to mainBox
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//swipe gestures
func tileRightSwiped(gestureRecognizer: UISwipeGestureRecognizer){
print("right swiped")
}
func tileLeftSwiped(gestureRecognizer: UISwipeGestureRecognizer){
print("left swiped ")
}
func tileDownSwiped(gestureRecognizer: UISwipeGestureRecognizer){
print("down swiped ")
}
func tileUpSwiped(gestureRecognizer: UISwipeGestureRecognizer){
print("Up swiped ")
}
}
Right swipe works just fine but I can't understand why for the other 3 directions, the app aborts unexpectedly and I get an error like this
"[myApp.ViewController tileDownSwiped]: unrecognized selector sent to instance 0x7fd230dbf5d0" Am I incorrectly using the swipegesture recognizer or is something wrong somewhere else in my code?
I'm at a total loss. Any help greatly appreciated.