swift头部无线轮播视图

Posted zhangxianhongx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift头部无线轮播视图相关的知识,希望对你有一定的参考价值。

//
//  AmonCirHeadView.swift
//  BQPReader
//
//  Created by mac on 16/5/25.
//  Copyright © 2016年 BQP. All rights reserved.
//

import UIKit

class AmonCirHeadView: UIView ,UIScrollViewDelegate{

    var dataArray:NSArray?;
    var _scrollView:UIScrollView?;
//计时器 var _timer:NSTimer
?;
//主要是为了确定scrollview和pagecontroller的显示位置 var index:Int64
?; var _pageControl:UIPageControl?; override init(frame: CGRect) { super.init(frame: frame);
//复制全部代码 插入图片,在此更换图片名即可,当然也可以网络请求图片,这个自己去修改逻辑吧 dataArray
= ["aion01.jpg","aion02.jpg","aion03.jpg"];
//创建scrollview _scrollView
= UIScrollView(frame: CGRectMake(0, 0, kScreenW,self.bounds.size.height)); _scrollView?.bouncesZoom = false; _scrollView?.bounces = false; _scrollView?.showsHorizontalScrollIndicator = false; _scrollView?.showsVerticalScrollIndicator = false; _scrollView?.pagingEnabled = true; _scrollView?.delegate = self; self.addSubview(_scrollView!); _scrollView?.contentSize = CGSizeMake(self.bounds.size.width * CGFloat((dataArray?.count)!), self.bounds.size.height); _scrollView?.backgroundColor = UIColor.redColor();
//创建pageControl _pageControl
= UIPageControl(frame: CGRectMake(0, self.bounds.size.height - 20, kScreenW, 20)); self.addSubview(_pageControl!); _pageControl!.backgroundColor = UIColor.clearColor(); _pageControl!.numberOfPages = (dataArray?.count)!; _pageControl!.currentPage = 0; _pageControl!.pageIndicatorTintColor = UIColor.whiteColor(); _pageControl!.currentPageIndicatorTintColor = UIColor.orangeColor(); creatImageView(); } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } //创建scrollview上的图片控件 func creatImageView(){ index = 0; var i = 0; for imgNmae in dataArray!{ let imgV = UIImageView(frame: CGRectMake(self.bounds.size.width * CGFloat(i), 0, self.bounds.size.width, self.bounds.size.height)); imgV.image = UIImage(named: imgNmae as! String); _scrollView?.addSubview(imgV); i = i + 1; } //创建计时器 _timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: "timeMethod", userInfo: nil, repeats: true);
//创建runloop设置计时器的运行模式,保证在滑动界面时,计时器也在工作 NSRunLoop.currentRunLoop().addTimer(_timer
!, forMode: NSRunLoopCommonModes); } //计时器调用的方法 func timeMethod(){ index = index!+1; if NSInteger(index!) >= dataArray?.count{ index = 0; } UIView.animateWithDuration(0.35) { () -> Void in self._scrollView!.contentOffset = CGPointMake(CGFloat(self.index!) * kScreenW, self._scrollView!.contentOffset.y); }; _pageControl!.currentPage = NSInteger(index!); } //scrollview的代理 func scrollViewDidScroll(scrollView: UIScrollView) { let x = scrollView.contentOffset.x/kScreenW; index = Int64(x); _pageControl!.currentPage = NSInteger(x); } }

 

以上是关于swift头部无线轮播视图的主要内容,如果未能解决你的问题,请参考以下文章

Swift 使用CollectionView 实现图片轮播封装就是这样简单

如何将这个 Objective-C 代码片段写入 Swift?

iOS Swift 中的 Android 片段模拟

如何使用sublime代码片段快速输入PHP头部版本声明

iOS开发项目实战——Swift实现图片轮播与浏览

Swift无限循环的图片轮播