我正在实现一个手势识别器,用于在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)
SWIFT 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
}
}
}
公平地说,我认为这样做并没有任何实际的好处。简单地执行与滑动相关联的操作而不是实际模拟手势识别器应该更好。
例如,如果您需要在刷卡时为卡片制作动画,为什么不简单地禁用卡片视图上的用户交互并以编程方式为其设置动画?
你需要实现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
}
}
}