简体   繁体   English

Swift滑动视图

[英]Swift swipe-able views

I'm new to Swift and iOS development, and am looking to set up a series of views, or "cards", that can be swiped left and right, with the next/previous cards remaining at the edges to indicate that they are there. 我是Swift和iOS开发的新手,我希望设置一系列可以左右滑动的视图或“卡片”,下一张/上一张卡片留在边缘,表示它们在那里。 This is done in iOS App Store previews: 这是在iOS App Store预览中完成的:

应用商店预览

So far all I've really found is this Medium article: iOS Swift: Swipe View . 到目前为止,我真正发现的是这篇中篇文章: iOS Swift:Swipe View However I'm not sure if this is the right way to do what I'm asking above, and I also don't know how to get the next/previous cards to remain at the edges on each swipe. 但是我不确定这是否是我上面要求的正确方法,而且我也不知道如何让下一张/上一张卡片保持在每次滑动的边缘。

Does anyone have any tips for achieving this? 有没有人有任何实现这个的提示?

The easiest way will be to use UICollectionView and UICollectionViewFlowLayout . 最简单的方法是使用UICollectionViewUICollectionViewFlowLayout UICollectionViewFlowLayout is provided out of the box and is very powerful. UICollectionViewFlowLayout是开箱即用的,功能非常强大。

Basically you need: 基本上你需要:

  1. Add UICollectionView to your view controller. UICollectionView添加到视图控制器。
  2. By default UICollectionView goes with Flow layout selected. 默认情况下, UICollectionView选择Flow布局。
  3. Configure UICollectionViewFlowLayout 配置UICollectionViewFlowLayout
    • Set scroll direction to Horizontal 将滚动方向设置为水平
    • Define item size and spacing. 定义项目大小和间距。 Keep in mind that you need just one item to be fully visible; 请记住,您只需要一个项目就可以完全看到; in addition spaces to the left and right should be visible as well; 此外,左侧和右侧的空间也应该是可见的; further more you need to show pieces of previous and next cards, so some basic math waiting for you:). 你需要更多地展示上一张和下一张卡片,所以一些基本的数学等着你:)。 One small trick to make you very first and very last cards be centered properly: you should specify vertical section insets to be equal to the sum of visible width of previous card plus space between cards. 一个小技巧可以使你的第一张和最后一张牌正确居中:你应该指定垂直部分插入等于前一张牌的可见宽度加上牌之间的空格。

Steps above should be enough to have vertically scrollable cards view. 上面的步骤应该足以具有垂直可滚动的卡片视图。 But it will not look great! 但它看起来不会很棒! To make it great you probably need perfectly to center most appropriate card after user end scrolling. 为了使它变得更好,您可能需要在用户结束滚动后完美地居中最合适的卡片。 To achieve sticky scrolling you need to work with the following UICollectionViewDelegate methods: 要实现粘性滚动,您需要使用以下UICollectionViewDelegate方法:

// compute collection view content offset in method bellow
override func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
    ...
}

// compute targetContentOffset in method bellow 
override func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    ...
}

... last hint: to make collection view deceleration look quick in code of your view controller do: ...最后提示:在视图控制器的代码中使集合视图减速看起来很快:

self.collectionView?.decelerationRate = UIScrollViewDecelerationRateFast

Paging indicator: 寻呼指标:

In example that you provide there is a paging control that reflects current item and overall items count. 在您提供的示例中,有一个分页控件可以反映当前项目和整体项目计数。 You need to add UIPageControll manually; 您需要手动添加UIPageControll ; you can do it in storyboard. 你可以在故事板中做到这一点。 The best place to update page control state will be: 更新页面控制状态的最佳位置是:

// method in UICollectionView delegate that you need to define.
// update your paging info in method bellow.
override func scrollViewDidScroll(scrollView: UIScrollView) {
    ...    
}

Remarks: 备注:

Methods that deal with scroll view that i've listed are inherited by UICollectionViewDelegate from UIScrollViewDelegate . 处理我列出的滚动视图的方法由UICollectionViewDelegateUIScrollViewDelegate继承。 UICollectionView itself derived from UIScrollViewDelegate . UICollectionView本身派生自UIScrollViewDelegate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM