2016-05-01 37 views
3

我想通过向右滑动手势来执行继续。如何通过向右滑动手势执行继续

showing a finger scrolling at the edge of the cell

什么是最好的做法是什么?

+0

如果您只希望在用户从屏幕边缘滑动时发生segue,请查看[UIScreenEdgePanGestureRecognizer](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIScreenEdgePanGestureRecognizer_class/# // apple_ref/OCC/instp/UIScreenEdgePanGestureRecognizer /边缘)。 –

回答

4

我实现了一个赛格瑞“从左到右滑动”与定制赛格瑞轻扫手势识别

您需要创建一个新的.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) 
     } 
    } 

} 

轻扫手势识别器也需要进入一个新的.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 
    } 
} 

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

+0

哇是的这是我正在寻找的,谢谢你的帮助:) –

+0

非常欢迎 –