ios – 由夹点和平移手势识别器同时触发的交互式ViewController转换

前端之家收集整理的这篇文章主要介绍了ios – 由夹点和平移手势识别器同时触发的交互式ViewController转换前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有两个viewControllers:

ViewController1

一组复杂的子视图控制器,中间有一个imageView

ViewController2

一个包含imageView的scrollView

我想要实现的是两个viewControllers之间的转换,它通过从viewController 1捏合imageView来触发,使其放大并切换到viewController 2.当转换结束时,imageView应该放大到目前为止因为它在捏合手势触发过渡期间被缩放.

同时我想支持在执行缩放转换时平移图像,这样就像缩放一样,处于结束状态的图像将被转换到它被平移到的位置.

到目前为止,我已经尝试过Hero过渡pod和我自己编写的自定义viewController过渡.英雄过渡的问题是图像没有正确地捕捉到第二个viewController中的结束状态.我使用自定义viewController转换的问题是我无法同时进行缩放和平移.

有没有人知道如何在Swift中实现它?非常感谢帮助.

解决方法

问题可以分为两个:

>如何在imageView上使用平移手势实现捏拉缩放和拖动
>如何在视图控制器中显示一个视图控制器,其中一个子视图(vc2中的imageView)与子视图(vc1中的imageView)相同

捏合手势缩放:使用UIScrollView更容易实现捏缩放,因为它支持开箱即用,无需添加手势识别器.创建一个scrollView并添加您想要缩放的视图,其中包含pinch作为其子视图(scrollView.addSubview(imageView)).不要忘记添加scrollView本身(view.addSubview(scrollView)).

配置scrollView的最小和最大缩放比例:scrollView.minimumZoomScale,scrollView.maximumZoomScale.为scrollView.delegate设置委托并实现UIScrollViewDelegate:

func viewForZooming(在scrollView中:UIScrollView) – > UIView的?

在这种情况下哪个应该返回你的imageView,

也符合UIGestureRecognizerDelegate并实现:

func gestureRecognizer(_ gestureRecognizer:UIGestureRecognizer,shouldRecognizeSimultaneouslyWith otherGestureRecognizer:UIGestureRecognizer) – >布尔

哪个应该返回true.这是允许我们使用平移手势识别器与内部捏手势识别器一起工作的关键.

平移手势拖动:只需使用目标创建平移手势识别器,然后将其添加到滚动视图scrollView.addGestureRecognizer(平移).

处理手势:捏缩放在这个阶段很好地工作,除了你想要在捏合结束时呈现第二个视图控制器.再实现一个UIScrollViewDelegate方法,以便在缩放结束时收到通知

func scrollViewDidEndZooming(_ scrollView:UIScrollView,with view:UIView?,atScale scale:CGFloat)

调用显示详细视图控制器presentDetail()的方法,我们将稍微实现它.

下一步是处理平移手势,我将让代码解释自己:

// NOTE: Do NOT set from anywhere else than pan handler.
private var initialTouchPositionY: CGFloat = 0
private var initialTouchPositionX: CGFloat = 0

@objc func panned(_ pan: UIPanGestureRecognizer) {

    let y = pan.location(in: scrollView).y
    let x = pan.location(in: scrollView).x

    switch pan.state {
    case .began:
        initialTouchPositionY = pan.location(in: imageView).y
        initialTouchPositionX = pan.location(in: imageView).x
    case .changed:
        let offsetY = y - initialTouchPositionY
        let offsetX = x - initialTouchPositionX
        imageView.frame.origin = CGPoint(x: offsetX,y: offsetY)
    case .ended:
        presentDetail()
    default: break
    }
}

该实现在平移位置之后移动imageView,并在手势结束时调用presentDetail().

在我们实现presentDetail()之前,前往详细视图控制器并添加属性以保存imageViewFrame和图像本身.现在在vc1中,我们实现了presentDetail():

private func presentDetail() {
    let frame = view.convert(imageView.frame,from: scrollView)
    let detail = DetailViewController()
    detail.imageViewFrame = frame
    detail.image = imageView.image

    // Note that we do not need the animation. 
    present(detail,animated: false,completion: nil)
}

在DetailViewController中,确保将imageViewFrame和图像设置为例如viewDidLoad,你将被设置.

完整的工作示例:

class ViewController: UIViewController,UIScrollViewDelegate,UIGestureRecognizerDelegate {

    let imageView: UIImageView = UIImageView()
    let scrollView: UIScrollView = UIScrollView()

    lazy var pan: UIPanGestureRecognizer = {
        return UIPanGestureRecognizer(target: self,action: #selector(panned(_:)))
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        imageView.image = // set your image

        scrollView.delegate = self
        scrollView.minimumZoomScale = 1.0
        scrollView.maximumZoomScale = 10.0

        scrollView.addSubview(imageView)
        view.addSubview(scrollView)

        scrollView.frame = view.frame

        let w = view.bounds.width - 30 // padding of 15 on each side
        imageView.frame = CGRect(x: 0,y: 0,width: w,height: w)
        imageView.center = scrollView.center

        scrollView.addGestureRecognizer(pan)
    }

    // NOTE: Do NOT set from anywhere else than pan handler.
    private var initialTouchPositionY: CGFloat = 0
    private var initialTouchPositionX: CGFloat = 0

    @objc func panned(_ pan: UIPanGestureRecognizer) {

        let y = pan.location(in: scrollView).y
        let x = pan.location(in: scrollView).x

        switch pan.state {
        case .began:
            initialTouchPositionY = pan.location(in: imageView).y
            initialTouchPositionX = pan.location(in: imageView).x
        case .changed:
            let offsetY = y - initialTouchPositionY
            let offsetX = x - initialTouchPositionX
            imageView.frame.origin = CGPoint(x: offsetX,y: offsetY)
        case .ended:
            presentDetail()
        default: break
        }
    }

    // MARK: UIScrollViewDelegate

    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return imageView
    }

    func scrollViewDidEndZooming(_ scrollView: UIScrollView,with view: UIView?,atScale scale: CGFloat) {
        presentDetail()
    }

    // MARK: UIGestureRecognizerDelegate

    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }

    // MARK: Private

    private func presentDetail() {
        let frame = view.convert(imageView.frame,from: scrollView)
        let detail = DetailViewController()
        detail.imageViewFrame = frame
        detail.image = imageView.image
        present(detail,completion: nil)
    }
}

class DetailViewController: UIViewController {

    let imageView: UIImageView = UIImageView()
    var imageViewFrame: CGRect!
    var image: UIImage?

    override func viewDidLoad() {
        super.viewDidLoad()
        imageView.frame = imageViewFrame
        imageView.image = image

        view.addSubview(imageView)
        view.addSubview(backButton)
    }

    lazy var backButton: UIButton = {
        let button: UIButton = UIButton(frame: CGRect(x: 10,y: 30,width: 60,height: 30))
        button.addTarget(self,action: #selector(back(_:)),for: .touchUpInside)
        button.setTitle("back",for: .normal)
        return button
    }()

    @objc func back(_ sender: UIButton) {
        dismiss(animated: false,completion: nil)
    }

}
原文链接:https://www.f2er.com/iOS/334354.html

猜你在找的iOS相关文章