我是一名学生,对编码非常陌生(在这种情况下,使用Swift语言编写Xcode)。 我试图使用按钮/滑块/手势来更改变量,如下所示:
有两个变量(firstVariable和secondVariable),并设置了一个UIslider输入,该输入应定义变量的值 我希望变量与滑块/触摸手势的值相同,但即使在模拟器中滚动滑块后,变量仍保持4和6。 我认为是因为它们超出了滑块功能的范围,但是如何链接它们?
class MyViewController : UIViewController {
var firstVariable = 4
override func viewDidLoad() {
super.viewDidLoad()
var secondVariable = 6
print(secondVariable)
print(firstVariable)
let myslider = UISlider()
//create a UISLider
myslider.maximumValue = 100
myslider.minimumValue = 0
myslider.value = 30
myslider.frame = CGRect(x: 150, y: 200, width: 200, height: 40)
myslider.addTarget(self, action: #selector(sliderinput), for: UIControl.Event.valueChanged)
self.view.addSubview(myslider)
}
@objc func sliderinput(sender: UISlider) {
firstVariable = Int(sender.value)
secondVariable = Int(sender.value)
}
}
谢谢!
答案 0 :(得分:0)
首先,将<section>
<div class="wrapper">
<img id="main-slider" src="https://via.placeholder.com/1365x528?text=Slider">
<div class="badge">
<span class="text">Contact us</span>
<a class="link">(555) <strong>123 4567</strong></a>
</div>
</div>
</section>
放在viewDidLoad之外,因为您无法在viewDidLoad之外访问它。要了解secondVariable
,请阅读this。并且您需要打印变量的值才能看到值实际上在变化。
scope
答案 1 :(得分:0)
对不起,这是真实情况。我想制作一个无限的时间轴,例如iOS日历天标签或视频编辑软件的时间轴。 我还没有弄清楚如何从触摸的开始和结束获取距离,以及如何在开始时将其链接到scrollDistance变量,因此我可以创建向下滚动效果,然后创建一个pinchgesturerecognizer来设置间距/缩放线之间。 我知道我可以使用UIscrollview或UIcollectionview,但是我觉得创建自定义一个xD是个好主意 再次感谢大家的支持! :D
import UIKit
class ViewController: UIViewController {
let lineElements: Array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
let spacing: Int = 30 //spacing between elements
var scrollDistance = 90 // 90 for example, this should be the pangesture distance
//UIPanGestureRecogn function to determine the Y scroll distance from touch began to ended
@objc func touchinput (sender: UIPanGestureRecognizer) {
let touchlocation = sender.location(in: view)
var touchStart: CGPoint
var touchEnd: CGPoint
print("touch current location is", touchlocation)
if sender.state == UIGestureRecognizer.State.began {
touchStart = sender.location(in: view)
print("touch start", touchStart)
} else if sender.state == UIGestureRecognizer.State.ended {
touchEnd = sender.location(in: view)
print("touch end", touchEnd)
}
//I haven’t figure out how to get the distance between start and end and link it to the scrollDistance
//let scrollDistance = touchEnd.y - touchStart.y
}
override func viewDidLoad() {
super.viewDidLoad()
// ===================== Timeline a partir daqui =========================
let totalElements: Int = lineElements.count
for n in 1...totalElements {
let yPosition = lineElements[n - 1] * spacing + scrollDistance //Get Line heigh index times the spacing + scroll distance by touch pan gesture
let hourlabel = UILabel()
let linepath = UIBezierPath()
linepath.move(to: CGPoint(x: 60, y: yPosition))
linepath.addLine(to: CGPoint(x: 350, y: yPosition))
linepath.lineWidth = 8
let lineshape = CAShapeLayer()
lineshape.path = linepath.cgPath
lineshape.strokeColor = UIColor.blue.cgColor
lineshape.fillColor = UIColor.white.cgColor
self.view.layer.addSublayer(lineshape)
hourlabel.frame = CGRect(x: 5, y: yPosition - 20, width: 45, height: 40)
hourlabel.text = "\(n):00"
hourlabel.font = UIFont(name: "Avenir-Claro", size: 12)
hourlabel.textColor = UIColor.blue
hourlabel.textAlignment = .right
self.view.addSubview(hourlabel)
}
// ===== GESTURE =====
let gesto = UIPanGestureRecognizer()
gesto.addTarget(self, action: #selector(touchinput))
view.addGestureRecognizer(gesto)
}
}
´´´