[英]programmatically simulate a swipe gesture in Swift
我正在實現一個手勢識別器,用於在 Swift 中滑動。 我希望能夠模擬卡片的投擲(以編程方式滑動視圖)。
我以為會有一個內置功能,但我發現的只是一個用於點擊手勢而不是滑動手勢的功能。
這就是我實現滑動手勢的方式:
let gesture = UIPanGestureRecognizer(target: self, action: Selector("wasDragged:"))
cardView.addGestureRecognizer(gesture)
cardView.userInteractionEnabled = true
}
func wasDragged (gesture: UIPanGestureRecognizer) {
let translation = gesture.translationInView(self.view)
let cardView = gesture.view!
// Move the object depending on the drag position
cardView.center = CGPoint(x: self.view.bounds.width / 2 + translation.x,
y: self.view.bounds.height / 2 + translation.y)
您可以自己創建UIPanGestureRecognizer並將其傳遞給wasDragged
方法。 不過,您應該檢查翻譯的不同值:
let gesture = UIPanGestureRecognizer()
gesture.setTranslation(CGPointMake(0, 100), inView: self.view)
wasDragged(gesture)
斯威夫特 4.2
let gesture = UIPanGestureRecognizer()
gesture.setTranslation(CGPoint(x: 0, y: 100), in: self.view)
wasDragged(gesture)
雖然我認為你需要別的東西。 為什么你首先需要模擬這個手勢?
對於 SWIFT 3.0
let swipeRightOrange:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(slideToRightWithGestureRecognizer))
swipeRightOrange.direction = .Right;
let swipeLeftOrange:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(slideToLeftWithGestureRecognizer))
swipeLeftOrange.direction = .Left;
@IBAction func slideToLeftWithGestureRecognizer(gestureRecognizer:UISwipeGestureRecognizer)
{
viewOrange.backgroundColor = UIColor.blue
}
@IBAction func slideToRightWithGestureRecognizer(gestureRecognizer:UISwipeGestureRecognizer)
{
viewOrange.backgroundColor = UIColor.lightGray
}
您無法模擬手勢識別器的全部含義(我的意思是,您實際上無法讓 iOS 認為這是一個真實的用戶操作)。
但是,您可以欺騙自己的代碼,使其表現得像真正的滑動一樣。 為此,您需要先創建一個手勢識別器:
var gestureRecognizerSwipeRight = UISwipeGestureRecognizer(target: self, action: "activatedGestureRecognizer:")
gestureRecognizerSwipeRight.direction = UISwipeGestureRecognizerDirection.Right
yourView.addGestureRecognizer(gestureRecognizerSwipeRight)
然后將其直接傳遞給您的操作:
// Some other place in your code
self.activatedGestureRecognizer(gesture: gestureRecognizerSwipeRight)
您的activatedGestureRecognizer(gesture:)
方法應該類似於:
func activatedGestureRecognizer(gesture: UIGestureRecognizer) {
if let gestureRecognizer = gesture as? UIGestureRecognizer {
// Here you can compare using if gestureRecognizer == gestureRecognizerSwipeRight
// ...or you could compare the direction of the gesture recognizer.
// It all depends on your implementation really.
if gestureRecognizer == gestureRecognizerSwipeRight {
// Swipe right detected
}
}
}
公平地說,我認為這樣做沒有任何實際好處。 簡單地執行與滑動相關的操作而不是實際模擬手勢識別器應該會好得多。
例如,如果您需要在刷卡時為卡片設置動畫,為什么不簡單地禁用卡片視圖上的用戶交互並以編程方式對其進行動畫處理呢?
這是可用於以編程方式滑動單元格的代碼。 執行時,單元格將動畫到滑動位置。
@IBAction
func swipeFirstCell() {
if let cell = tableView.cellForRow(at: IndexPath(row: 0, section: 0)) {
tableView.perform(Selector("_endSwipeToDeleteRowDidDelete:"), with: nil) // cancel any existing swipes
tableView.perform(Selector("_swipeToDeleteCell:"), with: cell)
}
}
請注意,您還需要為來自canEditRowAtIndexPath
的單元格返回 true。
這可能會被 Apple 的私有 API 檢測標記,但您至少可以將其用於開發,如果您真的需要,可能會混淆字符串。
您需要實現UISwipeGestureRecognizer
override func viewDidLoad() {
super.viewDidLoad()
var swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
swipeRight.direction = UISwipeGestureRecognizerDirection.Right
self.view.addGestureRecognizer(swipeRight)
var swipeDown = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
swipeDown.direction = UISwipeGestureRecognizerDirection.Down
self.view.addGestureRecognizer(swipeDown)
}
func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.Right:
print("Swiped right")
case UISwipeGestureRecognizerDirection.Down:
print("Swiped down")
case UISwipeGestureRecognizerDirection.Left:
print("Swiped left")
case UISwipeGestureRecognizerDirection.Up:
print("Swiped up")
default:
break
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.