SwingerDinger
SwingerDinger

Reputation: 276

UIPanGestureRecognizer on subclass UIView

I've been trying to lean more about subclassing certain objects. Now I've subclassed a UIView which has a PanGestureRecognizer for swiping left and right.

Can't seem to find the problem. It won't even move the UIView. I've tried looking the lifecycle of an UIView to set the isUserInteractionEnabled to true, but with no result. See the code below:

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
}
}

Upvotes: 0

Views: 390

Answers (1)

Mohammadalijf
Mohammadalijf

Reputation: 1377

add these code to your SwiperView see if it solves the problem

  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)
    }

Upvotes: 1

Related Questions