gpt4 book ai didi

ios - SwipeGestureRecognizer 无法快速向左、向上和向下正常工作

转载 作者:行者123 更新时间:2023-11-28 12:58:11 25 4
gpt4 key购买 nike

我正在尝试在一个快速应用程序中,在一堆动态生成的 UIView 上为所有四个方向实现 swipeGestureRecognizer。所以基本上,我希望这些 View 中的每一个都能响应所有四个方向的滑动。这是我的代码

    import UIKit

class ViewController: UIViewController {



override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.


//create a view to represent the main box
let mainBox = UIView(frame: CGRectMake(0,0, 320, 40 ))
mainBox.backgroundColor = UIColor.redColor()
mainBox.layer.cornerRadius = 0
mainBox.layer.borderWidth=1
self.view.addSubview(mainBox) //Add the newly created view to the main view(self)





//Create some UIViews on the fly

for var i=0; i<8; ++i{
//Add an image view
let tile = UIImageView(frame: CGRectMake(CGFloat(i)*CGFloat(40),0,40,40 )) //Create a new view
//Style the imageview
tile.backgroundColor=UIColor.greenColor()
tile.layer.cornerRadius=2
tile.layer.borderWidth=1


tile.userInteractionEnabled = true

let swipeRight = UISwipeGestureRecognizer(target: self, action:Selector("tileRightSwiped:"))
swipeRight.direction = .Right
tile.addGestureRecognizer(swipeRight)


let swipeLeft = UISwipeGestureRecognizer(target: self, action: Selector("tileLeftSwiped"))
swipeLeft.direction = .Left
tile.addGestureRecognizer(swipeLeft)


let swipeDown = UISwipeGestureRecognizer(target: self, action: Selector("tileDownSwiped"))
swipeDown.direction = .Down
tile.addGestureRecognizer(swipeDown)


let swipeUp = UISwipeGestureRecognizer(target: self, action: Selector("tileUpSwiped"))
swipeUp.direction = .Up
tile.addGestureRecognizer(swipeUp)











mainBox.addSubview(tile) //Add the newly created view to mainBox


}




}




override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}





//swipe gestures
func tileRightSwiped(gestureRecognizer: UISwipeGestureRecognizer){
print("right swiped")
}

func tileLeftSwiped(gestureRecognizer: UISwipeGestureRecognizer){
print("left swiped ")
}

func tileDownSwiped(gestureRecognizer: UISwipeGestureRecognizer){
print("down swiped ")
}

func tileUpSwiped(gestureRecognizer: UISwipeGestureRecognizer){
print("Up swiped ")
}




}

右滑工作正常,但我不明白为什么对于其他 3 个方向,应用程序意外中止并且我收到这样的错误“[myApp.ViewController tileDownSwiped]:发送到实例 0x7fd230dbf5d0 的无法识别的选择器”是我错误地使用了滑动手势识别器还是我的代码中的其他地方有问题?我完全不知所措了。非常感谢任何帮助。

最佳答案

您添加到手势的选择器是错误的,因为您错过了末尾的冒号。这就是您看到的崩溃的原因。

您可能还需要向手势识别器添加委托(delegate)并实现 shouldRecognizeSimultaneouslyWithGestureRecognizer:所以他们会同时工作。

关于ios - SwipeGestureRecognizer 无法快速向左、向上和向下正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34479945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com