可以获取视差效果以在Swift中工作

时间:2018-06-30 19:34:16

标签: ios swift parallax

我正在尝试创建我的第一个Swift移动应用程序(iPhone),其中用户水平滚动图像,并且文本标签的滚动速度与图像不同,即具有视差效果的简单应用程序。我几乎可以正常使用,但是标签放置和滚动速度存在一些问题。 以下是其工作原理的动画:https://drive.google.com/file/d/1N5lr0g8YsC15qSbWNpTTzD1r1KiHjS-C/view?usp=sharing 请检查我的代码,也许我遗漏了一些明显的东西。

import UIKit

struct scrollViewDataStruct {
    let title : String?
    let image : UIImage?
}

class ViewController: UIViewController, UIScrollViewDelegate  {

@IBOutlet weak var scrollView: UIScrollView!

var scrollViewData = [scrollViewDataStruct]()
var viewTagValue = 100
var tagValue = 1

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    scrollView.delegate = self

    scrollViewData = [scrollViewDataStruct.init(title: "Л", image: #imageLiteral(resourceName: "animal1")),
                      scrollViewDataStruct.init(title: "М", image: #imageLiteral(resourceName: "animal3")), scrollViewDataStruct.init(title: "Н", image: #imageLiteral(resourceName: "animal2")),
        scrollViewDataStruct.init(title: "П", image: #imageLiteral(resourceName: "animal4")),
    scrollViewDataStruct.init(title: "С", image: #imageLiteral(resourceName: "animal5"))]

    scrollView.contentSize.width = view.frame.width * CGFloat(scrollViewData.count)

    var i = 0

    for data in scrollViewData {
        let view = CustomView(frame: CGRect(x: self.scrollView.frame.width * CGFloat(i), y: 0, width: self.scrollView.frame.width, height: self.scrollView.frame.height) )
        view.imageView.image = data.image
        view.tag = i + viewTagValue
        self.scrollView.addSubview(view)

        let label = UILabel(frame: CGRect.init(origin:CGPoint.init(x: 0, y: 80), size: CGSize.init(width: 0,height: 200)))
        label.text = data.title
        label.font = UIFont.boldSystemFont(ofSize: 200)
        label.textColor = UIColor.black
        label.sizeToFit()
        label.textAlignment = .center
        label.tag = i + tagValue
        if i == 0 {
            label.center.x = view.center.x

        } else {
            label.center.x = view.center.x - self.scrollView.frame.width
        }
        self.scrollView.addSubview(label)
        i  += 1
    }
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView == scrollView {

        for i in 0..<scrollViewData.count {
        let label = scrollView.viewWithTag(i+tagValue) as! UILabel
        let view = scrollView.viewWithTag(i+viewTagValue) as! CustomView
        var scrollContentOffset = scrollView.contentOffset.x + self.scrollView.frame.width

        var viewOffset = (view.center.x - scrollView.frame.width / 2) - scrollContentOffset
        view.center.x = scrollContentOffset - (scrollView.frame.width / 4 - viewOffset) / 2


    }
    }
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
class CustomView: UIView {

    let imageView : UIImageView = {
        let imageView = UIImageView()
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.contentMode = .scaleAspectFit
        return imageView
    } ()

    override init(frame: CGRect) {
        super.init(frame: frame)

        self.addSubview(imageView)

        imageView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
        imageView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
        imageView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
        imageView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

}

感谢您的时间!

0 个答案:

没有答案