簡(jiǎn)單描述一下原理
原理其實(shí)很簡(jiǎn)單,只需往scrollerView上放三個(gè)imageView即可吉执,初始化時(shí)怪蔑,調(diào)整contentOffset枕扫,讓第二個(gè)imageView居中(位于當(dāng)前屏幕),第一個(gè)imageView顯示第6張圖片(假設(shè)是6張)畜埋,第二個(gè)imageView顯示第一張圖片莫绣,第三個(gè)imageView顯示第二張圖片,當(dāng)手向右滑動(dòng)時(shí)悠鞍,此時(shí)當(dāng)前屏幕顯示的是第6張圖片对室,當(dāng)滑動(dòng)動(dòng)畫停止后(代理函數(shù)里有),瞬間改變圖片(第一個(gè)imageView顯示第5張圖片咖祭,第二個(gè)imageView顯示第6張圖片掩宜,第三個(gè)imageView顯示第1張圖片),同時(shí)改變contentOffset(依然讓第二個(gè)imageView居中心肪,顯示在當(dāng)前屏幕中)锭亏,手勢(shì)向左滑動(dòng)時(shí)同理。
來吧硬鞍!開整吧!戴已!猿類們都愛用代碼說話9谈谩!糖儡!
- 初始化了一個(gè)圖片數(shù)組伐坏,然后是scrollerView的高度,每頁(yè)的寬度
view_WIDTH = Double(self.view.frame.width);
view_HEIGHT = 220.0
imagesArray = [UIImage.init(named: "James0.jpg")!,UIImage.init(named: "James1.jpg")!,UIImage.init(named: "James2.jpg")!,UIImage.init(named: "James3.jpg")!,UIImage.init(named: "James4.jpg")!,UIImage.init(named: "James5.jpg")!]
- 初始化scrollerView
func loadScrollerView() -> Void {
scrollerView = UIScrollView.init(frame: CGRect.init(x: 0.0, y: 64.0, width: view_WIDTH, height: view_HEIGHT))
scrollerView.backgroundColor = UIColor.orange
scrollerView.contentSize = CGSize.init(width: view_WIDTH * 3, height: 0)
scrollerView.showsVerticalScrollIndicator = false
scrollerView.showsHorizontalScrollIndicator = false
scrollerView.isPagingEnabled = true
scrollerView.bounces = true
scrollerView.contentOffset = CGPoint.init(x: view_WIDTH, y: 0)
scrollerView.delegate = self;
self.view.addSubview(scrollerView)
imageViewArray = NSMutableArray.init()
for i in 0...2
{
let imageView = UIImageView.init()
imageView.frame = CGRect.init(x: view_WIDTH * Double(i), y: 0.0, width: view_WIDTH, height: view_HEIGHT)
imageView.contentMode = UIViewContentMode.scaleAspectFill
imageView.isUserInteractionEnabled = true
imageView.tag = (5 + i)%6
print(imageView.tag)
imageView.image = imagesArray[imageView.tag]
scrollerView.addSubview(imageView)
imageViewArray.add(imageView)
let tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(imageViewClicked))
imageView.addGestureRecognizer(tapGesture)
}
pageControl = UIPageControl.init(frame: CGRect.init(x: 0.0, y: Double(scrollerView.frame.maxY - 30), width: view_WIDTH, height: 30.0))
pageControl.numberOfPages = 6
pageControl.currentPage = 0
self.view.addSubview(pageControl)
}
- 重新改變圖片和contentOffset
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
self.updateImageViewsAndPageControl()
}
func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
self.updateImageViewsAndPageControl()
}
func updateImageViewsAndPageControl() -> Void {
var flag = NSInteger()
if scrollerView.contentOffset.x > CGFloat(view_WIDTH)
{
flag = 1
}
else if scrollerView.contentOffset.x == 0.0
{
flag = -1
}
else
{
return
}
for x in 0..<imageViewArray.count
{
let imageView:UIImageView = imageViewArray[x] as! UIImageView
var index = imageView.tag + flag;
if index < 0
{
index = pageControl.numberOfPages - 1
}
else if index >= pageControl.numberOfPages
{
index = 0
}
imageView.tag = index
imageView.image = imagesArray[imageView.tag]
}
let currentImgView:UIImageView = imageViewArray[1] as! UIImageView
pageControl.currentPage = currentImgView.tag
scrollerView.contentOffset = CGPoint.init(x: view_WIDTH, y: 0)
}