Swift -banner滚动图自定义

简介: Swift -banner滚动图自定义

这片博客和http://blog.csdn.net/codingfire/article/details/51646607是一样的,只不过博主又用Swift把原来的Object-C代码写了一遍,其中还用到了代理方法,创建数据模型,在开发中还是比较常用的。

image.png

效果都一样,下面看代码:

import UIKit
//写在类别外面类似于#define
let WIDTH = UIScreen.mainScreen().bounds.size.width
let HEIGHT = UIScreen.mainScreen().bounds.size.height
protocol TopScrollViewDelegate {
    /*
     *UIScrollViewDelegate  协议方法,把点击的图片的位置传给使用者
     */
    func didClickScrollView(index:NSInteger)
}
class TopScrollView: UIView,UIScrollViewDelegate {
    /*
     // Only override drawRect: if you perform custom drawing.
     // An empty implementation adversely affects performance during animation.
     override func drawRect(rect: CGRect) {
     // Drawing code
     }
     */
    var _mainScrollView = UIScrollView()
    var contentLabel = UILabel()
    var currentImageView = UIImageView()
    var _currentIndex: NSInteger!
    var _dataArray = NSMutableArray()
    var pageSubView = UIView()
    var delegate:TopScrollViewDelegate!
    /*
     *初始化scrollView及其部件
     */
    init(frame: CGRect, dataArray:NSMutableArray) {
        super.init(frame:frame)
        _mainScrollView.frame = CGRectMake(0, 0, WIDTH, 170)
        _mainScrollView.contentSize = CGSizeMake(WIDTH*3, 170)
        _mainScrollView.backgroundColor = UIColor.whiteColor()
        _mainScrollView.delegate = self
        _mainScrollView.pagingEnabled = true
        _mainScrollView.userInteractionEnabled = true
        _mainScrollView.showsHorizontalScrollIndicator = false
        _mainScrollView.showsVerticalScrollIndicator = false
        _mainScrollView.bounces = false;
        _mainScrollView.contentOffset = CGPointMake(WIDTH, 0)
        self.addSubview(_mainScrollView)
        _currentIndex = 0;
        _dataArray.setArray(dataArray as [AnyObject])
        self.setUpDataDataArray(_dataArray)
        self.cretPageControlAndTitle()
        // 手势
        let tap = UITapGestureRecognizer(target: self, action: #selector(self.tapCLick))
        currentImageView.addGestureRecognizer(tap)
        NSTimer.scheduledTimerWithTimeInterval(4, target: self, selector: #selector(self.timerAction), userInfo: nil, repeats: true)
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder:aDecoder)
    }
    /*
     *此处为scrollView的复用,比目前网上大部分的同类型控件油画效果好,只需要三张图片依次替换即可实现轮播,不需要有几张图就使scrollView的contentSize为图片数*宽度
     */
    func setUpDataDataArray(dataArray:NSArray) {
        for var view in _mainScrollView.subviews {
            if view.isKindOfClass(UIImageView) {
                view.removeFromSuperview()
            }
        }
        // 中间图
        currentImageView.sd_setImageWithURL(NSURL(string: (dataArray[_currentIndex] as! DataModel).imgURL as String))
        currentImageView.userInteractionEnabled = true;
        currentImageView.frame = CGRectMake(WIDTH, 0, WIDTH, 170);
        _mainScrollView.addSubview(currentImageView)
        // 左侧图
        let preImageView = UIImageView()
        let imageStr = _currentIndex - 1 >= 0 ? (dataArray[_currentIndex-1] as! DataModel).imgURL as String : (dataArray.lastObject as! DataModel).imgURL as String
        preImageView.userInteractionEnabled = true
        preImageView.sd_setImageWithURL(NSURL(string: imageStr))
        preImageView.frame = CGRectMake(0, 0, WIDTH, 170)
        _mainScrollView.addSubview(preImageView)
        // 右侧
        let nextImageView = UIImageView()
        let imageStr1 = _currentIndex + 1 < dataArray.count ? (dataArray[_currentIndex+1] as! DataModel).imgURL as String : (dataArray.firstObject as! DataModel).imgURL as String
        nextImageView.userInteractionEnabled = true
        nextImageView.sd_setImageWithURL(NSURL(string: imageStr1))
        nextImageView.frame = CGRectMake(WIDTH*2, 0, WIDTH, 170)
        _mainScrollView.addSubview(nextImageView)
    }
    /*
     *创建标题和pageControl,此处pageCOntrol为自定义的,如需要可修改为系统的,或更换图片即可
     */
    func cretPageControlAndTitle() {
        contentLabel.frame = CGRectMake(0, self.frame.size.height-30, WIDTH, 30);
        contentLabel.textAlignment = .Left;
        contentLabel.font = UIFont.systemFontOfSize(12)
        contentLabel.text = (_dataArray.firstObject as! DataModel).title as String
        contentLabel.backgroundColor = UIColor.blackColor()
        contentLabel.textColor = UIColor.whiteColor()
        contentLabel.alpha = 0.6;
        self.addSubview(contentLabel)
        pageSubView.frame = CGRectMake(0, self.frame.size.height-30, WIDTH, 30)
        pageSubView.backgroundColor = UIColor.clearColor()
        self.addSubview(pageSubView)
        for index in 0..<_dataArray.count {
            let imageView = UIImageView(image: UIImage.init(named: "News_Pic_Number02@2x.png"))
            imageView.frame = CGRectMake(WIDTH-(CGFloat(_dataArray.count)*12)-10+CGFloat(index)*12, 11, 7, 7)
            if index == 0
            {
                imageView.image = UIImage.init(named: "News_Pic_Number01@2x.png")
            }
            pageSubView.addSubview(imageView)
        }
    }
    /*
     *图片的代理点击响应方法
     */
    func tapCLick() {
        delegate.didClickScrollView(_currentIndex)
    }
    /*
     *定时器方法,使banner页无限轮播
     */
    func timerAction() {
        let imageView = pageSubView.subviews[_currentIndex] as! UIImageView
        imageView.image = UIImage.init(named: "News_Pic_Number02@2x.png")
        if _currentIndex+1 < _dataArray.count {
            _currentIndex = _currentIndex + 1;
        }
        else
        {
            _currentIndex=0;
        }
        UIView.animateWithDuration(1, animations: {
            self._mainScrollView.contentOffset = CGPointMake(WIDTH*2, 0)
            },completion: {
                (finished) in
                self._mainScrollView.contentOffset = CGPointMake(WIDTH, 0)
                self.setUpDataDataArray(self._dataArray)
        })
        contentLabel.text = (_dataArray[_currentIndex] as! DataModel).title as String
        let imageView1 = pageSubView.subviews[_currentIndex] as! UIImageView
        imageView1.image = UIImage.init(named: "News_Pic_Number01@2x.png")
    }
    /*
     *UIScrollViewDelegate  协议方法,拖动图片的处理方法
     */
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        if scrollView == _mainScrollView
        {
            let imageView = pageSubView.subviews[_currentIndex] as! UIImageView
            imageView.image = UIImage.init(named: "News_Pic_Number02@2x.png")
            let index = scrollView.contentOffset.x/WIDTH;
            if index > 1
            {
                _currentIndex = _currentIndex + 1 < _dataArray.count ? _currentIndex+1 : 0;
                UIView.animateWithDuration(1, animations: {
                    self._mainScrollView.contentOffset = CGPointMake(WIDTH*2, 0)
                    },completion: {
                        (finished) in
                        self._mainScrollView.contentOffset = CGPointMake(WIDTH, 0)
                        self.setUpDataDataArray(self._dataArray)
                })
            }
            else if index < 1
            {
                _currentIndex = _currentIndex - 1 >= 0 ? _currentIndex-1 : _dataArray.count - 1;
                UIView.animateWithDuration(1, animations: {
                    self._mainScrollView.contentOffset = CGPointMake(0, 0)
                    },completion: {
                        (finished) in
                        self._mainScrollView.contentOffset = CGPointMake(WIDTH, 0)
                        self.setUpDataDataArray(self._dataArray)
                })
            }
            else
            {
                print("没滚动不做任何操作")
            }
            contentLabel.text = (_dataArray[_currentIndex] as! DataModel).title as String
            let imageView1 = pageSubView.subviews[_currentIndex] as! UIImageView
            imageView1.image = UIImage.init(named: "News_Pic_Number01@2x.png")
        }
    }
}


目录
相关文章
|
1月前
|
Swift iOS开发 UED
如何使用Swift和UIKit在iOS应用中实现自定义按钮动画
本文通过一个具体案例,介绍如何使用Swift和UIKit在iOS应用中实现自定义按钮动画。当用户点击按钮时,按钮将从圆形变为椭圆形,颜色从蓝色渐变到绿色;释放按钮时,动画以相反方式恢复。通过UIView的动画方法和弹簧动画效果,实现平滑自然的过渡。
59 1
|
2月前
|
Swift iOS开发 UED
如何使用Swift和UIKit在iOS应用中实现自定义按钮动画
【10月更文挑战第18天】本文通过一个具体案例,介绍如何使用Swift和UIKit在iOS应用中实现自定义按钮动画。当用户按下按钮时,按钮将从圆形变为椭圆形并从蓝色渐变为绿色;释放按钮时,动画恢复原状。通过UIView的动画方法和弹簧动画效果,实现平滑自然的动画过渡。
62 5
|
7月前
|
Swift 图形学 iOS开发
【Swift开发专栏】Swift中的自定义视图与绘制
【4月更文挑战第30天】本文探讨了Swift中自定义视图的创建与绘制,分为基础知识、绘制步骤和性能优化三部分。开发者通过继承`UIView`,重写`draw(_:)`方法并利用Core Graphics进行2D绘图。提高性能的技巧包括避免重复绘制、使用轻量级视图、优化图形上下文使用、启用图层背板及避免阻塞主线程。自定义视图让iOS和macOS界面更独特高效,进阶可探索Core Animation和Metal等技术。
95 0
|
Swift
Swift - 滚动选择器
Swift - 滚动选择器
129 0
Swift - 滚动选择器
|
Swift
Swift - 自定义tabbar的封装
Swift - 自定义tabbar的封装
347 0
Swift - 自定义tabbar的封装
|
Swift
Swift之自定义Button
Swift之自定义Button
238 0
Swift之自定义Button
|
Swift iOS开发
UISlider自定义滑动条高度(swift)
UISlider滑动条的高度系统默认给我们设定了,没有办法进行改变。可是,我们的设计又是需要改变这个高度的,怎么办呢?这个时候,我们可以写一个UISlider的子类,重写四个方法就能实现了。
3653 0
|
Swift
swift微博第14天(新特性的引导图)
swift微博第14天(新特性的引导图)
196 0
swift微博第14天(新特性的引导图)
|
Swift
swift微博第9天(自定义微博首页的菜单)
swift微博第9天(自定义微博首页的菜单)
183 0
swift微博第9天(自定义微博首页的菜单)
|
Swift
Swift5.1—自定义运算符
Swift5.1—自定义运算符
146 0

热门文章

最新文章

相关课程

更多

相关实验场景

更多