前端之家收集整理的这篇文章主要介绍了
开发----Swift自定义View实现 实现自动循环滚动,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
//
// AutoScrollImagesView.swift
// SwiftRealmDatabaseTest
//
// Created by Sintn on 16/4/12.
// Copyright © 2016年 sin. All rights reserved.
//
import UIKit
class AutoScrollImagesView: UIView,UIScrollViewDelegate{
let DEFAULTPAGECONTROLHEIGHT=CGFloat(30.0)
let DEFAULTPAGECONTROLINDICATIRTINTCOLOR=UIColor.orangeColor()
var urlsArrays = Set<String>()
var imagesArrays = Set <UIView> ()
let imageScrollView=UIScrollView()
let imagePageControl=UIPageControl()
var pageControlPageSize = 0
var timsTaskManager=NSTimer()
override func layoutSubviews() {
imageScrollView.frame = CGRect.init(x: 0,y: 0,width: self.frame.size.width,height: self.frame.size.height)
imageScrollView.delegate=self
imageScrollView.pagingEnabled=true
imageScrollView.showsVerticalScrollIndicator=false
imageScrollView.showsHorizontalScrollIndicator=false
imageScrollView.canCancelContentTouches=false
self.addSubview(imageScrollView)
imagePageControl.frame=CGRect.init(x: 0,y: self.frame.size.height-DEFAULTPAGECONTROLHEIGHT,height: DEFAULTPAGECONTROLHEIGHT)
imagePageControl.pageIndicatorTintColor=DEFAULTPAGECONTROLINDICATIRTINTCOLOR
self.addSubview(imagePageControl)
}
func reload(urls:NSArray) {
pageControlPageSize=urls.count
if pageControlPageSize==0 {
return
}
if urlsArrays.count>0 {
urlsArrays.removeAll()
}
imagePageControl.numberOfPages=pageControlPageSize
imageScrollView.contentSize=contentSizeOfScrollView()
initScrollImages(urls)
}
func initScrollImages(urls:NSArray){
if imagesArrays.count>0 {
for imageView in imagesArrays {
imageView.removeFromSuperview()
}
imagesArrays.removeAll()
}
for otems in urls {
let scorllImageView = UIImageView.init(frame: CGRect.init(x: 0,height: self.frame.size.height))
scorllImageView.sd_setImageWithURL(NSURL.init(string: otems as! String))
imagesArrays.insert(scorllImageView)
}
var x = CGFloat(0.0)
for genre in imagesArrays {
genre.frame = CGRectOffset(genre.frame,x,0)
imageScrollView.addSubview(genre)
x += self.frame.size.width
}
}
func contentSizeOfScrollView() -> CGSize {
return CGSize.init(width: self.frame.size.width*CGFloat(pageControlPageSize),height: self.frame.size.height)
}
func scrollViewDidScroll(scrollView: UIScrollView)
{
let v=imageScrollView.contentOffset.x
let k=(imageScrollView.contentSize.width / CGFloat.init(pageControlPageSize))
imagePageControl.currentPage = Int.init(v/k)
}
func beginAutoScroll(timerInterval:NSTimeInterval) {
timsTaskManager=NSTimer.scheduledTimerWithTimeInterval(timerInterval,target: self,selector: #selector(AutoScrollImagesView.timerTask),userInfo: nil,repeats: true)
}
func timerTask() {
let pt = imageScrollView.contentOffset
if pt.x==CGFloat.init(self.frame.size.width * CGFloat.init(pageControlPageSize-1)) {
imageScrollView.contentOffset=CGPoint.init(x: 0,y: 0)
imageScrollView.scrollRectToVisible(CGRect.init(x:self.frame.size.width,height: self.frame.size.height),animated: true)
}else
{
imageScrollView.scrollRectToVisible(CGRect.init(x: pt.x+self.frame.size.width,animated: true)
}
if imagePageControl.currentPage<pageControlPageSize-1 {
imagePageControl.currentPage=imagePageControl.currentPage+1
}else
{
imagePageControl.currentPage=0
}
}
func stopAutoScroll() {
if timsTaskManager.valid {
timsTaskManager.invalidate()
}
}
override func delete(sender: AnyObject?) {
super.delete(sender)
stopAutoScroll()
}
}
原文链接:https://www.f2er.com/swift/324087.html