swift 如何实现点击view后显示灰色背景

有这样一种场景,当我们点击view的时候,需要过0.几秒显示一个灰色或者别的颜色的背景

用button来实现,只有按下去的时候才会出现,往往在快速按下,快速抬起的时候是看不出这个变化的

下边是解决方案

 1 override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
 2         
 3         let touch = touches.first!
 4         let p = touch.locationInView(self.retweetBackgroundView)
 5         let insideRetweet = CGRectContainsPoint(self.retweetBackgroundView!.bounds ,p)
 6         
 7         if self.retweetBackgroundView?.hidden == false && insideRetweet {
 8             
 9             self.retweetBackgroundView!.performSelector("setBackgroundColor:", withObject: kWBCellHighlightColor, afterDelay: 0.15)
10             self._touchRetweetView = true
11         }else {
12 
13             self.contentView!.performSelector("setBackgroundColor:", withObject: kWBCellHighlightColor, afterDelay: 0.15)
14             self._touchRetweetView = false
15         }
16 
17     }
18     
19     override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
20         
21         self.touchesRestoreBackgroundColor()
22     }
23     
24     override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
25         
26         self.touchesRestoreBackgroundColor()
27     }
28     
29     func touchesRestoreBackgroundColor() {
30         NSObject.cancelPreviousPerformRequestsWithTarget(self.retweetBackgroundView!, selector: "setBackgroundColor:", object: kWBCellHighlightColor)
31          NSObject.cancelPreviousPerformRequestsWithTarget(self.contentView!, selector: "setBackgroundColor:", object: kWBCellHighlightColor)
32         self.retweetBackgroundView!.backgroundColor = UIColor.whiteColor()
33         self.contentView.backgroundColor = kWBCellInnerViewColor
34     }
原文地址:https://www.cnblogs.com/machao/p/5127098.html