首页 > 解决方案 > 我想使用 swift 在 xcode 中创建这个滑动 UIVies,我应该使用什么?一个collecntionView或TableView?

问题描述

我想用 xcode 和 swift 在我的 ios 应用程序中创建一个精确的屏幕我无法创建它,我应该使用什么?一个collectionViewScroolView

导入 UIKit

class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{


@IBOutlet weak var newCollectionView: UICollectionView!






override func viewDidLoad() {
    super.viewDidLoad()
    newCollectionView.delegate = self
    newCollectionView.dataSource = self
    // Do any additional setup after loading the view, typically from a nib.
}


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 4
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)

    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: view.frame.width, height: view.frame.height)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}

}

在此处输入图像描述

标签: iosswiftxcodetableviewcollectionview

解决方案


这是我的课。它完全有效。我还添加了故事板的图像。链接到我的 github 项目:https ://github.com/Agisight/scroller.git

class ViewController: UIViewController, UIScrollViewDelegate {
    @IBOutlet weak var scroll: UIScrollView!

    @IBOutlet var btns: [UIView]!
    override func viewDidLoad() {
        super.viewDidLoad()
        scroll.delegate = self
    }
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        methodAnimation()
    }

    func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
        methodAnimation()
    }

    func methodAnimation() {
        if btns.count == 0 {return}

        var i = 0
        // i – calculate it, it is your visible/desirable view.
        // here is variant of code
        let dragOffset = view.frame.width * 0.5 // callibrate it for you
        let offX = scroll.contentOffset.x
        i = Int(offX + dragOffset) / Int(view.frame.width)

        i = max(0, min(i, btns.count - 1))
        let yourX = btns[i].frame.width * CGFloat(i) // ()
        let p = CGPoint(x: yourX, y: 0);
        scroll.setContentOffset(p, animated: true)
    }
}

我的 UIViewController 滚动


推荐阅读