gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 08:04:54 25 4
gpt4 key购买 nike

我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com