ios - 为 View 的子部分定义操作

标签 ios swift optimization uipangesturerecognizer

我有一个 View ,里面有一个由 PanGesture 控制的圆圈。它的目的是当用户在 View 中拖动这个圆圈时,一些参数会相应地改变。

这是我的 View Controller :

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 上时一些参数发生变化。当圆圈位于 View 的右侧时, View 和相同参数会发生变化。这已经完成了(以我能想到的唯一方式)。

现在我想将我的 y 轴分成 9 个小节。我可以使用 if 语句,但这对我来说似乎有点不干净。有什么有效且更优雅的方法吗?

期待任何建议,谢谢!

最佳答案

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

}

关于ios - 为 View 的子部分定义操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45478350/

相关文章:

swift - 当应用程序关闭时,通知 willPresent 不会调用

ios - 要求用户授予位置权限的警告框未显示

用于从数据库返回新访问者的 MySQL 查询

ios - iOS 上 Objective-C 中的类型

IOS 崩溃 - 崩溃 EXC_CRASH (SIGABRT) - 错误 109

xcode - Swift 2.0 代码在 Xcode 中有效,但在 Playground 中无效

mysql - 优化 MySql InnoDB 数据库中的慢速查询

performance - 如何加速 Google App Engine Go 单元测试?

ios - 是否有命令从 Firebase App Distribution 获取最新版本号,就像 TestFlight latest_testflight_build_number 一样?

ios - 在 Objective-C 中使用太多静态变量是一种不好的做法吗?