定义视图子部分的操作

时间:2017-08-03 08:06:34

标签: ios swift optimization uipangesturerecognizer

我的视图中有一个由PanGesture控制的圆圈。当用户在视图中拖动该圆圈时,一些参数会相应地改变。

这是我的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
    }
}

}

我想将我的观点分成小节。在我的x轴上,我想要2个子部分,即当圆圈在lhs上时一些参数改变。当圆圈位于视图的右侧时,视图和相同参数会发生变化。这已经完成了(以我能想到的唯一方式)。

现在我想把我的y轴分成9个小节。我可以使用if语句,但这对我来说似乎有点不洁净。有没有有效和更优雅的方式呢?

期待任何建议,谢谢!

1 个答案:

答案 0 :(得分:0)

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

}