swiftUI基础 滑动手势UISwipeGestureRecognizer


class ViewController: UIViewController {
    
    @IBOutlet weak var image2: UIImageView!
    
    override func viewDidLoad() {
        
        let swipeGestureRight = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(_:)))
        swipeGestureRight.direction = UISwipeGestureRecognizerDirection.Right
        image2.addGestureRecognizer(swipeGestureRight)
        
        let swipeGestureDown = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(_:)))
        swipeGestureDown.direction = UISwipeGestureRecognizerDirection.Down
        image2.addGestureRecognizer(swipeGestureDown)
        
        let swipeGestureLeft = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(_:)))
        swipeGestureLeft.direction = UISwipeGestureRecognizerDirection.Left
        image2.addGestureRecognizer(swipeGestureLeft)
        
        let swipeGestureUp = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture(_:)))
        swipeGestureUp.direction = UISwipeGestureRecognizerDirection.Up
        image2.addGestureRecognizer(swipeGestureUp)
        
        super.viewDidLoad()
        
        image2.image = UIImage(named: "apple.png")
    }
    
    @IBAction func respondToSwipeGesture(send: UIGestureRecognizer) {
        
        if let swipeGesture = send as? UISwipeGestureRecognizer {
            
            switch swipeGesture.direction {
            case UISwipeGestureRecognizerDirection.Right:
                changeImage()
                print("Swiped right")
            case UISwipeGestureRecognizerDirection.Down:
                changeImage()
                print("Swiped down")
            case UISwipeGestureRecognizerDirection.Left:
                changeImage()
                print("Swiped left")
            case UISwipeGestureRecognizerDirection.Up:
                changeImage()
                print("Swiped up")
            default:
                break
            }
        }
    }
    
    func changeImage(){
        if (image2.image == UIImage(named: "apple.png")){
            image2.image = UIImage(named: "image1.png")}
        else{
            image2.image = UIImage(named: "apple.png")
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
}


if let 的这两种用法有什么区别?

  1. let button = self.view.viewWithTag(1) as? UIButton
  2. button?.enabled = true
  3. //和
  4. if let button = self.view.viewWithTag(1) as? UIButton{
  5. button.enabled = true
  6. }


应该没有区别吧,但是下面那种是推荐用法。毕竟官方的例子都是用下面的,而且不容易错。 

button 尝试让 button 为 UIButton 类型
let button = self.view.viewWithTag(1) as? UIButton

因为你不能肯定知道 button 是不是 UIButton 类型, 所以在用 enabled 的时候需要这样写 「button?」来避免 crash
button?.enabled = true

检查 button 是否为 UIButton 类型
if let button = self.view.viewWithTag(1) as? UIButton{

if 里面肯定是, 所以不用「button?」这样写
button.enabled = true
}

if let 的作用域只在花括号里

第一种可能就需要一直 button?.xxx button?.xxx button?.xxx  (内部可能需做什么判断吧?)
会耗掉一些效能吧? 




你可能感兴趣的:(swiftUI基础 滑动手势UISwipeGestureRecognizer)