Playing and viewing are separated, and playing is used for open source SDCycleScrollView
Here is what is given to view:
// // // joopic // // Created by jianxiong li on 16/9/27. // Copyright © 2016 joobot. All rights reserved.// import Foundation import UIKit //Picture Carousel Component Agent Agreementprotocol FullScreenShowImageViewDelegate{ //Get the data source func galleryDataSource()->[String] //Get the width and height dimensions of the internal scrollerView func galleryScrollerViewSize()->CGSize func hiddenForCliked(index:Int) } //Picture Carousel Component Controllerclass FullScreenShowImageView: UIView,UIScrollViewDelegate{ //Proxy object var delegate : FullScreenShowImageViewDelegate! //Screen Width let kScreenWidth = BWidth //The current image index var currentIndex : Int = 0 //Data source var dataSource : [String]? //The three images used for carousels, the center and the right (no matter how many images are used, these three imageViews are used alternately) var leftImageView , middleImageView , rightImageView : UIImageView? //Place the scroll view of the imageView var scrollerView : UIScrollView? //The width and height of scrollView var scrollerViewWidth : CGFloat? var scrollerViewHeight : CGFloat? //Page Controller (small dots) var pageControl : UIPageControl? //Load indicator (used to display the image when the iamgeView has not displayed the image) var placeholderImage:UIImage! //Automatic scroll timer var autoScrollTimer:NSTimer? init(frame: CGRect,delegate:FullScreenShowImageViewDelegate) { (frame: frame) = delegate praperaUI() } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } func praperaUI() { //Get and set the scrollerView size let size : CGSize = () = = //Get data = () //Set scrollerView () //Set loading instructions picture () //Set imageView () //Set the page controller () //Set the automatic scroll timer //() = () () } func addTapAction(){ //Add component click event let tap = UITapGestureRecognizer(target: self, action: #selector((_:))) (tap) } //Click for incident response func handleTapAction(tap:UITapGestureRecognizer)->Void{ //Get the image index value () () } func presentViewAnimate() { let fr = ?.frame ?.frame = CGRect(x: fr!., y: 22, width: fr!.width, height: fr!.height) (10, animations: { ?.frame = fr! }) { (_) in } } func dismissViewAnimate() { let fr = ?.frame ?.frame = CGRect(x: fr!., y: fr!. - StatusAndNavHeight, width: fr!.width, height: fr!.height) (10, animations: { ?.frame = CGRect(x: fr!. , y: -42, width: fr!.width, height: fr!.height) }) { (_) in = true ?.frame = fr! } } //Set scrollerView func configureScrollerView(){ = UIScrollView(frame: CGRect(x: 0,y: 0, width: !, height: BHeight)) ?.backgroundColor = () ?.delegate = self ?.contentSize = CGSize(width: ! * 3, height: BHeight) //The scroll view content area is offset to the left by one view's width ?.contentOffset = CGPoint(x: !, y: 0) ?.pagingEnabled = true ?.bounces = false (!) } //Set loading instructions picture func configurePlaceholder(){ //Here I use ImageHelper to convert text into images as loading indicator let font = (17)// (ofSize: 17.0, weight: UIFontWeightMedium) let size = CGSize(width: !, height: !) placeholderImage = UIImage(named: "Image loading...") } //Set imageView func configureImageView(){ = UIImageView(frame: CGRect(x: 0, y: (BHeight-scrollerViewHeight!)/2, width: !, height: !)) = UIImageView(frame: CGRect(x: !, y: (BHeight-scrollerViewHeight!)/2, width: !, height: ! )); = UIImageView(frame: CGRect(x: 2*!, y: (BHeight-scrollerViewHeight!)/2, width: !, height: !)); ?.showsHorizontalScrollIndicator = false ?.contentMode = ?.contentMode = ?.contentMode = //Set the pictures of the three imageViews at the beginning at the left, middle and right (the last picture in the data source, the first picture, and the second picture when they are respectively) if(?.count != 0){ resetImageViewSource() } ?.addSubview(!) ?.addSubview(!) ?.addSubview(!) } //Set the page controller func configurePageController() { = UIPageControl(frame: CGRect(x: kScreenWidth/2-60, y: BHeight - 30, width: 120, height: 20)) ?.numberOfPages = (?.count)! ?.userInteractionEnabled = false (!) } //Set the automatic scroll timer func configureAutoScrollTimer() { //Set a timer and scroll every three seconds autoScrollTimer = (3, target: self, selector: #selector(), userInfo: nil, repeats: true) } //Scroll a picture as soon as the timer time arrives func letItScroll(){ let offset = CGPoint(x: 2*scrollerViewWidth!, y: 0) ?.setContentOffset(offset, animated: true) } //Reset the image of each imageView after scrolling func resetImageViewSource() { //The first picture is currently displayed if == 0 { ?.sd_setImageWithURL(NSURL(string: !.last!)) ?.sd_setImageWithURL(NSURL(string: !.first!)) let rightImageIndex = (?.count)!>1 ? 1 : 0 //Protect ?.sd_setImageWithURL(NSURL(string: ![rightImageIndex])) } //The last picture is currently displayed else if == (?.count)! - 1 { ?.sd_setImageWithURL(NSURL(string:![-1])) ?.sd_setImageWithURL(NSURL(string: !.last!)) ?.sd_setImageWithURL(NSURL(string: !.first!)) } //Other situations else{ ?.sd_setImageWithURL(NSURL(string:![-1])) ?.sd_setImageWithURL(NSURL(string: ![])) ?.sd_setImageWithURL(NSURL(string: ![+1])) } //Set the page controller's current page number ?.currentPage = } //ScrollView is fired after scrolling func scrollViewDidScroll(scrollView: UIScrollView) { //Get the current offset let offset = if(?.count != 0){ //If you slide left (show next picture) if(offset >= !*2){ //Restore the offset = CGPoint(x: !, y: 0) //View index +1 = + 1 if == ?.count { = 0 } } //If swipe to the right (show the previous one) if(offset <= 0){ //Restore the offset = CGPoint(x: !, y: 0) //View index-1 = - 1 if == -1 { = (?.count)! - 1 } } //Reset the pictures of each imageView resetImageViewSource() } } //Drag and scroll manually to start func scrollViewWillBeginDragging(scrollView: UIScrollView) { //Change the automatic scrolling timer (prevent the user from scrolling automatically when manually moving the picture) //autoScrollTimer?.invalidate() } // Manual drag and scroll end func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) { //Restart the automatic scroll timer //configureAutoScrollTimer() } }
How to use:
var sliderGallery : FullScreenShowImageView! var bannerCurrentIndex:Int = 0 //Picture carousel component protocol method: Get the internal scrollView size func galleryScrollerViewSize() -> CGSize { return CGSize(width: BWidth, height: BHeight/2) } //Picture carousel component protocol method: obtain data collection func galleryDataSource() -> [String] { return as! [String] } //Click for incident response func hiddenForCliked(index:Int){ if(bannerCurrentIndex != index){ (Int32(index)) } ?.setNavigationBarHidden(false, animated: false) } func showImageGallery(index:Int){ //Initialize the picture carousel component if(sliderGallery == nil){ sliderGallery = FullScreenShowImageView(frame: CGRect(x: 0, y: 0, width: BWidth, height: BHeight),delegate:self) = index () //Add the picture carousel component to the current view (sliderGallery) }else{ = index () = false } () ?.setNavigationBarHidden(true, animated: false) } //pragma -- SDCycleScrollViewDelegate func cycleScrollView(cycleScrollView: SDCycleScrollView!, didSelectItemAtIndex index: Int) { print("--------index:\(index)") bannerCurrentIndex = index (index) }
The above is all the code we have compiled this time. If you still don’t understand anything when you study, you can discuss it in the comment area below. Thank you for your support.