3
votes

That below is part of my app when you click the screen it will register the touch and either move to the previous photo or the next photo depending on the touch . Right now anywhere that you touch the screen it will register and move to the previous or next image . I want to restrict that and only register touches on the red squares so that if you touch anything between the 2 red squares nothing will happen . This is my code

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {


    for touch in touches {
        let location = touch.location(in: self.view)

       if location.x <  self.view.layer.frame.size.width / 2        {
  // touched left side of screen show previous image if exist

        }
        else {

     // touched right side of screen show next 

        }
    }
}

I think the key is the let location = touch.location(in: self.view) then location.x knows the width location where you touched . I want to only register taps on say the leftest 10% of the screen and the rightest 10% of the screen . Any suggestions would be great I'm using swift 4 .

( The red squares are only for illustration . I added using a photo editing feature it's not part of the app )

enter image description here

2
Add two transparent buttons or two transparent UIviews with tap gesture recognisers.Paulw11
I agree, this is what tap gesture recognizers were made for. Overriding touchesBegan is an unnecessarily involved process for this, and it may require overriding other touch methods depending on the subclass.fake girlfriends
I agree, turn the red rectangles into clear rectangles and put tap gesture recognizers on them.matt

2 Answers

6
votes

In ViewDidLoad, you can add two invisible UIView to get the tap gesture on either side as below,

override func viewDidLoad() {
    super.viewDidLoad()

    let touchArea = CGSize(width: 80, height: self.view.frame.height)

    let leftView = UIView(frame: CGRect(origin: .zero, size: touchArea))
    let rightView = UIView(frame: CGRect(origin: CGPoint(x: self.view.frame.width - touchArea.width, y: 0), size: touchArea))

    leftView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(leftViewTapped)))
    rightView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(rightViewTapped)))

    leftView.backgroundColor = .clear
    rightView.backgroundColor = .clear

    self.view.addSubview(leftView)
    self.view.addSubview(rightView)
}

@objc func leftViewTapped() {
    print("Left")
}

@objc func rightViewTapped() {
    print("Right")
}

OR

You can update the current method in question as below to get required result.

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard let location = touches.first?.location(in: self.view) else { return }

    let touchAreaWidth: CGFloat = 80

    if location.x <= touchAreaWidth {
        print("Left area touched")
    } else if location.x >= (self.view.frame.size.width - touchAreaWidth) {
        print("Right area touched")
    } else {
        print("Ignore in-between touch.")
    }
}
3
votes

For me the accepted answer didn't work. What worked was:

func addLeftTapGesture() {
    let leftTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(leftTap))
    leftTapGestureRecognizer.delegate = self
    view.addGestureRecognizer(leftTapGestureRecognizer)
}

func addRightTapGesture() {
    let rightTapGestureRecongnizer = UITapGestureRecognizer(target: self, action: #selector(rightTap))
    rightTapGestureRecongnizer.delegate = self
    view.addGestureRecognizer(rightTapGestureRecongnizer)
}

Then I check which part of the screen was touched:

@objc func rightTap(tap: UITapGestureRecognizer) {
    let point = tap.location(in: self.view)
    let rightArea = CGRect(x: view.frame.width/2, y: 0, width: view.frame.width/2, height: view.frame.height)
    if rightArea.contains(point) {
        print("Right tapped")
    }
    currentImage += 1
}

@objc func leftTap(tap: UITapGestureRecognizer) {
    let point = tap.location(in: self.view)
    let leftArea = CGRect(x: 0, y: 0, width: view.frame.width/2, height: view.frame.height)
    if leftArea.contains(point) {
        print("Left tapped")
    }
    currentImage -= 1
}