zoukankan      html  css  js  c++  java
  • swift头部无线轮播视图

    //
    //  AmonCirHeadView.swift
    //  BQPReader
    //
    //  Created by mac on 16/5/25.
    //  Copyright © 2016年 BQP. All rights reserved.
    //
    
    import UIKit
    
    class AmonCirHeadView: UIView ,UIScrollViewDelegate{
    
        var dataArray:NSArray?;
        var _scrollView:UIScrollView?;
    //计时器 var _timer:NSTimer
    ?;
    //主要是为了确定scrollview和pagecontroller的显示位置 var index:Int64
    ?; var _pageControl:UIPageControl?; override init(frame: CGRect) { super.init(frame: frame);
    //复制全部代码 插入图片,在此更换图片名即可,当然也可以网络请求图片,这个自己去修改逻辑吧 dataArray
    = ["aion01.jpg","aion02.jpg","aion03.jpg"];
    //创建scrollview _scrollView
    = UIScrollView(frame: CGRectMake(0, 0, kScreenW,self.bounds.size.height)); _scrollView?.bouncesZoom = false; _scrollView?.bounces = false; _scrollView?.showsHorizontalScrollIndicator = false; _scrollView?.showsVerticalScrollIndicator = false; _scrollView?.pagingEnabled = true; _scrollView?.delegate = self; self.addSubview(_scrollView!); _scrollView?.contentSize = CGSizeMake(self.bounds.size.width * CGFloat((dataArray?.count)!), self.bounds.size.height); _scrollView?.backgroundColor = UIColor.redColor();
    //创建pageControl _pageControl
    = UIPageControl(frame: CGRectMake(0, self.bounds.size.height - 20, kScreenW, 20)); self.addSubview(_pageControl!); _pageControl!.backgroundColor = UIColor.clearColor(); _pageControl!.numberOfPages = (dataArray?.count)!; _pageControl!.currentPage = 0; _pageControl!.pageIndicatorTintColor = UIColor.whiteColor(); _pageControl!.currentPageIndicatorTintColor = UIColor.orangeColor(); creatImageView(); } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } //创建scrollview上的图片控件 func creatImageView(){ index = 0; var i = 0; for imgNmae in dataArray!{ let imgV = UIImageView(frame: CGRectMake(self.bounds.size.width * CGFloat(i), 0, self.bounds.size.width, self.bounds.size.height)); imgV.image = UIImage(named: imgNmae as! String); _scrollView?.addSubview(imgV); i = i + 1; } //创建计时器 _timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: "timeMethod", userInfo: nil, repeats: true);
    //创建runloop设置计时器的运行模式,保证在滑动界面时,计时器也在工作 NSRunLoop.currentRunLoop().addTimer(_timer
    !, forMode: NSRunLoopCommonModes); } //计时器调用的方法 func timeMethod(){ index = index!+1; if NSInteger(index!) >= dataArray?.count{ index = 0; } UIView.animateWithDuration(0.35) { () -> Void in self._scrollView!.contentOffset = CGPointMake(CGFloat(self.index!) * kScreenW, self._scrollView!.contentOffset.y); }; _pageControl!.currentPage = NSInteger(index!); } //scrollview的代理 func scrollViewDidScroll(scrollView: UIScrollView) { let x = scrollView.contentOffset.x/kScreenW; index = Int64(x); _pageControl!.currentPage = NSInteger(x); } }

     此类中代码,是已经封装好的,在外部创建,给其frame和父视图,即可显示。需要注意的地方,就是图片哪里了,在代码中已经写出来了。

  • 相关阅读:
    【leetcode】1215.Stepping Numbers
    【leetcode】1214.Two Sum BSTs
    【leetcode】1213.Intersection of Three Sorted Arrays
    【leetcode】1210. Minimum Moves to Reach Target with Rotations
    【leetcode】1209. Remove All Adjacent Duplicates in String II
    【leetcode】1208. Get Equal Substrings Within Budget
    【leetcode】1207. Unique Number of Occurrences
    【leetcode】689. Maximum Sum of 3 Non-Overlapping Subarrays
    【leetcode】LCP 3. Programmable Robot
    【leetcode】LCP 1. Guess Numbers
  • 原文地址:https://www.cnblogs.com/zxh-iOS/p/5529777.html
Copyright © 2011-2022 走看看