为视图的子部分定义操作
Define actions for subsections of view
我有一个视图,里面有一个由 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 语句,但这对我来说似乎有点不干净。有什么有效且更优雅的方法吗?
期待任何建议,谢谢!
检查 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
}
}
}
我有一个视图,里面有一个由 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 语句,但这对我来说似乎有点不干净。有什么有效且更优雅的方法吗?
期待任何建议,谢谢!
检查 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
}
}
}