###Abstract
How to automatic scroll
New features in Swift 3.0
How to Async load image with memery and disk cache policy likeSDWebImage
.
###Introduction
……………………………………………………………………………………….
A class with ad. pics auotmatic scrolling build in swift with image cache policy used.
###Installtion
Manual:
Download This Project and drag the FGSwiftAutoScrollView
folder into your peroject, do not forget to ensure “copy item if need” being selected.
###Usage
Load Web Images:
1
2
3
4
5
6
7
8
9
10
11
12
13//MARK:-
//MARK:automatic scrollView with web images
convenience init(frame:CGRect, placeHolder placeHolderImage:UIImage?,remoteImageUrls imgs:Array<String>?, selectImageAction imageDidSelectedAction:@escaping FGImageClickBlock){
self.init(frame: frame)
self.createLoacalCacheFolder()
self.placeHolderImage=placeHolderImage
self.didSelectedImageAction=imageDidSelectedAction
self.imageUrlArray=imgs
DispatchQueue.main.async {
self.createScrollView()
}
}Load Local Images:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26//MARK:automatic scrollView with local images
convenience init(frame:CGRect,placeHolder placeHolderImage:UIImage?,localImageNames imgs:Array<String>?, selectImageAction imageDidSelectedAction:@escaping FGImageClickBlock){
self.init(frame: frame)
self.placeHolderImage=placeHolderImage
var fileUrlsArray:Array<String>=[]
for name in imgs!{
var path:String?
if name.hasSuffix("jpg")||name.hasSuffix("png"){
path=Bundle.main.path(forResource: name, ofType: nil)
}
else{
path=Bundle.main.path(forResource: name, ofType: "png")
}
if path==nil{
path=""
}
path=path?.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed)
let fullPath=String.init(format: "file://%@", path!)
fileUrlsArray.append(fullPath)
}
self.didSelectedImageAction=imageDidSelectedAction
self.imageUrlArray=fileUrlsArray
self.createScrollView()
}you can simply use the srcoll call back block if need (not necessary)
1
2
3
4self.banner?.imageDidScrolledBlock={ (currentIndex) in
print("滚到到了第"+String(currentIndex)+"页了")
}
###Explain:
If you don’t need add image tap action, property didSelectedImageAction block can be nil.
###How to implementation
Create a class:
FGSwiftAutoScrollView.swift
and confirm to the UIScrollViewDelegate
1 | class FGSwiftAutoScrollView: UIView,UIScrollViewDelegate{ |
Add some properties
1 | //auto scroll interval |
Override initWithFrame:
1 | override init(frame:CGRect){ |
Create an area for disk image cache
1 | //MARK:- create cache area |
Give two convenience functions
1 | //MARK:-Load Web Image |
1 | //MARK:Load Local Image |
Create main scroll view
1 | //MARK:- |
Impletatin of sroll view delegate
1 | //MARK:- |
Implementation of tap image action
1 | //MARK:- |
Destroy the timer in deinit
1 | deinit{ |
Write an extension to async download image like
SDWebImage
1 | extension FGSwiftAutoScrollView{ |
Async image loading like
SDWebImage
with cache in memery an disk
1 | func fg_setImageWithUrlString(imageView:UIImageView?,urlString:String?,placeHolder:UIImage?){ |
Donwload Image Task
1 | //async download image |
End. Attached the address of my source code on GitHub
FGSwiftAutoScrollView Hope your star, fork or pull request.
###Support Me
- Blog: CGPointZeero
- GitHub: Insfgg99x
- Mooc: CGPointZero
- Jianshu: CGPointZero
- Email: newbox0512@yahoo.com
……………………………………………………………………………………
Copyright (c) 2016 CGPointZero. All rights reserved.