【iOS】用UIScrollView产生视差效果

学习文章

用UIScrollView产生视差效果

效果

SmoothStyle.gif
ReplaceStyle.gif

源码

MoreInfoView:
import UIKit

class MoreInfoView: UIView {

    var imageView: UIImageView?
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        self.layer.borderWidth   = 0.5
        self.layer.borderColor   = UIColor.blackColor().CGColor
        self.layer.masksToBounds = true
        
        imageView = UIImageView(frame: frame)
        
        self.addSubview(imageView!)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
  
ViewController:
import UIKit

// MARK: -ScrollView上ImageView的位移因子
enum SwitchFactor {
    
    // 平滑过渡
    case SmoothStyle
    
    // 交换过渡
    case ReplaceStyle
    
    // 以下皆是经验值,并非计算出来的
    func factor() -> CGFloat {
        
        switch self {
            
        case .SmoothStyle:
            
            return 1
            
        case .ReplaceStyle:
            
            return 2
        }
    }
}

class ViewController: UIViewController, UIScrollViewDelegate {
    
    var scrollView: UIScrollView?
    let viewWidth = UIScreen.mainScreen().bounds.width
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.blackColor()
        
        scrollView = UIScrollView(frame: view.bounds)
        scrollView?.delegate      = self
        scrollView?.pagingEnabled = true
        
        view.addSubview(scrollView!)
        
        let imageArray = [UIImage(named: "1"),UIImage(named: "2"),UIImage(named: "3"),UIImage(named: "4"),UIImage(named: "5")]
        
        for (index, value) in imageArray.enumerate() {
            
            let show = MoreInfoView(frame: CGRect(x: CGFloat(index) * view.bounds.size.width, y: 0, width: view.bounds.width, height: view.bounds.height))
            
            show.imageView?.image = value
            
            scrollView?.addSubview(show)
        }
        
        scrollView?.contentSize = CGSize(width: CGFloat(imageArray.count) * view.bounds.width, height: view.bounds.height)
    }
    
    // MARK: UIScrollViewDelegate
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        let x = scrollView.contentOffset.x
        
        for (index, value) in scrollView.subviews.enumerate() {
            
            if value.isKindOfClass(MoreInfoView.classForCoder()) {
                
                let temp = value as! MoreInfoView
                
                // 乘数因子
                let factor = SwitchFactor.ReplaceStyle
                
                // 产生视差效果
                var rect: CGRect = (temp.imageView?.frame)!
                rect.origin.x = factor.factor() * (x - CGFloat(index) * viewWidth)
                temp.imageView?.frame = rect
            }
            
        }
    }
    
    
}

下载源码

下载地址

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,267评论 4 61
  • “哇塞,阿楠,你快看,要是我是老师,你想象一下下面都是人头,我肯定一句话都说不出来。” 下午三点钟,暖暖的...
    求阙Alicenannan阅读 119评论 0 1
  • 一、深copy、浅copy 浅拷贝是指源对象与拷贝对象共用一份实体,仅仅是引用的变量不同(名称不同)。对其中任何一...
    不会游泳De鱼阅读 11,647评论 6 7
  • 已记不清楚从几时开始,遇到烦心事情,就会去收拾自己的床铺、房间,把所有的物品整理一遍,堆放整齐,心情似乎就好了...
    优游2012阅读 684评论 0 0