The simplest and most brutal way to achieve infinite cyclic scrolling uiscrollview

Source: Internet
Author: User

viewcontroller.swift//uiscrollview_example////Created by xxx on 16/4/1.//copyright©2016 year xxx. All rights Reserved.//import Uikitclass Viewcontroller:uiviewcontroller, uiscrollviewdelegate {var scrollview:ui    scrollview!    var childcacheview:[uiview]! Override Func Viewdidload () {super.viewdidload ()//Do any additional setup after loading the view, typical        Ly from a nib. Setupview ()} override func didreceivememorywarning () {super.didreceivememorywarning ()//Dispose of    Any resources the can be recreated. } func Setupview () {Childcacheview = [] Let frame = CGRectMake (0, 0, Self.view.frame . Size.width, +) Let ScrollView = Uiscrollview (frame:frame) scrollview.delegate = self Self.view.ad Dsubview (scrollView) Self.scrollview = scrollView var offset_x:cgfloat = 0.0 Let ind Exs = [TotalCount-1, 0, 1] for I in 0.. < 3 {Let Childview = CreateChildView (indexs[i]) Childview.frame = CGRectMake (0.0 + offset_x, 0, self . View.frame.size.width, Scrollview.addsubview (childview) offset_x + = CGFloat (self.view.frame.si                Ze.width) Childview.tag = Indexs[i] Childcacheview.append (Childview)}        Scrollview.contentsize = Cgsizemake (self.view.frame.size.width * 3.0, +) scrollview.pagingenabled = True                Scrollview.showsverticalscrollindicator = False Scrollview.showshorizontalscrollindicator = False Scrollview.setcontentoffset (cgpointmake (width, 0), animated:false) Let left = UIButton (frame:cgrectmake        (0, 410, 100, 40)) Left.settitle ("left", ForState:UIControlState.Normal) Left.settitlecolor (Uicolor.blackcolor (), Forstate:uicontrol State.normal) Left.addgesturerecognizer (UITapGestureRecognizer (target:self, Action: #selector (Viewcontroller.tapLeftButton (_:))) Self.view.addSubview (left) Let right = UIButton (Frame:cgrectmake (200, 410, 100, 4 0)) Right.settitle ("right", ForState:UIControlState.Normal) Right.settitlecolor (Uicolor.blackcolor (), forSt Ate:UIControlState.Normal) Right.addgesturerecognizer (UITapGestureRecognizer (target:self, Action: #selector (VIEWC        Ontroller.taprightbutton (_:)))) Self.view.addSubview (right)} var width:cgfloat {        Return Self.view.frame.size.width} func Prevchildview (), UIView {return childcacheview[0]}        Func Currchildview (), UIView {return childcacheview[1]} func Nextchildview () UIView { return childcacheview[2]} var totalcount = 8 var currentindex:int = 0 var urls = ["http ://pic3.zhongsou.com/image/3807f47d76cc2876255.jpg "," http://d.hiphotos.baidu.com/zhidao/pic/item/ 902397dda144ad3452a5d2fad2a20cf431ad85b0.JPG "," http://photo.niaolei.org.cn/uploads/201212/1356349545RuOuHkAy.jpg "," http://img3.fengniao.com/forum/a Ttachpics/155/160/6191982.jpg "," http://img0.pconline.com.cn/pconline/1403/03/4374256_05_thumb.jpg "," http:/ /photo.niaolei.org.cn/uploads/201212/1356350324tg2ii27r.jpg "," http://images3.qianyan.biz/qy/1/20/89/  201472417401165442030.jpg "," http://pic3.zhongsou.com/image/380bccf5f1d656ba95e.jpg "] @objc private func Tapimage (Gesture:uigesturerecognizer) {print ("Tapimage ...")} @objc private Func Tapleftbutton (gesture : Uigesturerecognizer) {print ("Tapleftbutton ...") var offset = Scrollview.contentoffset Offs Et.x-= width scrollview.setcontentoffset (offset, animated:true)} @objc private func Taprightbutton (GES        Ture:uigesturerecognizer) {print ("Taprightbutton ...") var offset = Scrollview.contentoffset Offset.x + = width scrollview.sEtcontentoffset (offset, animated:true)} func scrollviewdidscroll (Scrollview:uiscrollview) {print ("SCR         Ollviewdidscroll...\ (Scrollview.contentoffset)}} func scrollviewdidenddecelerating (Scrollview:uiscrollview) {        Print ("scrollviewdidenddecelerating...\ (Scrollview.contentoffset)") Computescrolloffset ()} Func scrollviewwillbegindragging (Scrollview:uiscrollview) {print ("scrollviewwillbegindragging...\ (scrollView.c Ontentoffset)} func scrollviewwillbegindecelerating (Scrollview:uiscrollview) {print ("scrollviewwillb Egindecelerating...\ (Scrollview.contentoffset)}} func Scrollviewdidendscrollinganimation (scrollview:uiscrollv        Iew) {print ("scrollviewdidendscrollinganimation...\ (Scrollview.contentoffset)") Computescrolloffset ()}        Func Computescrolloffset () {Let offset_x = scrollview.contentoffset.x Let index = Offset_x/width     If index = = 0 {       Swipe forward print ("swipe forward") Let Currview = Currchildview () Let NextView = NE            Xtchildview () Let PrevView = Prevchildview () Currchildview (). frame.origin.x + = width                        Prevchildview (). frame.origin.x + = width nextchildview (). frame.origin.x-= width * 2.0 Scrollview.setcontentoffset (cgpointmake (width, 0), animated:false) childcacheview[0] = Nextv Iew childcacheview[1] = prevview childcacheview[2] = Currview Updatechildview (Childcacheview[0], Currindex:childcacheview[1].tag, Increaseindex:false)}            else If index = = 2 {//Slide back print ("swipe backwards") Let Currview = Currchildview () Let NextView = Nextchildview () Let PrevView = Prevchildview () currchildview (). fr ame.origin.x-= width Nextchildview ().frame.origin.x-= width Prevchildview (). frame.origin.x + = width * 2.0 Scrollview.setcont Entoffset (cgpointmake (width, 0), animated:false) childcacheview[0] = Currview Childcach EVIEW[1] = NextView childcacheview[2] = PrevView Updatechildview (childcacheview[2], Curr        Index:childcacheview[1].tag, Increaseindex:true)}} func CreateChildView (index:int), UIView { Let ImageView = Uiimageview () imageview.userinteractionenabled = True Imageview.addgesturerecognizer (UITAPG Esturerecognizer (Target:self, Action: #selector (Viewcontroller.tapimage (_:)))) Let label = UILabel (frame: CGRectMake (0, 0, +)) Label.text = "Label:\ (index)" Label.tag = Label.font = uifont.systemf Ontofsize (28.0) Label.textcolor = Uicolor.whitecolor () imageview.addsubview (label) ImageView. Kf_setimagewithurl (Nsurl(String:urls[index])!, Placeholderimage:uiimage (named: "Test")) return ImageView} func Updatec Hildview (Childview:uiview, Currindex:int, Increaseindex:bool) {var index = 0 Let label = CHILDVIEW.VIEWW Ithtag (as!)                UILabel if Increaseindex {index = currindex + 1 If index > totalCount-1 { index = 0}} else {index = currIndex-1 If index < 0 {Inde x = totalCount-1}} label.text = "label:\ (index)" Childview.tag = Index (childvi EW as! Uiimageview). Kf_setimagewithurl (Nsurl (String:urls[index])!, Placeholderimage:uiimage (named: "Test")}}

Create a blank project and paste the code directly into a copy.

The picture download uses Onecat's kingfisher, downloads itself.

Note Configure Info.plist, add

<key>NSAppTransportSecurity</key><dict>    <key>NSAllowsArbitraryLoads</key>    <true/></dict>

Over

The simplest and most brutal way to achieve infinite cyclic scrolling uiscrollview

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.