加载SVG格式的轮播图页面。
import Foundation
import UIKit
import SVGKit
class AppIntroductionView: UIViewController, UIScrollViewDelegate {
private var pageControl = UIPageControl() // 轮播图下方的指示器白色圆点
private let numOfPages = 4
private var scrollView = UIScrollView()
private var startButton = UIButton()
let screenWidth = UIScreen.main.bounds.size.width
let screenHeight = UIScreen.main.bounds.size.height
override func viewDidLoad() {
super.viewDidLoad()
pageControl.numberOfPages = numOfPages
pageControl.currentPage = 0
pageControl.tintColor = .white
pageControl.pageIndicatorTintColor = UIColor.lightGray
pageControl.currentPageIndicatorTintColor = UIColor.blue
view.addSubview(pageControl)
// 设置UIPageControl的约束
pageControl.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
pageControl.centerXAnchor.constraint(equalTo: view.centerXAnchor),
pageControl.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -20)
])
let frame = self.view.bounds
scrollView = UIScrollView(frame: frame)
scrollView.isPagingEnabled = true
scrollView.showsHorizontalScrollIndicator = false
scrollView.showsVerticalScrollIndicator = false
scrollView.scrollsToTop = false
scrollView.bounces = false
scrollView.contentOffset = CGPointZero
scrollView.contentSize = CGSize(width:frame.size.width * CGFloat(numOfPages), height: frame.size.height)
scrollView.delegate = self
for index in 0..<numOfPages {
// // 加载常规图片
// let imageView = UIImageView(image: UIImage(named:"组 98423")) //GuideImage\(index + 1)
// imageView.frame = CGRect(x:frame.size.width * CGFloat(index), y: 0, width: frame.size.width, height:frame.size.height)
// scrollView.addSubview(imageView)
// 加载SVG图片
let svgView = SVGKFastImageView(svgkImage: SVGKImage(named: "golany"))
svgView?.frame = CGRectMake(frame.size.width * CGFloat(index), 0, frame.size.width, frame.size.height)
scrollView.addSubview(svgView!)
}
self.view.insertSubview(scrollView, at: 0)
startButton = UIButton(type: .system)
startButton.setTitle("进入登录页面", for: .normal)
startButton.setTitleColor(.blue, for: .highlighted)
startButton.frame = CGRect(x: screenWidth/2 - 50 , y: screenHeight - 180, width: 100, height: 50)
startButton.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
startButton.alpha = 0.0
view.addSubview(startButton)
}
@objc func buttonTapped(sender: UIButton!) {
UserDefaults().setValue(true, forKey: "IsShowGuideView")
self.dismiss(animated: true)
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let offset = scrollView.contentOffset
pageControl.currentPage = Int(offset.x / view.bounds.width)
if pageControl.currentPage == numOfPages - 1 {
UIView.animate(withDuration: 0.5) {
self.startButton.alpha = 1.0
}
} else {
UIView.animate(withDuration: 0.2) {
self.startButton.alpha = 0.0
}
}
}
}```