[英]How to use Swipe gesture recognizer for a long press up?
我正在使用滑動手勢識別器通過向上滑動和向下滑動來增加和減少計數器。
向上滑動時,標簽也會偏移+10;向下滑動時,標簽會偏移-10。
一切都很好,但是一旦向上滑動,標簽的偏移量就會恢復為0。我的目標是只要向上滑動即可將偏移量保持在+10。 這是我的代碼:
private func setupSwipeGestures() {
var swipeUp = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
var swipeDown = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
swipeUp.direction = .Up
swipeDown.direction = .Down
view.addGestureRecognizer(swipeUp)
view.addGestureRecognizer(swipeDown)
}
func handleSwipes(sender:UISwipeGestureRecognizer) {
let increment: Int
let offset: CGFloat
// up or down
if sender.direction == .Up {
increment = 1
offset = 10
} else {
increment = -1
offset = -10
}
題:
據我所知,您想在向上滑動時存儲該+10值,您可以購買,將您的increment
和offset
聲明為全局類,如下所示:
class ViewController: UIViewController {
//Declare it below your class declaration
var increment = 0
var offset: CGFloat = 0
}
在您的代碼中,您要在handleSwipes
函數中聲明這兩個變量,以便在調用此函數時它將變為0,偏移量將始終變為+10或-10,但是一旦在全局類中對其進行聲明,它將在其保留后保留其值獲取並且如果每次調用handleSwipes
函數時都希望增加它,則可以這樣進行:
offset += 10
offset -= 10
您的increment
變量也會發生同樣的事情,您可以根據需要進行更改,然后可以通過這種方式將標簽位置更改為handleSwipes
函數:
yourLbl.center = CGPoint(x: yourLbl.center.x, y: yourLbl.center.y + offset)
您的handleSwipes
函數將為:
func handleSwipes(sender:UISwipeGestureRecognizer) {
// up or down
if sender.direction == .Up {
increment = 1
offset += 10
println(offset)
yourLbl.center = CGPoint(x: yourLbl.center.x, y: yourLbl.center.y + offset)
} else {
increment = -1
offset -= 10
println(offset)
yourLbl.center = CGPoint(x: yourLbl.center.x, y: yourLbl.center.y + offset)
}
}
希望這會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.