Swift-轮播图页面

加载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
            }
        }
    }
}```

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

敛柒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值