子类UIView上的UIPanGestureRecognizer

时间:2016-12-12 16:17:54

标签: ios swift uiview uipangesturerecognizer

我一直在努力更多地关注某些对象的子类化。 现在,我已经为UIView创建了子类,其中PanGestureRecognizer左右滑动。

似乎无法找到问题所在。它甚至不会移动UIView。我已经尝试查看UIView的生命周期,将isUserInteractionEnabled设置为true,但没有结果。请参阅以下代码:

VC

import UIKit

class SwipeViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    addNewProfile()
}

private func addNewProfile() {

    let swipeView = SwiperView(frame: CGRect(x: self.view.bounds.width / 2 - 150, y: self.view.bounds.height / 2 - 75, width: 300, height: 150))
    swipeView.parentView = self.view
    swipeView.delegate = self
    swipeView.shadow = true
    swipeView.isUserInteractionEnabled = true
    swipeView.backgroundColor = UIColor.white
    swipeView.alpha = 0.0
    view.addSubview(swipeView)

    UIView.animate(withDuration: 0.3, animations: {
        swipeView.alpha = 1.0
    }, completion: { (succeed) in
        swipeView.isUserInteractionEnabled = true
    })
}
}

//MARK: - ChosenSwipeResultDelegate
extension SwipeViewController: ChosenSwipeResultDelegate {

    func pickedLeftSide() {

    }

    func pickedRightSide() {

    }

}

SwiperView

import UIKit

protocol ChosenSwipeResultDelegate {

func pickedLeftSide()
func pickedRightSide()
}

@IBDesignable class SwiperView: UIView {

private var _shadow: Bool!
private var _parentView: UIView!

var delegate: ChosenSwipeResultDelegate?

var parentView: UIView {
    set {
        _parentView = newValue
    }
    get {
        return _parentView
    }
}

@IBInspectable var shadow: Bool {
    get {
        return layer.shadowOpacity > 0.0
    }
    set {
        if newValue == true {
            addShadow()
        }
    }
}

@IBInspectable var cornerRadius: CGFloat {
    get {
        return layer.cornerRadius
    }
    set {
        layer.cornerRadius = newValue

        if shadow == false {
            layer.masksToBounds = true
        }
    }
}

override func setNeedsLayout() {
    super.setNeedsLayout()
    isUserInteractionEnabled = true

}

override func awakeFromNib() {
    super.awakeFromNib()

    isUserInteractionEnabled = true
    let dragGesture = UIPanGestureRecognizer(target: self, action: #selector(SwiperView.dragging(gesture:)))
    addGestureRecognizer(dragGesture)
}

func dragging(gesture: UIPanGestureRecognizer) {

    let translation = gesture.translation(in: parentView)
    let tinderView = gesture.view!

    tinderView.center = CGPoint(x: parentView.bounds.width / 2 + translation.x, y: parentView.bounds.height / 2 + translation.y)

    let xFromCenter = tinderView.center.x - parentView.bounds.width / 2

    let scale = min(100 / abs(xFromCenter), 1)

    var rotation = CGAffineTransform(rotationAngle: xFromCenter / 200)

    let stretch = rotation.scaledBy(x: scale, y: scale)

    tinderView.transform = stretch

    if gesture.state == .ended {

        if tinderView.center.x < 100 {
            print("left")

            UIView.animate(withDuration: 0.3, animations: {
                tinderView.alpha = 0.0
            }, completion: { (succeed) in

                self.delegate?.pickedLeftSide()
            })

        } else if tinderView.center.x > parentView.bounds.width - 100 {

            print("right")

            UIView.animate(withDuration: 0.3, animations: {
                tinderView.alpha = 0.0
            }, completion: { (succeed) in

                self.delegate?.pickedRightSide()
            })
        } else {

            print("Not chosen")

            rotation = CGAffineTransform(rotationAngle: 0)

            let stretch = rotation.scaledBy(x: 1, y: 1)
            tinderView.transform = stretch

            tinderView.center = CGPoint(x: parentView.bounds.width / 2, y: parentView.bounds.height / 2)

        }
    }
}

private func addShadow(shadowColor: CGColor = UIColor.black.cgColor, shadowOffset: CGSize = CGSize(width: 1.0, height: 2.0), shadowOpacity: Float = 0.4, shadowRadius: CGFloat = 3.0) {
    layer.shadowColor = shadowColor
    layer.shadowOffset = shadowOffset
    layer.shadowOpacity = shadowOpacity
    layer.shadowRadius = shadowRadius
}
}

1 个答案:

答案 0 :(得分:1)

将这些代码添加到SwiperView,看看它是否解决了问题

  override init(frame: CGRect) {
        super.init(frame: frame)

        isUserInteractionEnabled = true
        let dragGesture = UIPanGestureRecognizer(target: self, action: #selector(SwiperView.dragging(gesture:)))
        addGestureRecognizer(dragGesture)

    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        isUserInteractionEnabled = true
        let dragGesture = UIPanGestureRecognizer(target: self, action: #selector(SwiperView.dragging(gesture:)))
        addGestureRecognizer(dragGesture)
    }