滑动 UIImageView 数组循环
Swipe UIImageView array loop
我是新来的,也是Swift的初学者!
我正在尝试创建一个左右滑动的 UIImageView,当我到达最后一张图片时应用程序崩溃,如果我在第一张图片中并尝试向右滑动,也会发生同样的崩溃!
我想做的是当我的图片数组到达最后一张图片时,循环将我送回第一张图片,有人可以帮我吗?
先感谢您!!!
这是我的代码;
func swiped(手势:UIGestureRecognizer){
CATransaction.begin()
CATransaction.setAnimationDuration(animationDuration)
CATransaction.setCompletionBlock {
let delay = dispatch_time(DISPATCH_TIME_NOW, Int64(self.switchingInterval * NSTimeInterval(NSEC_PER_SEC)))
dispatch_after(delay, dispatch_get_main_queue()) {
}
}
let transition = CATransition()
transition.type = kCATransitionFade
chracters.layer.addAnimation(transition, forKey: kCATransition)
CATransaction.commit()
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.Right :
print("User swiped right")
// decrease index
imageIndex -= 1
// check if index is in range
if swipePosition > imageList.count-1{
// Do Nothing
} else {
swipePosition += 1
}
chracters.image = UIImage(named: imageList[imageIndex])
case UISwipeGestureRecognizerDirection.Left:
print("User swiped Left")
// increase index first
imageIndex += 1
// check if index is in range
if swipePosition == 0 {
//Do Nothing
} else {
swipePosition -= 1
}
chracters.image = UIImage(named: imageList[imageIndex])
default:
break;
//stops the code
}
}
}
首先你的条件似乎不明确,你可以使用 swipePosition 本身而不是 imageIndex。其次,您的 chracters.image 应该在 if/else 块内。以下是更新后的片段。如果你想使用 imageIndex,你可以相应地改变它。还添加了从最后一张图片切换到第一张图片以及从第一张图片切换到最后一张的代码。
if swipePosition > imageList.count-1{
swipePosition = 0
chracters.image = UIImage(named:imageList[swipePosition]
} else {
chracters.image = UIImage(named:imageList[swipePosition]
swipePosition += 1
}
case UISwipeGestureRecognizerDirection.Left:
print("User swiped Left")
// check if index is in range
if swipePosition == 0 {
swipePosition = imageList.count-1
chracters.image = UIImage(named:imageList[swipePosition]
} else {
chracters.image = UIImage(named:imageList[swipePosition]
swipePosition -= 1
}
我是新来的,也是Swift的初学者!
我正在尝试创建一个左右滑动的 UIImageView,当我到达最后一张图片时应用程序崩溃,如果我在第一张图片中并尝试向右滑动,也会发生同样的崩溃! 我想做的是当我的图片数组到达最后一张图片时,循环将我送回第一张图片,有人可以帮我吗? 先感谢您!!! 这是我的代码;
func swiped(手势:UIGestureRecognizer){ CATransaction.begin()
CATransaction.setAnimationDuration(animationDuration)
CATransaction.setCompletionBlock {
let delay = dispatch_time(DISPATCH_TIME_NOW, Int64(self.switchingInterval * NSTimeInterval(NSEC_PER_SEC)))
dispatch_after(delay, dispatch_get_main_queue()) {
}
}
let transition = CATransition()
transition.type = kCATransitionFade
chracters.layer.addAnimation(transition, forKey: kCATransition)
CATransaction.commit()
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.Right :
print("User swiped right")
// decrease index
imageIndex -= 1
// check if index is in range
if swipePosition > imageList.count-1{
// Do Nothing
} else {
swipePosition += 1
}
chracters.image = UIImage(named: imageList[imageIndex])
case UISwipeGestureRecognizerDirection.Left:
print("User swiped Left")
// increase index first
imageIndex += 1
// check if index is in range
if swipePosition == 0 {
//Do Nothing
} else {
swipePosition -= 1
}
chracters.image = UIImage(named: imageList[imageIndex])
default:
break;
//stops the code
}
}
}
首先你的条件似乎不明确,你可以使用 swipePosition 本身而不是 imageIndex。其次,您的 chracters.image 应该在 if/else 块内。以下是更新后的片段。如果你想使用 imageIndex,你可以相应地改变它。还添加了从最后一张图片切换到第一张图片以及从第一张图片切换到最后一张的代码。
if swipePosition > imageList.count-1{
swipePosition = 0
chracters.image = UIImage(named:imageList[swipePosition]
} else {
chracters.image = UIImage(named:imageList[swipePosition]
swipePosition += 1
}
case UISwipeGestureRecognizerDirection.Left:
print("User swiped Left")
// check if index is in range
if swipePosition == 0 {
swipePosition = imageList.count-1
chracters.image = UIImage(named:imageList[swipePosition]
} else {
chracters.image = UIImage(named:imageList[swipePosition]
swipePosition -= 1
}