swift - 如何使用向右滑动的手势执行 segue

标签 swift swipe collectionview

我想通过向右滑动手势来执行 segue。

showing a finger scrolling at the edge of the cell

最佳做法是什么?

最佳答案

我正在实现一个带有“从左到右滑动”的转场,带有一个自定义转场和一个滑动手势识别器

您需要创建一个新的 .swift 文件并在界面构建器属性中使用 segue 作为类。

enter image description here

//  Created by David Seek on 25.04.16.
//  Copyright © 2016 David Seek. All rights reserved.
//

import UIKit

class FromLeftToRightSegue: UIStoryboardSegue {
    override func perform() {

        let firstVC = self.sourceViewController.view as UIView!
        let secondVC = self.destinationViewController.view as UIView!

        let screenWidth = UIScreen.mainScreen().bounds.size.width
        let screenHeight = UIScreen.mainScreen().bounds.size.height


        secondVC.frame = CGRectMake(-screenWidth, 0, screenWidth, screenHeight)

        let window = UIApplication.sharedApplication().keyWindow
        window?.insertSubview(secondVC, aboveSubview: firstVC)

        // Animate the transition.
        UIView.animateWithDuration(0.3, animations: { () -> Void in // set animation duration

            firstVC.frame = CGRectOffset(firstVC.frame, 0.0, 0.0) // old screen stay

            secondVC.frame = CGRectOffset(secondVC.frame, screenWidth, 0.0) // new screen strave from left to right

        }) { (Finished) -> Void in
            self.sourceViewController.presentViewController(self.destinationViewController as UIViewController,
                                                            animated: false,
                                                            completion: nil)
        }
    }

}

Swipe Gesture Recognizer 也需要输入到一个新的 .swift 文件中:

import Foundation
import UIKit

//
//  UISwipeGestureRecognizer.h
//  UIKit
//
//  Copyright (c) 2009-2015 Apple Inc. All rights reserved.
//

// Recognizes: when numberOfTouchesRequired have moved mostly in the specified direction, enough to be considered a swipe.
//             a slow swipe requires high directional precision but a small distance
//             a fast swipe requires low directional precision but a large distance

// Touch Location Behaviors:
//     locationInView:         location where the swipe began. this is the centroid if more than one touch was involved
//     locationOfTouch:inView: location of a particular touch when the swipe began

public struct UISwipeGestureRecognizerDirection : OptionSetType {
    public init(rawValue: UInt)

    public static var Right: UISwipeGestureRecognizerDirection { get }
    public static var Left: UISwipeGestureRecognizerDirection { get }
    public static var Up: UISwipeGestureRecognizerDirection { get }
    public static var Down: UISwipeGestureRecognizerDirection { get }
}

@available(iOS 3.2, *)
public class UISwipeGestureRecognizer : UIGestureRecognizer {

    public var numberOfTouchesRequired: Int // default is 1. the number of fingers that must swipe
    public var direction: UISwipeGestureRecognizerDirection // default is UISwipeGestureRecognizerDirectionRight. the desired direction of the swipe. multiple directions may be specified if they will result in the same behavior (for example, UITableView swipe delete)
}

通过以下方式在您想要的 ViewController.swift 中调用:

private var swipeGestureRecognizer: UISwipeGestureRecognizer?

override func viewDidLoad() {
    super.viewDidLoad()
    swipeGestureRecognizer = MySwipeGestureRecognizer(target: self, swipeLeftSegue: "yourSwipeLeftSegue", swipeRightSeque: "yourSwipeRightSegue")
        view.addGestureRecognizer(swipeGestureRecognizer!)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "yourSwipeLeftSegue" {
        //your code goes here
    }
}

希望这对你有用,如果你有任何问题,请告诉我。

关于swift - 如何使用向右滑动的手势执行 segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36972102/

相关文章:

ios - Swift URL appendingPathComponent 将 `?` 转换为 `%3F`

ios - 将数据从 CollectionView 传递到 TabBarController,而不是在 Swift 中传递给他的 child

wpf - 分组的 CollectionView 的组可以水平呈现吗?

ios - 如何从 Material Inspector 设置 colorBufferWriteMask

ios - 动画 UIView 上的动画层

ios - 无法专门化非泛型类型 'Set'

javascript - 滑动以显示/隐藏div?

c# - Unity3d 如何从另一个脚本引用滑动控件?

android - 如何通过左右滑动更改 Activity

ios - 在被点击的单元格的索引处更改数据源