我已将左右滑动手势识别器添加到图像视图中,但仅右滑动即可。
let swipeLeftRecognizer = UISwipeGestureRecognizer(target: self, action: "swipeLeft")
swipeLeftRecognizer.direction = .Left
let swipeRightRecognizer = UISwipeGestureRecognizer(target: self, action: "swipeRight")
swipeLeftRecognizer.direction = .Right
if itemImages.count > 1 {
imageView.addGestureRecognizer(swipeLeftRecognizer)
imageView.addGestureRecognizer(swipeRightRecognizer)
for recognizer in imageView.gestureRecognizers! {
recognizer.delegate = self
}
func swipeRight() {
if currentIndex == 0 {
itemImages.last!.getDataInBackgroundWithBlock( {
(data, error) -> Void in
self.imageView.image = UIImage(data: data!)
})
let transition = CATransition()
transition.duration = 1.0
transition.subtype = kCATransitionFromLeft
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
transition.type = kCATransitionReveal
imageView.layer.addAnimation(transition, forKey: nil)
currentIndex = itemImages.count - 1
} else {
itemImages[currentIndex - 1].getDataInBackgroundWithBlock( {
(data, error) -> Void in
self.imageView.image = UIImage(data: data!)
})
let transition = CATransition()
transition.duration = 1.0
transition.subtype = kCATransitionFromLeft
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
transition.type = kCATransitionReveal
imageView.layer.addAnimation(transition, forKey: nil)
currentIndex--
}
}
func swipeLeft() {
let index = itemImages.count - 1
if currentIndex == index {
itemImages[0].getDataInBackgroundWithBlock( {
(data, error) -> Void in
self.imageView.image = UIImage(data: data!)
})
let transition = CATransition()
transition.duration = 1.0
transition.subtype = kCATransitionFromLeft
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
transition.type = kCATransitionReveal
imageView.layer.addAnimation(transition, forKey: nil)
currentIndex = 0
} else {
itemImages[currentIndex + 1].getDataInBackgroundWithBlock( {
(data, error) -> Void in
self.imageView.image = UIImage(data: data!)
})
let transition = CATransition()
transition.duration = 1.0
transition.subtype = kCATransitionFromLeft
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
transition.type = kCATransitionReveal
imageView.layer.addAnimation(transition, forKey: nil)
currentIndex++
}
}
最佳答案
let swipeLeftRecognizer = UISwipeGestureRecognizer(target: self, action: "swipeLeft")
swipeLeftRecognizer.direction = .Left
let swipeRightRecognizer = UISwipeGestureRecognizer(target: self, action: "swipeRight")
swipeRightRecognizer.direction = .Right
///您两次使用了“ swipeLeftRecognizer.direction”。
现在您的leftSwite朝着正确的方向工作
或者,您可以将故事板中的swipeGesture连接到您的“ swipeLeft”和“ swipeRight”方法,而不必在之前编写任何代码
关于ios - 手势识别器不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34907375/