您的位置:首页 > 移动开发 > Swift

开发----Swift自定义View实现 实现自动循环滚动

2016-04-13 09:52 567 查看

AutoScrollImagesView.swift

//
//  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, width: self.frame.size.width, 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, y: 0, width: self.frame.size.width, 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, y: 0, width: self.frame.size.width, height: self.frame.size.height), animated: true);
}else
{
imageScrollView.scrollRectToVisible(CGRect.init(x: pt.x+self.frame.size.width, y: 0, width: self.frame.size.width, height: self.frame.size.height), 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();
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code
}
*/

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: