当且仅当 CGPoint 在视图内持续 2 秒时才启动函数

Initiate a function if and only if a CGPoint is within a view for 2 seconds

在情节提要中,我有一个 ViewController 叫家。

Home 有一个红色的 UIView 作为子视图,位于 Home 的中心。

红色视图有一个 LongPressGestureRecognizer,激活后用户可以在屏幕上移动红色视图。

当用户将红色视图移到屏幕左侧足够近时,弹出 UIView Sidebar

边栏内有 n 颜色独特的 subviews

当用户在彩色子视图的框架内移动红色视图时,颜色mix和红色视图接触到的侧边栏中的颜色会改变颜色。变混音了。

`

 func handleAuditioneeLongPress(sender: UILongPressGestureRecognizer) {
        switch sender.state {
        case .Began:
            hideSideBar()
        case .Changed:
            let touchPoint = sender.locationInView(self.view)
            thumbnailTrackTouchPoint(touchPoint)
        case .Ended:
            let dropPoint = sender.locationInView(self.view)
            thumbnailWasDropped(dropPoint)
        default:
            break
        }
    }
func thumbnailTrackTouchPoint(touchPoint: CGPoint){
      UIView.setAnimationsEnabled(true)
      UIView.animateWithDuration(0.03, delay: 0.0, options: [.BeginFromCurrentState, .CurveEaseOut], animations: {
                self.red.center.x = touchPoint.x
            self.red.center.y = touchPoint.y - 50
            }, completion: nil)
     if touchPoint.x < 30{
         showSidebar()
         isSidebarOut = true
      }
     if isSidebarOut {
            if touchPoint.x > 135 {
                hideSideBar()
                isSidebarOut = false
            }

            let location = self.view.convertPoint(touchPoint, toView: sidebar)
            let subview = sidebar.hitTest(location, withEvent: nil)
            if( subview != nil) {
            if let coloredIconController = self.sidebarController.viewToColoredIconController[subview!]


/*
if coloredIconController.view.frame.contains(location) for more than 2 seconds mix the colors
*/

` 当用户操纵侧边栏时,我不想意外混合。

所以我想要一个检查条件。当且仅当接触点已包含在彩色视图中 2 秒时,红色才应该混合。

当点进入视图时启动预定计时器

myTimer = NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: "mixing method", userInfo: userInfo, repeats: false)

当 cgpoint 出来时,你就

if myTimer != nil {
   myTimer.invalidate()
}