在Swift中,无论如何都知道要刷哪个对象?
问题描述:
我想将发送方redBox
传递给功能leftSwipeFunc
.我不知道如何传递不是UISwipeGestureREcognizer
的参数.如果您知道答案,请帮助我.
I want to pass the sender redBox
to the function leftSwipeFunc
. I don't know how to pass a parameter that's not UISwipeGestureREcognizer
. Please help me if you know the answer.
let swipeLeft = UISwipeGestureRecognizer(target: self, action: Selector("leftSwipeFunc:"))
swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
redBox.addGestureRecognizer(swipeLeft)
func leftSwipeFunc(gesture:UISwipeGestureRecognizer){ }
答
手势识别器具有一个称为view
的属性,该属性是它们所附加的UIView
.因此,您可以在leftSwipeFunc
内部使用该视图来获取视图:
Gesture recognizers have a property called view
that is the UIView
that they are attached to. So, you can use that inside of leftSwipeFunc
to get the view:
func leftSwipeFunc(gesture: UISwipeGestureRecognizer) {
let swipedView = gesture.view
// Assuming redBox is a property of your view controller
if swipedView == redBox {
println("the redBox was swiped")
} else {
println("some other view was swiped")
}
}