Search code examples
iosswiftoptimizationuipangesturerecognizer

Define actions for subsections of view


I have a view with a circle inside of it controlled by a PanGesture. It is the intention that when a user drags this circle around in the view that some parameters change accordingly.

Here is my viewcontroller:

import UIKit

class ViewController: UIViewController {

var circleCenter: CGPoint!

@IBOutlet weak var soundSpace: UIView!

override func viewDidLoad() {
    super.viewDidLoad()

    // Add a draggable view
    let circle = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 50.0, height: 50.0))
    circle.center = CGPoint(x: soundSpace.bounds.width/2, y: soundSpace.bounds.height/2) //self.view.center
    circle.layer.cornerRadius = 25.0
    circle.backgroundColor = UIColor.black

    // add pan gesture recognizer to
    circle.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(self.dragCircle)))

    self.soundSpace.addSubview(circle)
}

func dragCircle(gesture: UIPanGestureRecognizer) {
    let target = gesture.view!


    switch gesture.state {
    case .began, .ended:

        circleCenter = target.center

    case .changed:
        let translation = gesture.translation(in: self.soundSpace)

        // Ensure circle stays within subview 
        if (circleCenter.x + translation.x) > self.soundSpace.bounds.minX+25 &&
           (circleCenter.x + translation.x) < self.soundSpace.bounds.maxX-25 &&
           (circleCenter.y + translation.y) > self.soundSpace.bounds.minY+25 &&
           (circleCenter.y + translation.y) < self.soundSpace.bounds.maxY-25{


            target.center = CGPoint(x: circleCenter!.x + translation.x, y: circleCenter!.y + translation.y)

            // Check whether circle is in rhs or lhs              
            if target.center.x < (self.soundSpace.bounds.width / 2){
                print("Option 1")
            } else {
                print("Option 2")
            } 

        }
    default: break
    }
}

}

I would like to divide my view into subsections. On my x-axis I would like 2 subsections i.e. some parameter change when circle is on the lhs. of the view and same parameters change when circle is on the right hand side of the view. This has already been done (in the only manner I could think of on the top of my head).

Now I would like to divide my y-axis into 9 subsections. I could use an if-statement, but that seems a bit unclean to me. Is there any effective and more elegant way of doing that?

Looking forward to any suggestions, thanks!


Solution

  • To check the correct section of y

    func dragCircle(gesture: UIPanGestureRecognizer) {
        let target = gesture.view!
        switch gesture.state {
        case .began, .ended:
    
            circleCenter = target.center
    
        case .changed:
            let translation = gesture.translation(in: self.soundSpace)
    
            // Ensure circle stays within subview 
            if (circleCenter.x + translation.x) > self.soundSpace.bounds.minX+25 &&
               (circleCenter.x + translation.x) < self.soundSpace.bounds.maxX-25 &&
               (circleCenter.y + translation.y) > self.soundSpace.bounds.minY+25 &&
               (circleCenter.y + translation.y) < self.soundSpace.bounds.maxY-25{
    
    
                target.center = CGPoint(x: circleCenter!.x + translation.x, y: circleCenter!.y + translation.y)
    
                //------ y calculation ------
    
                let yPosition = target.center.y
                let totalHeight = self.soundSpace.bounds.height
                let totalYSections = 9
                let ySectionHeight = totalHeight/totalYSections
                let reminder = yPosition % ySectionHeight
                let correction = 0
                if (reminder > 0) {
                     correction = 1
                }
                let currentYSection = (yPosition / ySectionHeight) + correction
                print(currentYSection)
    
                //------ y calculation ------
    
                // Check whether circle is in rhs or lhs              
                if target.center.x < (self.soundSpace.bounds.width / 2){
                    print("Option 1")
                } else {
                    print("Option 2")
                } 
    
            }
        default: break
        }
    }
    
    }