swift - Swift 中类似控制中心的滑动 View 实现

标签 swift uiview uigesturerecognizer control-center

我正在尝试在应用程序上创建交互式滑动过渡。

目标是当 View Controller 从底部拖动(而不是从底部边缘拖动,因此不会与控制中心冲突)时呈现一个 uiview,并将部分覆盖主视图 Controller (就像 iOS 一样)控制中心)。转换应该是交互式的,即根据用户的拖动。

很高兴听到有关可用 API 的想法。

最佳答案

以下代码将执行在 Xcode 8 中测试并有效的简单幻灯片 View 动画代码。

 import UIKit

class ViewController: UIViewController,UIGestureRecognizerDelegate {

// I am using VisualView as a slideView
@IBOutlet weak var slideView: UIVisualEffectView!

//Button to open slideView
@IBOutlet weak var button: UIBarButtonItem!

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

    // setting background for sideView
       slideView.backgroundColor = UIColor(patternImage: UIImage(named: "original.jpg")!)

    //Initial hide so it won't show.when the mainView loads...
      slideView.isHidden = true

    // DownSwipe to hide slideView
       let downSwipe = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.handleSwipes(_:)))
       downSwipe.direction = .down
       view.addGestureRecognizer(downSwipe)
    }

   // set UIButton to open slideVie...(I am using UIBarButton)
     @IBAction func sdrawButton(_ sender: AnyObject) {

      self.slideView.isHidden = false
      slideView.center.y += view.frame.height

      UIView.animate(withDuration: 0.7, delay: 0, options: UIViewAnimationOptions.curveEaseIn, animations:{
        self.slideView.center.y -= self.view.frame.height
        self.button.isEnabled = false
        }, completion: nil)  
}

   func handleSwipes(_ sender:UISwipeGestureRecognizer) {
    if (sender.direction == .down) {
        print("Swipe Down")

        self.slideView.isHidden = true

        self.slideView.center.y -= self.view.frame.height
        UIView.animate(withDuration: 0.7, delay: 0, options: UIViewAnimationOptions.curveEaseOut, animations:{
            self.slideView.center.y += self.view.frame.height
            self.button.isEnabled = true
            }, completion: nil)
  }  
  }
  }

让我知道该代码适合您...

关于swift - Swift 中类似控制中心的滑动 View 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39875306/

相关文章:

iphone - 我怎么知道触发 touchesBegan 的手指也是触发手势的手指?

arrays - 过滤对象数组中的两个值

swift - 获取 UIView/UIImageView 的特定角坐标,而不管其旋转

ios - 当 UIView 从它的 superView 分离时如何得到通知?

ios - Swift iOS - 视觉框架文本识别和矩形

ios - Objective-c shouldRecognizeSimultaneouslyWithGestureRecognizer 禁用其他GestureRecognizer

ios - 实例成员不能用于类型 View Controller

ios - 如何从 UITableView 调用 UITableViewCell 中的添加目标按钮函数?

ios - 如何单击全屏 UIView 后面的按钮?

ios - 检测在手势识别器对象区域之外开始的滑动