swift:计时器(每秒)+ 手势识别器使 GUI 崩溃
swift: timer (every second) + gestureRecognizer crashs the GUI
我有一个简单的手势识别器。当手势为 "UP" 时,菜单会向上滑动。 "DOWN": 菜单向下滑动。这行得通。
我也有一个计时器。它只是每秒计算一个数字++。这也行。但是计时器一起使我的 GUI 崩溃。
动画正确启动,但当计时器调用我的反方法时,它会跳回原始 GUI。
这是什么问题?我在 google 没有找到任何内容,但我想我搜索了错误的词。 (英语不是我的母语,德语也没有那么多文学...)
这是我的代码片段:
class ViewController: UIViewController, MKMapViewDelegate{
@IBOutlet weak var swipe: UILabel!
var number: Int = 0
@IBOutlet weak var menuView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
var topSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
var downSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
topSwipe.direction = .Up
downSwipe.direction = .Down
view.addGestureRecognizer(topSwipe)
view.addGestureRecognizer(downSwipe)
var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "counter", userInfo: nil, repeats: true)
}
func counter() {
number++
swipe.text = "\(number)"
}
func handleSwipes(sender:UISwipeGestureRecognizer) {
if (sender.direction == .Up) {
print("TOP")
var menuPosition = CGPointMake(self.menuView.frame.origin.x, self.menuView.frame.origin.y - 100.0)
UIView.animateWithDuration(0.65, animations: {
self.menuView.frame = CGRectMake(menuPosition.x,menuPosition.y,self.menuView.frame.size.width,self.menuView.frame.size.height)
})
}
if (sender.direction == .Down) {
menuChoose = true
UIView.animateWithDuration(0.65, animations: {
var menuPosition = CGPointMake(self.menuView.frame.origin.x, self.menuView.frame.origin.y + 100.0)
self.menuView.frame = CGRectMake(menuPosition.x,menuPosition.y,self.menuView.frame.size.width,self.menuView.frame.size.height)
})
}
}
}
我试图锁定定时器。因此,当手势开始时,只需计算没有 swipe.text = "(number)" 的数字
动画结束,但在 "unlock" 之后 GUI 跳回原点。
感谢您提供一些我可以搜索的代码片段或关键字:)
似乎每当设置 UILabel
的 text
属性 时,它都会重新绘制视图,从而导致重置 menuView
的位置(也许有人可以在评论中详细说明 "why")。如果您使用自动布局约束来操纵 menuView
的位置,它会起作用。您可以通过设置 spaceConstraint.constant += 100
或 spaceConstraint.constant -= 100
然后调用
来完成此操作
UIView.animateWithDuration(0.65, animations: {
self.view.layoutIfNeeded()
})
我有一个简单的手势识别器。当手势为 "UP" 时,菜单会向上滑动。 "DOWN": 菜单向下滑动。这行得通。
我也有一个计时器。它只是每秒计算一个数字++。这也行。但是计时器一起使我的 GUI 崩溃。 动画正确启动,但当计时器调用我的反方法时,它会跳回原始 GUI。
这是什么问题?我在 google 没有找到任何内容,但我想我搜索了错误的词。 (英语不是我的母语,德语也没有那么多文学...)
这是我的代码片段:
class ViewController: UIViewController, MKMapViewDelegate{
@IBOutlet weak var swipe: UILabel!
var number: Int = 0
@IBOutlet weak var menuView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
var topSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
var downSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
topSwipe.direction = .Up
downSwipe.direction = .Down
view.addGestureRecognizer(topSwipe)
view.addGestureRecognizer(downSwipe)
var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "counter", userInfo: nil, repeats: true)
}
func counter() {
number++
swipe.text = "\(number)"
}
func handleSwipes(sender:UISwipeGestureRecognizer) {
if (sender.direction == .Up) {
print("TOP")
var menuPosition = CGPointMake(self.menuView.frame.origin.x, self.menuView.frame.origin.y - 100.0)
UIView.animateWithDuration(0.65, animations: {
self.menuView.frame = CGRectMake(menuPosition.x,menuPosition.y,self.menuView.frame.size.width,self.menuView.frame.size.height)
})
}
if (sender.direction == .Down) {
menuChoose = true
UIView.animateWithDuration(0.65, animations: {
var menuPosition = CGPointMake(self.menuView.frame.origin.x, self.menuView.frame.origin.y + 100.0)
self.menuView.frame = CGRectMake(menuPosition.x,menuPosition.y,self.menuView.frame.size.width,self.menuView.frame.size.height)
})
}
}
}
我试图锁定定时器。因此,当手势开始时,只需计算没有 swipe.text = "(number)" 的数字 动画结束,但在 "unlock" 之后 GUI 跳回原点。
感谢您提供一些我可以搜索的代码片段或关键字:)
似乎每当设置 UILabel
的 text
属性 时,它都会重新绘制视图,从而导致重置 menuView
的位置(也许有人可以在评论中详细说明 "why")。如果您使用自动布局约束来操纵 menuView
的位置,它会起作用。您可以通过设置 spaceConstraint.constant += 100
或 spaceConstraint.constant -= 100
然后调用
UIView.animateWithDuration(0.65, animations: {
self.view.layoutIfNeeded()
})