【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
            }
            
        }
    }
    
    
}

下載源碼

下載地址

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 發(fā)現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 15,410評論 4 61
  • “哇塞,阿楠,你快看,要是我是老師,你想象一下下面都是人頭,我肯定一句話都說不出來。” 下午三點鐘,暖暖的...
    求闕Alicenannan閱讀 159評論 0 1
  • 一、深copy、淺copy 淺拷貝是指源對象與拷貝對象共用一份實體,僅僅是引用的變量不同(名稱不同)。對其中任何一...
    不會游泳De魚閱讀 11,745評論 6 7
  • 已記不清楚從幾時開始,遇到煩心事情,就會去收拾自己的床鋪、房間,把所有的物品整理一遍,堆放整齊,心情似乎就好了...
    優(yōu)游2012閱讀 705評論 0 0

友情鏈接更多精彩內容