学习文章
用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
}
}
}
}
下载源码
下载地址
网友评论