1、创建scrollview
self.frame = CGRect(x: rect.origin.x, y: 0, width: rect.size.width, height: rect.size.height)
self.contentSize = CGSize(width: CGFloat(imageNames.count ) * self.bounds.width,height: rect.size.height)
self.bounces = false
self.isPagingEnabled = true
self.showsVerticalScrollIndicator = false
self.showsHorizontalScrollIndicator = false
for i in 0...(imageNames.count - 1) {
let imageView = UIImageView.init(frame: CGRect(x: CGFloat(i) * self.bounds.width, y: 0, width: self.bounds.width, height: rect.size.height))
self.addSubview(imageView)
imageView.contentMode = .scaleAspectFill
imageView.image = UIImage(named: imageNames[i])
}
2、创建pagecontrol
pageControl = UIPageControl.init(frame: CGRect(x: (rect.size.width - 100)*0.5, y: rect.size.height - 30, width: 100, height: 20))
pageControl.numberOfPages = imageNames.count
pageControl.pageIndicatorTintColor = pageIndicatorTintColor
pageControl.currentPageIndicatorTintColor = currentPageIndicatorTintColor
//将小白点放到scr之上
view.insertSubview(pageControl, aboveSubview: scrollView)
3、创建自动轮播的计时器
timer = Timer.scheduledTimer(timeInterval: timeInterval, target: self, selector: #selector(self.timerManager), userInfo: nil, repeats: true)
func timerManager() {
//实现方法
}
4、实现代理方法
func scrollViewDidScroll(_ scrollView: UIScrollView) {
}
在滚动时可以添加动画,使移动时视觉效果增强
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(0.3)//时间
UIView.setAnimationCurve(UIViewAnimationCurve.easeInOut)
//移动路径
UIView.commitAnimations()