Reputation: 125
I have written the code to swipe an image in the gallery app. When it is popped up to a large image,I want to swipe the images left and right and written the following code.But its still not working.Can anybody suggest whats wrong with the code?
override func viewDidLoad() {
super.viewDidLoad()
self.imageview.image = self.Image
var leftSwipe = UISwipeGestureRecognizer(target:self, action: Selector("handleSwipes:"))
var rightSwipe = UISwipeGestureRecognizer(target:self, action: Selector("handleSwipes:"))
leftSwipe.direction = .Left
rightSwipe.direction = .Right
view.addGestureRecognizer(leftSwipe)
view.addGestureRecognizer(rightSwipe)
}
func handleSwipes(sender: UISwipeGestureRecognizer) {
if (sender.direction == .Left){
self.imageview.image = self.Image
}
if (sender.direction == .Right){
self.imageview.image = self.Image
}
}
Upvotes: 3
Views: 1276
Reputation: 2451
assuming that you have an array of UIImage
s called imagesArray
var currentIndex = 0{
didSet{
self.imageview.image = imagesArray[currentIndex]
}
} // instance variable
func handleSwipes(sender: UISwipeGestureRecognizer)
{
if (sender.direction == .Left)
{
if currentIndex < imagesArray.count - 1
{
currentIndex++
}
}
else
{
if currentIndex > 0
{
currentIndex--
}
}
}
Upvotes: 1
Reputation: 359
handleSwipes func calls self.Image and it is never updated since it is allocated(i.e.,self.Image one image itself ) in viewdidload.Just pass different image for left and right swipe Just pass different image in left and right swipe. if (sender.direction == .Left){ self.imageview.image = self.Image }
if (sender.direction == .Right){
self.imageview.image = UIImage(named: "myImage.png");
}
Upvotes: 1